-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ssid
196 lines (170 loc) · 8.26 KB
/
check_ssid
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
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import os
import subprocess
import datetime
import json
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)
#-------------------------------------------------------------------------------
def mergestatus(previous,newinput):
result = previous
if newinput == "CRITICAL":
result = newinput
elif newinput == "WARNING" and result != "CRITICAL":
result = newinput
return result
def match_ssid(ssid,listregex):
for testregex in listregex:
if re.match(testregex,ssid):
return True
return False
def dossidcall(interface,ignoredlist,alertlist,expected,utilwarn,utilcrit):
cmdline = ["/usr/sbin/iw","dev",interface,"info"]
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:
return ("WARNING","The interface "+interface+" is not valid "+interface+".")
else:
for line in output.splitlines():
cleared = line.strip()
parts = cleared.split()
if len(parts)==2 and parts[0]=="type":
if parts[1] not in ["managed"]:
return ("UNKNOWN","The interface "+interface+" is mode "+parts[1]+".")
cmdline = ["/usr/bin/jc","iw","dev",interface,"scan"]
infomsg(" ".join(cmdline))
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:
return ("WARNING","Cannot get the wireless info for the interface "+interface+".")
resultstatus = ""
resultmessages = []
ignoredcount = 0
alertcount = 0
resultperf = []
jsoninfo = json.loads(output)
infomsg(jsoninfo)
totalcount = len(jsoninfo)
for network in jsoninfo:
infomsg(network)
if "ssid" in network.keys():
current_ssid = network["ssid"]
if "primary_channel" in network.keys():
current_channel = str(network["primary_channel"])
else:
current_channel = "unknown"
if match_ssid(current_ssid,ignoredlist):
resultmessages.append("The SSID "+current_ssid+" with channel "+current_channel+" matches the ignore list "+str(ignoredlist)+".")
ignorecount=ignorecount+1
elif match_ssid(current_ssid,alertlist):
resultstatus = "CRITICAL"
resultmessages.append("The SSID "+current_ssid+" with channel "+current_channel+" matches the alert list "+str(alertlist)+".")
alertcount=alertcount+1
elif current_ssid in expected.keys():
current_mac = network["bssid"].strip().upper()
cleaned_mac = current_mac.replace(":","")
expected_macs = expected[current_ssid]
if cleaned_mac not in expected_macs:
resultstatus = "CRITICAL"
resultmessages.append("The base mac "+current_mac+" for SSID "+current_ssid+" with channel "+current_channel+" is not in valid list "+str(expected_macs)+".")
if "channel_utilisation" in network.keys():
current_utilization_parts = network["channel_utilisation"].strip().split("/")
current_utilization = round((int(current_utilization_parts[0])/int(current_utilization_parts[1]))*100)
infomsg(current_utilization)
if current_utilization >= utilcrit:
resultstatus = "CRITICAL"
resultmessages.append("The channel utilization is "+str(current_utilization)+"% for SSID "+current_ssid+" with channel "+current_channel+" has reached the critical "+str(utilcrit)+"%.")
elif current_utilization >= utilwarn:
resultstatus = "WARNING"
resultmessages.append("The channel utilization is "+str(current_utilization)+"% for SSID "+current_ssid+" with channel "+current_channel+" has reached the warning "+str(utilwarn)+"%.")
else:
resultmessages.append("The channel utilization is "+str(current_utilization)+"% for SSID "+current_ssid+" with channel "+current_channel+" is OK.")
else:
current_utilization=-1
resultperf.append("ssid_"+current_ssid+"="+str(current_utilization))
return (resultstatus," ".join(resultmessages)+" | totalcount="+str(totalcount)+" ignoredcount="+str(ignoredcount)+" alertcount="+str(alertcount)+" "+" ".join(resultperf))
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--alert", dest="alert", default="", help="alert if an ssid matching")
parser.add_argument("-g", "--ignored", dest="ignored", default="", help="ssids to ignore")
parser.add_argument("-e", "--expected", dest="expected", default="", help="expected mac addresses for ssids")
parser.add_argument("-u", "--utilization", dest="utilization", default="70;90", help="max utilization of expected ssids warning;critical")
parser.add_argument("-i", "--interface", dest="interface", default="", help="interface to use")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
interfacelist = args.interface.split(",")
interfacelist.remove("")
if len(interfacelist)==0:
searchpath = "/sys/class/net"
entries = os.listdir(searchpath)
for entry in entries:
if os.path.isdir(os.path.join(searchpath,entry,"wireless")):
interfacelist.append(entry)
infomsg(str(interfacelist))
ignoredlist = args.ignored.split("¶")
if "" in ignoredlist:
ignoredlist.remove("")
alertlist = args.alert.split("¶")
if "" in alertlist:
alertlist.remove("")
exptparts = args.expected.split("¶")
if "" in exptparts:
exptparts.remove("")
expected = {}
for entry in exptparts:
segments = entry.strip().split("=")
ssid = segments[0]
macs = segments[1].replace(";",",").split(",")
cleanedmacs = []
for tempmac in macs:
cleanedmacs.append(tempmac.strip().replace(":","").upper())
expected[ssid] = cleanedmacs
utilparts = args.utilization.split(";")
utilwarn = int(utilparts[0].strip())
utilcrit = int(utilparts[1].strip())
finalstatus = ""
finalmessages = []
for interface in interfacelist:
status, message = dossidcall(interface,ignoredlist,alertlist,expected,utilwarn,utilcrit)
finalstatus = mergestatus(finalstatus,status)
if message != "":
finalmessages.append(message)
if (finalstatus==""):
exitnagios("OK","Wireless info is valid. "+" ".join(finalmessages))
else:
exitnagios(finalstatus," ".join(finalmessages))
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------