-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_interfacespeed
101 lines (85 loc) · 4.19 KB
/
check_interfacespeed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import os
import subprocess
import platform
#-------------------------------------------------------------------------------
_candebug = False
def candebug():
global _candebug
return _candebug
def setcandebug(value):
global _candebug
_candebug = value
def infomsg(msg):
if candebug() == True:
print(msg, flush=True)
def exitnagios(status,message):
if status=="OK":
exitcode = 0
elif status=="WARNING":
exitcode = 1
elif status=="CRITICAL":
exitcode = 2
elif status=="UNKNOWN":
exitcode = 3
else:
exitcode = 4
print(status+": "+message, flush=True)
sys.exit(exitcode)
#-------------------------------------------------------------------------------
def doospfroutescall(interface,expectedspeeds,warningspeeds,expectedduplex,warningduplex):
cmdline = ["/usr/sbin/ethtool",interface]
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8").strip()
errors = completedproc.stderr.decode("utf-8").strip()
exitcode = completedproc.returncode
if exitcode == 0:
foundspeed = ""
foundduplex = ""
lines = output.splitlines()
for line in lines:
#infomsg(line)
tokenspeed = "\tSpeed:"
if line.startswith(tokenspeed):
foundspeed = line[len(tokenspeed)+1:]
foundspeed = foundspeed.lower()
tokenduplex = "\tDuplex:"
if line.startswith(tokenduplex):
foundduplex = line[len(tokenduplex)+1:]
foundduplex = foundduplex.lower()
if (foundspeed!="" and len(expectedspeeds)==0) and (foundduplex!="" and len(expectedduplex)==0):
exitnagios("OK","the interface speed is "+foundspeed+" and duplex is "+foundduplex)
elif (foundspeed in expectedspeeds) and (foundduplex in expectedduplex):
exitnagios("OK","the interface speed is "+foundspeed+" and duplex is "+foundduplex+" as expected")
elif (foundspeed in warningspeeds) and (foundduplex in warningduplex):
exitnagios("WARNING","the interface speed is "+foundspeed+" and duplex is "+foundduplex+" but it was expected "+" ".join(expectedspeeds)+" and "+" ".join(expectedduplex))
else:
exitnagios("CRITICAL","the interface speed is "+foundspeed+" and duplex is "+foundduplex+" but it was expected "+" ".join(expectedspeeds)+" and "+" ".join(expectedduplex))
else:
exitnagios("CRITICAL","issue retrieving the interface information")
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", dest="interface", default="", help="wan,lan,eth0")
parser.add_argument("-e", "--expected", dest="expected", default="1000Mb/s", help="1000Mb/s")
parser.add_argument("-w", "--warning", dest="warning", default="100Mb/s", help="100Mb/s")
parser.add_argument("-u", "--duplex", dest="duplex", default="full", help="full or half")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
interface = args.interface
if interface == "":
exitnagios("CRITICAL","no interface selected")
expectedspeeds = []
for item in args.expected.split("¶"):
expectedspeeds.append(item.strip().lower())
warningspeeds = []
for item in args.warning.split("¶"):
warningspeeds.append(item.strip().lower())
doospfroutescall(interface,expectedspeeds,warningspeeds,[args.duplex.lower()],[])
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------