From 37d504a0397e78deb2b7ed54a7cda44dfb56d536 Mon Sep 17 00:00:00 2001 From: Tomas Edwardsson Date: Wed, 2 Apr 2014 15:34:07 +0000 Subject: [PATCH] Added okc-get_network_stat --- .../nagios-okplugin-get_network_stat.spec | 43 ++++++++++++ okc-get_network_stat/okc-get_network_stat | 65 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 okc-get_network_stat/nagios-okplugin-get_network_stat.spec create mode 100644 okc-get_network_stat/okc-get_network_stat diff --git a/okc-get_network_stat/nagios-okplugin-get_network_stat.spec b/okc-get_network_stat/nagios-okplugin-get_network_stat.spec new file mode 100644 index 0000000..e36d1ec --- /dev/null +++ b/okc-get_network_stat/nagios-okplugin-get_network_stat.spec @@ -0,0 +1,43 @@ +%define debug_package %{nil} + +Summary: A Nagios plugin to get network statistics over NRPE +Name: nagios-okplugin-get_network_stat +Version: 1.0.0 +Release: 1%{?dist} +License: GPLv2+ +Group: Applications/System +URL: https://github.com/opinkerfi/nagios-plugins/okc-get_network_stat +Source0: https://github.com/opinkerfi/nagios-plugins/okc-get_network_stat/%{name}-%{version}.tar.gz +Requires: nagios-plugins-nrpe +BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) +Packager: Tomas Edwardsson +BuildArch: noarch + + + +%description +A Nagios plugin to get network statistics over NRPE + + +%prep +%setup -q + +%build + + +%install +rm -rf %{buildroot} +install -D -p -m 0755 okc-get_network_stat %{buildroot}%{_libdir}/nagios/plugins/okc-get_network_stat + +%clean +rm -rf %{buildroot} + +%files +%defattr(-,root,root,-) +#%doc README LICENSE +#%{_libdir}/nagios/plugins/* +%{_libdir}/nagios/plugins/okc-get_network_stat + +%changelog +* Wed Apr 2 2014 Tomas Edwardsson 1.0.0-1 +- Initial release diff --git a/okc-get_network_stat/okc-get_network_stat b/okc-get_network_stat/okc-get_network_stat new file mode 100644 index 0000000..05cc498 --- /dev/null +++ b/okc-get_network_stat/okc-get_network_stat @@ -0,0 +1,65 @@ +#!/usr/bin/python + +from pynag.Plugins import PluginHelper, ok, warning, critical, unknown +from pynag.Utils import runCommand +import re + +helper = PluginHelper() + +helper.parser.add_option("-H", help="Host to connect to", dest="host", default='localhost') + +helper.parse_arguments() + +if helper.options.host == "localhost": + command="cat /proc/net/dev|sed 's/ */ /g'" +else: + command="/usr/lib64/nagios/plugins/check_nrpe -H '%s' -c get_network_interfaces" % helper.options.host + +return_code, stdout, stderr = runCommand(command) + +try: + receive, transmit = stdout.split("\n")[1].split("|")[1:] + receive = receive.split() + transmit = transmit.split() + + interfaces = [] + + for int_line in stdout.split("\n")[2:]: + int_line = int_line.strip() + if not int_line: + continue + + int_arr = re.split("[: ]+", int_line.strip()) + + interface = int_arr.pop(0) + + if interface == "lo" or interface.startswith('sit'): + continue + + interfaces.append(interface) + for i in range(0, len(receive)): + helper.add_metric( + label="%s_rx_%s" % (interface, receive[i]), + value=int_arr.pop(0), + uom="c") + for i in range(0, len(transmit)): + helper.add_metric( + label="%s_tx_%s" % (interface, transmit[i]), + value=int_arr.pop(0), + uom="c") + + helper.add_summary("Network interfaces %s" % ", ".join(interfaces)) + + # Here all metrics will be checked against thresholds that are either + # built-in or added via --threshold from the command-line + helper.check_all_metrics() + + # Print out plugin information and exit nagios-style + helper.exit(ok) +except Exception, error: + helper.status(unknown) + helper.add_summary(stdout + stderr) + helper.exit(unknown) + +# vim: sts=4 expandtab autoindent +