-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_interfacenetwork
134 lines (112 loc) · 5.33 KB
/
check_interfacenetwork
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import os
import subprocess
import netifaces
import ipaddress
#-------------------------------------------------------------------------------
_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 dointerfacenetworkcheck(interface,segment,address,existence,notexists):
interfaces = netifaces.interfaces()
found_addrs = []
found_interface = False
for item in interfaces:
if item == interface:
found_interface = True
addrs = netifaces.ifaddresses(interface)
if netifaces.AF_INET in addrs.keys():
for entry in addrs[netifaces.AF_INET]:
found_addrs.append(entry)
if netifaces.AF_INET6 in addrs.keys():
for entry in addrs[netifaces.AF_INET6]:
found_addrs.append(entry)
break
infomsg(found_addrs)
if notexists == True:
if found_interface:
exitnagios("WARNING","interface "+interface+" exists, but it should not exists")
else:
exitnagios("OK","interface "+interface+" does not exists")
elif segment=="" and address=="":
if (found_interface == True) and (existence == True):
exitnagios("OK","interface "+interface+" exists")
else:
exitnagios("CRITICAL","no IP for interface "+interface)
else:
errors_segment = []
errors_address = []
if segment != "":
for test in segment.split(","):
found = False
for option in found_addrs:
if "/" in option["netmask"]:
extra = option["netmask"].split("/")[-1]
else:
extra = option["netmask"]
network = ipaddress.ip_network(option["addr"]+"/"+extra, strict=False)
if str(network)==str(ipaddress.ip_network(test, strict=False)):
found = True
break
if found == False:
errors_segment.append(test)
if address != "":
for test in address.split(","):
found = False
for option in found_addrs:
testparts = test.replace(":",".").split(".")
refeparts = option["addr"].replace(":",".").split(".")
if len(testparts)==len(refeparts):
matching = True
for index, item in enumerate(testparts):
if testparts[index]!="*":
matching = matching and (refeparts[index]==testparts[index])
if matching:
found = True
if found == False:
errors_address.append(address)
if len(errors_segment)==0 and len(errors_address)==0:
exitnagios("OK","found a match - segment: "+segment+" - address: "+address)
else:
exitnagios("CRITICAL","could not find a match - segment errors: "+str(errors_segment)+" - address errors: "+str(errors_address)+" - current: "+str(found_addrs))
exitnagios("CRITICAL","unexpected case")
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-I", "--interface", dest="interface", default="", help="interface to check")
parser.add_argument("-s", "--segment", dest="segment", default="", help="expected network segment")
parser.add_argument("-a", "--address", dest="address", default="", help="expected network address")
parser.add_argument("-x", "--existence", action="store_true", dest="existence", default=False, help="check only for interface exitence")
parser.add_argument("-t", "--notexists", action="store_true", dest="notexists", default=False, help="check only for interface not exitence")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
if (args.interface == ""):
exitnagios("CRITICAL","interface not defined")
dointerfacenetworkcheck(args.interface,args.segment,args.address,args.existence,args.notexists)
main()
#-------------------------------------------------------------------------------