-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_usb
140 lines (121 loc) · 5.08 KB
/
check_usb
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import re
import sys
import subprocess
import argparse
#-------------------------------------------------------------------------------
_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 status_to_code(status):
if status in ["OK","0",0]:
exitcode = 0
elif status in ["WARNING","1",1]:
exitcode = 1
elif status in ["CRITICAL","2",2]:
exitcode = 2
elif status in ["UNKNOWN","3",3]:
exitcode = 3
else:
exitcode = 4
return exitcode
def exitnagios(status,message):
exitcode = status_to_code(status)
print(status+": "+message, flush=True)
sys.exit(exitcode)
#-------------------------------------------------------------------------------
def check_the_line(lineid,linemsg,device_identifiers,device_messages):
matching = False
infomsg(lineid)
infomsg(linemsg)
device_options = device_identifiers.split(",")
for option in device_options:
optionparts = option.strip().split(":")
device_id = optionparts[0]+":"+optionparts[1]
if len(optionparts)>2:
device_range = optionparts[2]
else:
device_range = None
infomsg(device_id)
infomsg(device_range)
infomsg(device_messages)
if lineid.lower() == device_id.lower():
if device_messages == "":
matching = True
break
else:
for item in device_messages.split("¶"):
if item.strip() == linemsg.strip():
matching = True
break
return matching
def is_usb_connected(device_identifiers,device_messages,device_expected):
found = False
try:
cmdline = ["/usr/bin/lsusb"]
completedproc = subprocess.run(cmdline,capture_output=True)
output = completedproc.stdout.decode("utf-8")
errors = completedproc.stderr.decode("utf-8")
exitcode = completedproc.returncode
founddict = {}
if exitcode==0:
infomsg(output)
for line in output.splitlines():
parts = line.split()
if parts[0]=="Bus" and parts[2] == "Device":
infomsg(line)
if check_the_line(parts[5]," ".join(parts[6:]),device_identifiers,device_messages):
key = device_identifiers+"@"+device_messages
if key in founddict:
founddict[key] = founddict[key]+1
else:
founddict[key] = 1
else:
exitnagios("CRITICAL","issue wiht lsusb")
except Exception as e:
exitnagios("CRITICAL","internal error - "+str(e))
#here parse dictkey and extract range before @ and makesure it matches
totalfound = None
try:
totalfound = founddict[device_identifiers+"@"+device_messages]
if device_expected=="":
expected_min = None
expected_max = None
else:
parts = device_expected.split(":")
if len(parts)==1:
expected_min = int(device_expected)
expected_max = int(device_expected)
else:
expected_min = int(parts[0])
expected_max = int(parts[1])
if (device_expected=="") or ((totalfound>=expected_min) and (totalfound<=expected_max)):
exitnagios("OK","found "+str(totalfound)+" USB "+str(founddict))
else:
exitnagios("CRITICAL","cannot find the USB device, or the count of them is not matching "+str(founddict)+" - expected "+str(device_expected))
except Exception as e:
if totalfound == None:
exitnagios("CRITICAL","cannot find "+device_identifiers+" "+device_messages)
else:
exitnagios("CRITICAL","internal error - "+str(e))
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--devices", dest="devices", default="", help="devices" )
parser.add_argument("-m", "--messages", dest="messages", default="", help="messages")
parser.add_argument("-e", "--expected", dest="expected", default="", help="expected")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
is_usb_connected(args.devices,args.messages,args.expected)
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------