-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ipv6ra
116 lines (97 loc) · 3.85 KB
/
check_ipv6ra
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
#!/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 doipv6racall(interface,expected,ignored):
infomsg("expected: "+str(expected))
infomsg("ignored: "+str(ignored))
cmdline = ["/usr/bin/rdisc6","--numeric","--quiet","--multiple","10",interface]
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8")
errors = completedproc.stderr.decode("utf-8")
response = completedproc.returncode
infomsg(output)
rafound = []
if response == 0:
alllines = output.splitlines()
for line in alllines:
line = line.strip()
if line!="":
if line not in rafound:
rafound.append(line)
infomsg(rafound)
missing = []
for item in expected:
if (item not in rafound) and (item not in ignored):
missing.append(item)
unexpected = []
for item in rafound:
if (item not in expected) and (item not in ignored):
unexpected.append(item)
count_missing = len(missing)
count_unexpected = len(unexpected)
if count_missing==0 and count_unexpected==0:
exitnagios("OK","The found ipv6 RA are as expected - "+str(expected)+" | missing=0 unexpected=0")
else:
messages = []
if count_missing>0:
messages.append("There are missing RA - "+str(missing))
if count_unexpected>0:
messages.append("There are unexpected RA - "+str(unexpected))
exitnagios("CRITICAL"," --- ".join(messages)+" | missing="+str(count_missing)+" unexpected="+str(count_unexpected))
#-------------------------------------------------------------------------------
#collect arguments
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interface", action="store", dest="interface", default="", help="interface to evaluate")
parser.add_argument("-e", "--expected", action="store", dest="expected", default="", help="expected RA prefixes")
parser.add_argument("-g", "--ignored", action="store", dest="ignored", default="", help="ignore these RA prefixes")
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 args.interface=="":
exitnagios("CRITICAL","no interface selected")
expected = args.expected.split(",")
if "" in expected:
expected.remove("")
if None in expected:
expected.remove(None)
ignored = args.ignored.split(",")
if "" in ignored:
ignored.remove("")
if None in ignored:
ignored.remove(None)
doipv6racall(args.interface,expected,ignored)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------