-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_arping
98 lines (78 loc) · 3.33 KB
/
check_arping
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import os
import subprocess
#-------------------------------------------------------------------------------
_candebug = False
def setcandebug(value):
global _candebug
_candebug = value
def candebug():
global _candebug
return _candebug
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 doarpingcall(hostname,packets,timeout):
cmdline = ["/usr/sbin/arping","-C",str(packets),"-w",str(timeout),hostname]
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8")
errors = completedproc.stderr.decode("utf-8")
response = completedproc.returncode
infomsg(output)
#and then check the response...
if response == 0:
alllines = output.splitlines()
allmacs = []
for line in alllines:
if " bytes from " in line:
parts = line.split()
mac = parts[3]
if mac not in allmacs:
allmacs.append(mac)
infomsg(allmacs)
if len(allmacs)==1:
linems = alllines[-1]
linepercent = alllines[-2]
avgms = float(linems.split("=")[1].strip().split()[0].split("/")[1])
percent = float(linepercent.split(",")[2].strip().split()[0].replace("%",""))
exitnagios("OK",hostname+" is respoding to ARP | average="+str(avgms)+" percent="+str(percent))
else:
exitnagios("CRITICAL",hostname+" has several MACs responding to ARP "+str(allmacs)+" | average=0 percent=0")
else:
exitnagios("CRITICAL",hostname+" is not responding to ARP | average=0 percent=0")
#-------------------------------------------------------------------------------
#collect arguments
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--hostname", action="store", help="target IP or hostname")
parser.add_argument("-p", "--packets", action="store", type=int, default=5, help="number of pings")
parser.add_argument("-t", "--timeout", action="store", type=int, default=10, help="timeout")
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.hostname:
exitnagios("CRITICAL","Must give a hostname.")
doarpingcall(args.hostname,args.packets,args.timeout)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------