This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwstatus.py
executable file
·356 lines (298 loc) · 11.5 KB
/
gwstatus.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/python
# CENTOS 5.x users use #!/usr/bin/python26
"""Check Gateway Health."""
# Joey Stanford <[email protected]>
from Queue import Queue
from threading import Thread
class NetWorker(Thread):
"""Defines the Worker which does all the work"""
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
def run(self):
"""Let's run the worker"""
while True:
# Get the work from the queue and expand the tuple
systems, repeater, myip = self.queue.get()
check_ircddb(systems, repeater)
check_single_dashboard(systems, repeater, myip)
check_pingable(systems, repeater, myip)
self.queue.task_done()
class Repeater(object):
"""Repeater Details Object"""
def __init__(self, callsign, location):
self.callsign = callsign.upper()
self.location = location
self.dashboard = ""
self.pingable = ""
self.ip = ""
self.ircddbip = ""
self.web_status = ""
self.in_gwys_file = False
def readconfigfile(configfile):
"""setup the config file"""
config = ConfigParser.ConfigParser()
try:
config.read(configfile)
except ConfigParser.Error, err:
print 'Config File Error:', err
exit(1)
return config
def ping(ip):
"""perform a safe ping"""
ret = subprocess.call("ping -c 1 %s" % ip,
shell=True,
stdout=open('/dev/null', 'w'),
stderr=subprocess.STDOUT)
return (ret)
def get_ip():
"""determine our external ip"""
ip_regex = '([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})'
conn = httplib.HTTPConnection("checkip.dyndns.org", timeout=30)
conn.request("GET", "")
res = conn.getresponse()
if res.status == 200:
ip = re.split(ip_regex, res.read())[1]
else:
print 'Error connecting to the server!! Check your internet connection'
exit()
conn.close()
return ip
def process_gwys_file(gwys, systems):
"""read gateway file and extract data we care about"""
for line in gwys.readlines():
if len(line.split()) == 3:
callsign, ip, _ = line.split()
elif len(line.split()) == 2:
callsign, _ = line.split()
ip = ""
else:
print "Bad gwys.txt line: %s" % (line)
continue
callsign = callsign.upper()
if callsign in systems:
systems[callsign].in_gwys_file = True
systems[callsign].ip = ip
def check_ircddb(systems, repeater):
"""check ircddb status"""
if (repeater.startswith("XRF") or repeater.startswith("REF")):
# no ddns yet
systems[repeater].ircddbip = ""
else:
ircddbgw = repeater.lower() + ".gw.ircddb.net"
try:
systems[repeater].ircddbip = socket.gethostbyname(ircddbgw)
except socket.gaierror:
systems[repeater].ircddbip = ""
def check_single_dashboard(systems, repeater, myip):
"""check the dashboard of each repeater"""
if systems[repeater].ip == myip:
systems[repeater].dashboard = "SELF"
systems[repeater].web_status = "N/A"
elif (systems[repeater].ip == "" and systems[repeater].ircddbip == ""):
systems[repeater].dashboard = "OFFLINE"
systems[repeater].web_status = "No IP Addresss"
else:
# let's see if we can connect using one of the IPs
try:
if systems[repeater].ip == "":
conn = httplib.HTTPConnection(systems[repeater].ircddbip,
timeout=30)
else:
conn = httplib.HTTPConnection(systems[repeater].ip, timeout=30)
conn.request("HEAD", "/")
res = conn.getresponse()
conn.close()
except socket.error:
resstatus = 408
except httplib.BadStatusLine:
resstatus = 204
else:
resstatus = res.status
# Let's interpret the results
if resstatus == 200:
systems[repeater].dashboard = "ONLINE"
elif (resstatus in [503, 408]):
systems[repeater].dashboard = "OFFLINE"
else:
# Broken systems e.g. "No Content"
systems[repeater].dashboard = httplib.responses[resstatus]
# set the web status as our parting gift
systems[repeater].web_status = str(resstatus)
def check_pingable(systems, repeater, myip):
"""Can we even reach the system?"""
if (systems[repeater].ip == "" and systems[repeater].ircddbip == ""):
systems[repeater].pingable = "BROKEN"
elif systems[repeater].ip == myip:
systems[repeater].pingable = "SELF"
elif (systems[repeater].ip == "" and systems[repeater].ircddbip != ""):
if ping(systems[repeater].ircddbip) == 0:
systems[repeater].pingable = "ONLINE"
else:
systems[repeater].pingable = "OFFLINE"
elif ping(systems[repeater].ip) == 0:
systems[repeater].pingable = "ONLINE"
else:
systems[repeater].pingable = "OFFLINE"
def generate_html(systems):
"""Generate the HTML file"""
keylist = systems.keys()
keylist.sort()
# HTML vars
startline = "<TD BGCOLOR=#EEEEEE>"
endline = "</TD>\n"
down = '<B><FONT COLOR="#CC0000">OFFLINE</FONT></B></TD>\n'
broken = '<B><FONT COLOR="#FFA500">BROKEN</FONT></B></TD>\n'
up = '<FONT COLOR="#00BB00">ONLINE</FONT></TD>\n'
details = ('<a href='
'"http://status.ircddb.net/qam.php?call=CALLSIGN"'
'>CALLSIGN</a>')
with open(config.get("files", "htmlout"), "w") as html:
with open(config.get("files", "header"), "r") as header:
# write out header
for line in header.readlines():
html.write(line)
html.write("\n")
html.write("<P><SMALL>")
html.write("Last Updated: ")
html.write(ctime())
html.write("</SMALL></P>")
html.write("\n")
# write out collected data
# for repeater in systems:
for repeater in keylist:
html.write("<TR>\n")
# callsign
html.write(startline)
if (systems[repeater].callsign.startswith("XRF")
or systems[repeater].callsign.startswith("REF")):
# XRF & REF not handled by ircddb
html.write(systems[repeater].callsign)
else:
html.write(
details.replace("CALLSIGN", systems[repeater].callsign))
html.write(endline)
# dashboard status
html.write(startline)
if systems[repeater].dashboard == "SELF":
html.write(broken.replace("BROKEN", "SELF"))
elif systems[repeater].dashboard == "OFFLINE":
html.write(down)
elif systems[repeater].dashboard == "ONLINE":
if systems[repeater].ip == "":
linked_ip = \
'<a href="http://' + \
systems[repeater].ircddbip + \
'">ONLINE</a>'
else:
linked_ip = \
'<a href="http://' + \
systems[repeater].ip + \
'">ONLINE</a>'
html.write(up.replace("ONLINE", linked_ip))
else:
html.write(
broken.replace("BROKEN", systems[repeater].dashboard))
# ping status
html.write(startline)
if systems[repeater].pingable == "OFFLINE":
html.write(down)
elif systems[repeater].pingable == "BROKEN":
html.write(broken)
elif systems[repeater].pingable == "SELF":
html.write(broken.replace("BROKEN", "SELF"))
elif systems[repeater].pingable == "ONLINE":
html.write(up)
else:
html.write(broken)
# location
html.write(startline)
html.write(systems[repeater].location)
html.write(endline)
# IP
html.write(startline)
if systems[repeater].ip == "":
if systems[repeater].in_gwys_file:
html.write(broken)
else:
html.write(broken.replace("BROKEN", "MISSING"))
elif (systems[repeater].callsign.startswith("XRF")
or systems[repeater].callsign.startswith("REF")
or (systems[repeater].ip == systems[repeater].ircddbip)):
html.write(up.replace("ONLINE", systems[repeater].ip))
else:
html.write(broken.replace("BROKEN", systems[repeater].ip))
html.write(endline)
# ddns ip
html.write(startline)
if (systems[repeater].callsign.startswith("XRF")
or systems[repeater].callsign.startswith("REF")):
# no ddns yet
html.write("[N/A]")
else:
if systems[repeater].ircddbip == "":
html.write(down)
elif systems[repeater].ircddbip == systems[repeater].ip:
html.write(up.replace("ONLINE",
systems[repeater].ircddbip))
else:
html.write(
broken.replace("BROKEN", systems[repeater].ircddbip))
html.write(endline)
# detailed web status
html.write(startline)
html.write(
("<a href='https://http.cat/" + systems[repeater].web_status +
"'>" + systems[repeater].web_status + "</a>"))
html.write(endline)
# finish up
html.write("</TABLE>\n")
html.write("</BODY>\n")
html.write("</HTML>\n")
def main():
"""Main Function"""
# load up the systems we want to interrogate
systems = {}
for callsign, location in config.items("systems"):
systems[callsign.upper()] = Repeater(callsign, location)
# grab our IP so we can know if we're running the script
# on the same IP as a systems[repeater].and indicate as such
myip = get_ip()
# remove old gwysfile from previous run and download the latest
# copy
subprocess.call(["rm", config.get("files", "gwysfile")])
subprocess.call(["wget", config.get("files", "gwysdownload")])
# read in gateways
with open(config.get("files", "gwysfile"), "r") as gwys:
process_gwys_file(gwys, systems)
# Net operations take time so we're going to have each repeater
# have it's own worker thread to speed up the process
# Create a queue to communicate with the worker threads
queue = Queue()
# Create 1 worker thread for each repeater
ip_num = len(systems)
for x in range(ip_num):
worker = NetWorker(queue)
# Setting daemon to True will let the main thread exit even
# though the workers are blocking
worker.daemon = True
worker.start()
# determine systems[repeater].status
for repeater in systems:
# Put the tasks into the queue as a tuple
queue.put((systems, repeater, myip))
# Causes the main thread to wait for the queue to finish
# processing all the tasks
queue.join()
# write out html
generate_html(systems)
if __name__ == "__main__":
import subprocess
from time import ctime
import httplib
import re
import ConfigParser
import socket
configfile = "gwstatus.ini"
config = readconfigfile(configfile)
main()