-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_devolospeed
184 lines (157 loc) · 6.95 KB
/
check_devolospeed
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
##aptitude install python-nagiosplugin
import argparse
import subprocess
import socket
import sys
import re
#-------------------------------------------------------------------------------
_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)
#-------------------------------------------------------------------------------
original_getaddrinfo = socket.getaddrinfo
def forced_ipv6_gai_family(*args, **kwargs):
global original_getaddrinfo
responses = original_getaddrinfo(*args, **kwargs)
return [response
for response in responses
if response[0] == socket.AF_INET6]
def forced_ipv4_gai_family(*args, **kwargs):
global original_getaddrinfo
responses = original_getaddrinfo(*args, **kwargs)
return [response
for response in responses
if response[0] == socket.AF_INET]
#-------------------------------------------------------------------------------
def getmacfromip(remoteIP):
command = ["/usr/sbin/arp","-n",str(remoteIP)]
output = subprocess.check_output(command).decode("utf-8")
rows = output.split("\n")
infomsg(rows)
if len(rows) > 0:
row = rows[1]
parts = row.split()
if len(parts) > 0:
infomsg(parts)
return parts[2]
return ""
def findinterfaces():
result = []
command = ["/usr/bin/plcdevs"]
output = subprocess.check_output(command).decode("utf-8")
for row in output.split("\n"):
parts = row.split()
if len(parts) > 0:
result.append(parts[0])
return result
def getspeeds(interface,targetmac):
command = ["/usr/bin/plcstat", "-i", interface, "-t", "-q"]
output = subprocess.check_output(command).decode("utf-8")
rows = output.split("\n")
infomsg(rows)
for row in output.split("\n"):
parts = row.split()
if len(parts) > 0:
infomsg(parts[3])
if (parts[3] == targetmac):
speeds = [parts[5], parts[6]]
infomsg(speeds)
return(speeds)
return None
def change_mac(base, offset):
infomsg(base)
mac = base.replace(":","").replace("-","").replace(" ","")
infomsg(mac)
old_string = "{:012X}".format(int(mac, 16) + offset)
new_string = ":".join(s for s in re.split(r"(\w{2})", old_string.upper()) if s.isalnum())
return new_string
#-------------------------------------------------------------------------------
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host", dest="host", help="Hostname")
parser.add_argument("-m", "--mac", dest="mac", default=None, help="MAC address of remote station")
parser.add_argument("-w", "--warning", dest="warning", default=None, help="Warning level")
parser.add_argument("-c", "--critical", dest="critical" , default=None, help="Critical level")
parser.add_argument("-t", "--timeout", dest="timeout", default="10:CRITICAL", help="timeout value:STATUS")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
return args
def main():
args = parse_args()
setcandebug(args.debug)
if not args.host:
exitnagios("CRITICAL","Must give a hostname.")
hostname = args.host
if (args.warning == None):
warning = 0
else:
warning = int(args.warning)
if (args.critical == None):
critical = 0
else:
critical = int(args.critical)
if (args.mac == None):
targetip = socket.gethostbyname(hostname)
if (targetip == ""):
exitnagios("CRITICAL","It was not possible to resolve hostname into IP havig hostname as "+hostname)
tempmac = getmacfromip(targetip)
infomsg(targetmac)
if (tempmac == ""):
exitnagios("CRITICAL","It was not possible to resolve IP into MAC. having hostname as "+hostname+" and targetip as "+targetip)
targetmac = change_mac(tempmac,1)
else:
targetmac = args.mac
parts = args.timeout.split(":")
timeoutvalue = int(parts[0])
if len(parts)>1:
timeoutstatus = parts[1]
else:
timeoutstatus = "CRITICAL"
if timeoutstatus in ["OK", "WARNING", "CRITICAL", "UNKNOWN"]:
interfaces = findinterfaces()
for interface in interfaces:
infomsg(interface)
speeds = getspeeds(interface,targetmac)
if speeds != None:
upload = speeds[0]
download = speeds[1]
if (upload == "n/a") or (download == "n/a"):
exitnagios("OK","Direct link! targetmac is "+targetmac)
else:
upload = int(upload)
download = int(download)
average = (upload+download)/2
if (average > warning):
exitnagios("OK","Speed is fast enough. Having targetmac as "+targetmac+" upload as "+str(upload)+" and download as "+str(download)+" and average: "+str(average)+" | average="+str(average)+" upload="+str(upload)+" download="+str(download))
elif (average > critical):
exitnagios("WARNING","Speed is in warning range. Having targetmac as "+targetmac+" and upload as "+str(upload)+" and download as "+str(download)+" and average: "+str(average)+" | average="+str(average)+" upload="+str(upload)+" download="+str(download))
else:
exitnagios("CRITICAL","Speed is in critical range. Having targetmac as "+targetmac+" and upload as "+str(upload)+" and download as "+str(download)+" and average: "+str(average)+" | average="+str(average)+" upload="+str(upload)+" download="+str(download))
exitnagios(timeoutstatus,"It was not possible to retreive speeds having targetmac as "+targetmac)
else:
exitnagios("CRITICAL","timeout status is not valid")
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------