-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_snmp_ports
143 lines (120 loc) · 5.23 KB
/
check_snmp_ports
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
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
import argparse
import sys
import re
import subprocess
import datetime
import socket
#-------------------------------------------------------------------------------
_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)
#-------------------------------------------------------------------------------
original_getaddrinfo = socket.getaddrinfo
def forced_ipv6_gai_family(*args, **kwargs):
global original_getaddrinfo
responses = original_getaddrinfo(*args, **kwargs)
return [response
for response in responses
if response[0] == socket.AF_INET6]
def forced_ipv4_gai_family(*args, **kwargs):
global original_getaddrinfo
responses = original_getaddrinfo(*args, **kwargs)
return [response
for response in responses
if response[0] == socket.AF_INET]
#-------------------------------------------------------------------------------
def dosnmpuptimecall(listports, expected, hostname, port, community, timeoutvalue, timeoutstatus):
found = {}
token = "iso.3.6.1.2.1.2.2.1.5."
exitcode, stdout = snmpget(hostname, port, community, ".1.3.6.1.2.1.2.2.1.5")
if exitcode==None:
exitnagios(timeoutstatus,"Timeout")
elif exitcode==0:
for line in stdout.strip().splitlines():
if line.startswith(token):
parsing = line[len(token):]
parts = parsing.split("=",1)
port=parts[0].strip()
payload=parts[1].strip().split()
found[port]=payload[1]
else:
exitnagios("CRITICAL","Unexpected scenario")
infomsg(found)
validports = listports.split(",")
infomsg(validports)
errors = {}
success = {}
for testport in validports:
if testport in found.keys():
current = found[testport]
if current==expected:
success[testport] = current
else:
errors[testport] = current
else:
errors[testport] = "?"
if len(errors)==0:
exitnagios("OK","all ports as expected "+str(success))
else:
exitnagios("CRITICAL","some ports are not expected "+str(errors)+" - "+str(success))
def snmpget(hostname, port, community, oid):
cmdline = ["/usr/bin/snmpwalk","-v","2c","-c",community,hostname+":"+str(port),oid]
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 errors.startswith("Timeout: No Response from "):
exitcode = None
output = None
return (exitcode, output)
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--hostname", dest="hostname", default="", help="server to check")
parser.add_argument("-p", "--port", dest="port", default=161, help="port to use, usually 143 or 993")
parser.add_argument("-t", "--timeout", dest="timeout", default="30:CRITICAL", help="timeout value:STATUS")
parser.add_argument("-m", "--community", dest="community", default="public", help="private/public")
parser.add_argument("-l", "--listports", dest="listports", default="", help="comma separated list of ports to check speed")
parser.add_argument("-e", "--expected", dest="expected", default="", help="expected speed")
parser.add_argument("-®", "--debug", action="store_true", dest="debug", default=False, help="be more verbose")
args = parser.parse_args()
setcandebug(args.debug)
if args.hostname == "":
exitnagios("CRITICAL","no hostname specified")
parts = args.timeout.split(":")
timeoutvalue = int(parts[0])
if len(parts)>1:
timeoutstatus = parts[1]
else:
timeoutstatus = "CRITICAL"
if timeoutstatus in ["OK", "WARNING", "CRITICAL", "UNKNOWN"]:
dosnmpuptimecall(args.listports,args.expected,args.hostname,int(args.port),args.community,timeoutvalue,timeoutstatus)
else:
exitnagios("CRITICAL","timeout status is not valid")
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------
#timeout
#warning