[packages/nagios-plugin-check_iface] Ver 2.0; python based variant

arekm arekm at pld-linux.org
Tue Jul 11 12:36:32 CEST 2023


commit 3fa5fece07b0491d13510f9e489212a5b0d2c11b
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Tue Jul 11 11:35:19 2023 +0200

    Ver 2.0; python based variant

 check_iface.py                 | 87 ++++++++++++++++++++++++++++++++++++++++++
 nagios-plugin-check_iface.spec | 11 +++---
 2 files changed, 93 insertions(+), 5 deletions(-)
---
diff --git a/nagios-plugin-check_iface.spec b/nagios-plugin-check_iface.spec
index e6294e7..c13184d 100644
--- a/nagios-plugin-check_iface.spec
+++ b/nagios-plugin-check_iface.spec
@@ -1,15 +1,16 @@
 %define		plugin	check_iface
 Summary:	Nagios/Icinga plugin for checking network interface speed
 Name:		nagios-plugin-%{plugin}
-Version:	1.0
-Release:	3
-License:	GPL
+Version:	2.0
+Release:	1
+License:	GPL v3
 Group:		Networking
-Source0:	https://raw.githubusercontent.com/wifibox/linux-admin-tools/master/nagios/plugins/check_net_iface
+Source0:	check_iface.py
 # Source0-md5:	30366d25cf1e3b035cf49f2d5d556cbe
 Source1:	check_iface.cfg
-URL:		https://github.com/wifibox/linux-admin-tools/blob/master/nagios/plugins/check_net_iface
 Requires:	nagios-common
+Requires:       python3-netifaces
+Requires:       python3-psutil
 BuildArch:	noarch
 BuildRoot:	%{tmpdir}/%{name}-%{version}-root-%(id -u -n)
 
diff --git a/check_iface.py b/check_iface.py
new file mode 100644
index 0000000..14dc5a2
--- /dev/null
+++ b/check_iface.py
@@ -0,0 +1,87 @@
+#!/usr/bin/python3
+# GPL v3+
+
+import argparse
+import netifaces
+import psutil
+import sys
+
+NAGIOS_OK = 0
+NAGIOS_WARNING = 1
+NAGIOS_CRITICAL = 2
+NAGIOS_UNKNOWN = 3
+
+nagios_states = {
+        NAGIOS_OK: "OK",
+        NAGIOS_WARNING: "WARNING",
+        NAGIOS_CRITICAL: "CRITICAL",
+        NAGIOS_UNKNOWN: "UNKNOWN"
+        }
+
+nagios_state = NAGIOS_UNKNOWN
+
+def check_duplex(value):
+    allowed_duplex = [ "full", "half" ]
+    if value not in allowed_duplex:
+        raise argparse.ArgumentTypeError("must be one of: %s" % ", ".join(allowed_duplex))
+
+    if value == "full":
+        return psutil._common.NicDuplex.NIC_DUPLEX_FULL
+    elif value == "half":
+        return psutil._common.NicDuplex.NIC_DUPLEX_HALF
+
+def get_default_iface():
+    try:
+        return netifaces.gateways()['default'][netifaces.AF_INET][1]
+    except IndexError as e:
+        return "eth0"
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--speed', '-s', action="store", type=int, default=1000, help='expected minimal interface speed')
+parser.add_argument('--duplex', '-d', action="store", type=check_duplex, default="full", help='expected duplex')
+parser.add_argument('--mtu', '-m', action="store", type=int, default=1500, help='expected minimal MTU')
+parser.add_argument('--interface', '-i', action="store", type=str, help='interface name')
+parser.add_argument("--verbose", help="Verbose mode.", action="store_true")
+
+args = parser.parse_args()
+
+try:
+
+    if not args.interface:
+        args.interface = get_default_iface()
+
+    ifs = psutil.net_if_stats()
+    if args.interface not in ifs:
+        nagios_state = NAGIOS_CRITICAL
+        print("Network interface `{iface}' not found".format(iface=args.interface))
+        sys.exit(nagios_state)
+
+    msgs = []
+
+    iface = ifs[args.interface]
+
+    nagios_state = NAGIOS_OK
+
+    if iface.duplex != psutil._common.NicDuplex.NIC_DUPLEX_UNKNOWN and iface.duplex < args.duplex:
+        nagios_state = max(nagios_state, NAGIOS_CRITICAL)
+        msgs.append("duplex={duplex} (CRITICAL, expected: {duplex_expected})".format(duplex=str(iface.duplex), duplex_expected=args.duplex))
+    else:
+        msgs.append("duplex={duplex} (OK)".format(duplex=str(iface.duplex)))
+
+    if iface.speed != 0 and iface.speed < args.speed:
+        nagios_state = max(nagios_state, NAGIOS_CRITICAL)
+        msgs.append("speed={speed} (CRITICAL, expected min: {speed_expected})".format(speed=iface.speed, speed_expected=args.speed))
+    else:
+        msgs.append("speed={speed} (OK)".format(speed=iface.speed))
+
+    if iface.mtu != 0 and iface.mtu != args.mtu:
+        nagios_state = max(nagios_state, NAGIOS_CRITICAL)
+        msgs.append("mtu={mtu} (CRITICAL, expected min: {mtu_expected})".format(mtu=iface.mtu, mtu_expected=args.mtu))
+    else:
+        msgs.append("mtu={mtu} (OK)".format(mtu=iface.mtu))
+
+    print("{state} - interface `{iface}': {msg}".format(state=nagios_states[nagios_state], iface=args.interface, msg=", ".join(msgs)))
+except Exception as e:
+    print("UNKNOWN: %{e}".format(e=e))
+    nagios_state = NAGIOS_UNKNOWN
+sys.exit(nagios_state)
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/nagios-plugin-check_iface.git/commitdiff/3fa5fece07b0491d13510f9e489212a5b0d2c11b



More information about the pld-cvs-commit mailing list