-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_port_external
228 lines (200 loc) · 9.2 KB
/
check_port_external
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
import os
import sys
import subprocess
import argparse
import mechanize
import socket
import time
from bs4 import BeautifulSoup
#-------------------------------------------------------------------------------
_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 dointernalcall(ipprotocol,tcporudp,hostname,port):
try:
if ipprotocol=="4":
sfamily = socket.AF_INET
elif ipprotocol=="6":
sfamily = socket.AF_INET6
else:
return "ERROR: invalid ip version protocol '"+ipprotocol+"'"
try:
info = socket.getaddrinfo(hostname, None, sfamily)
ip_address = info[0][4][0]
infomsg("The ip address is "+ip_address)
except:
return "ERROR: issue resolving ip address"
sock = None
if tcporudp == "tcp":
sock = socket.socket(sfamily, socket.SOCK_STREAM)
elif tcporudp == "udp":
sock = socket.socket(sfamily, socket.SOCK_DGRAM)
else:
return "ERROR: specify the protocol as tcp or udp instead of '"+tcporudp+"'"
timeout = 5
sock.settimeout(timeout)
content = "ERROR: uninitialized"
if tcporudp == "tcp":
try:
status = sock.connect_ex((ip_address, port))
infomsg("the tcp status is "+str(status))
if status==0:
content = "OPEN"
elif status in [110,113,11]:
content = "TIMEOUT"
elif status in [111]:
content = "CLOSED"
else:
content = "ERROR: "+str(status)
except TimeoutError as e:
content = "TIMEOUT"
except Exception as e:
infomsg("socket exception -> "+str(e))
elif tcporudp == "udp":
try:
sock.sendto("", (ip_address, port))
content = "OPEN"
except socket.error as e:
content = "CLOSED"
except Exception as e:
infomsg("socket exception -> "+str(e))
if sock!=None:
sock.close()
except Exception as e:
infomsg("block exception -> "+str(e))
content=""
return content
def dohttpcall(baseurl,accesstoken,ipprotocol,tcporudp,hostname,port):
try:
url = baseurl+"?accesstoken="+accesstoken+"&hostname="+hostname+"&protocol="+tcporudp+"&port="+str(port)
if ipprotocol != "":
url = url+"&ipv="+ipprotocol
infomsg(url)
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots file
#br.addheaders = [("User-agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")]
br.addheaders = [("User-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0")]
res = br.open(url)
content = res.read().decode()
except Exception as e:
content=""
return content
#-------------------------------------------------------------------------------
def doportexternalcallsimple(baseurl,accesstoken,ipprotocol,tcporudp,hostname,port,responses,timeoutvalue,timeoutstatus):
try:
ipprotocol = ipprotocol.strip().replace("-","")
tcporudp = tcporudp.lower().strip()
if baseurl=="":
content = dointernalcall(ipprotocol,tcporudp,hostname,port)
else:
content = dohttpcall(baseurl,accesstoken,ipprotocol,tcporudp,hostname,port)
infomsg(content)
if content == None:
exitnagios("CRITICAL","no result from page")
lines = content.splitlines()
if len(lines)>0:
status = lines[0].split(":")[0].lower()
infomsg(status)
if status not in ["open","closed","timeout"]:
return ("CRITICAL","not valid check status: "+content)
else:
if status in responses:
exitnagios("OK","the result "+status+" for port "+str(port)+" was as the expected "+str(responses))
else:
if status == "timeout":
exitstatus = timeoutstatus
else:
exitstatus = "CRITICAL"
return (exitstatus,"the result "+status+" for port "+str(port)+" was not the in the expected "+str(responses))
else:
return ("CRITICAL","empty output in check")
except Exception as e:
return ("CRITICAL","unexpected error during the check "+str(e))
def doportexternalcall(retries,baseurl,accesstoken,ipprotocol,tcporudp,hostname,port,responses,timeoutvalue,timeoutstatus):
counter = 0
while True:
exitlevel,message = doportexternalcallsimple(baseurl,accesstoken,ipprotocol,tcporudp,hostname,port,responses,timeoutvalue,timeoutstatus)
if (exitlevel=="OK") or (counter>=retries):
exitnagios(exitlevel, message)
else:
counter = counter+1
time.sleep(5)
exitnagios("CRITICAL","unexpected state")
#-------------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-4", "--ipv4", action="store_true", dest="ipv4", default=False, help="use ipv4 (default, exclusive with ipv6)")
parser.add_argument("-6", "--ipv6", action="store_true", dest="ipv6", default=False, help="use ipv6 (exclusive with ipv4)")
parser.add_argument("-0", "--ipv0", action="store_true", dest="ipv0", default=False, help="do not specify ip protocol version")
parser.add_argument("-H", "--hostname", dest="hostname", default="", help="server to check")
parser.add_argument("-p", "--port", dest="port", default="", help="TCP port number to check")
parser.add_argument("-r", "--responses", dest="responses", default="open", help="respose: open,closed,timeout")
parser.add_argument("-t", "--timeout", dest="timeout", default="30:CRITICAL", help="timeout value:STATUS")
parser.add_argument("-k", "--accesstokenfile", dest="accesstokenfile", default="", help="file containing the authentication token for the remote service")
parser.add_argument("-u", "--baseurl", dest="baseurl", default="", help="url to use")
parser.add_argument("-s", "--retries", action="store", default="3", help="number of times to retry the logic")
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","hostname not defined")
protocol = None
exclist = 0
if args.ipv0 == True :
protocol = ""
exclist = exclist+1
if args.ipv4 == True :
protocol = "-4"
exclist = exclist+1
if args.ipv6 == True :
protocol = "-6"
exclist = exclist+1
if (exclist>1) :
exitnagios("CRITICAL","select ip protocol version 4 or 6, or 0 to relay on OS handling")
elif (exclist<1) :
exitnagios("CRITICAL","no protocol selected")
tcporudp = "tcp"
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"]:
if args.accesstokenfile=="":
accesstoken = ""
else:
with open(args.accesstokenfile) as handle:
accesstoken=handle.read().splitlines()[0]
baseurl = args.baseurl
# if baseurl=="":
# baseurl = "https://www.bigbroeyes.com/external_checks/check_port.php"
retries = int(args.retries)
doportexternalcall(retries,baseurl,accesstoken,protocol,tcporudp,args.hostname,int(args.port),args.responses.split(","),timeoutvalue,timeoutstatus)
else:
exitnagios("CRITICAL","timeout status is not valid")
if __name__ == "__main__":
main()
#-------------------------------------------------------------------------------