www.paulscott.za.net

Paul Scott

Paul Scott


Member of The Internet Defense League
Social
Follow @paulscott56
Stats
In order to subscribe, please send a chat IM message with your favourite client containing ONLY the word subscribe to: paulscottzanet@gmail.com

Updates 229
Subscribers 5
Stuff
RSS 2.0 Feed
SIOC
Show Tag Clouds
Quick Search
Please enter a keyword to search for

Rants and musings of Paul Scott


Paul Scott on 2009-05-26 11:54:39 Comments (1) Tweet this

I needed a quick script to monitor a bunch of sites quickly and *very* simply. I looked at doing this all in a single thread with a ping, but then decided to go a non blocking route and make the thing a little more thread safe. This was done so that multiple sites that actually could go down could be monitored with relative accuracy, without a single site blocking the rest.

OK, enough flapping, here is the code (Python this time)


import threading
import os, popen2, select, signal
import Queue
import sys, xmpp
import time

HOST_UP = 1
HOST_DOWN = 0

PING_INTERVAL = 60

jidparams={'jid':'pingerget@gmail.com/pinger', 'password':'somepass'}
jid=xmpp.protocol.JID(jidparams['jid'])
cl=xmpp.Client(jid.getDomain(), debug=[])
con=cl.connect() ##proxy={'host':'cache.uwc.ac.za','port':'8080','user':'userdude','password':'le password'})
if not con:
print 'could not connect!'
sys.exit()

auth=cl.auth(jid.getNode(),jidparams['password'],resource=jid.getResource())

if not auth:
print 'could not authenticate!'
sys.exit()

def log(s):
print s
text = s
tojid='ping-alerts@jabber.org'
#cl.SendInitPresence(requestRoster=0)
id=cl.send(xmpp.protocol.Message(tojid,text))
time.sleep(1)

class Pinger(threading.Thread):
def __init__(self, queue, address, *args, **kwargs):
self.address = address
self.queue = queue
threading.Thread.__init__(self, *args, **kwargs)
self.stop = 0

def run(self):
child = popen2.Popen3("ping -i %i %s 21" % (PING_INTERVAL, self.address))
while 1:
ready_fds = select.select([child.fromchild], [], [])
line = child.fromchild.readline()
if line.find("Destination Host Unreachable") = 0:
log("host %s is down" % self.address)
else:
pass

if self.stop:
os.kill(child.pid, signal.SIGTERM)
break

def main():
pinglist = ["www.uwc.ac.za", "ics.uwc.ac.za", "avoir.uwc.ac.za", "eteaching.uwc.ac.za", "elearn.uwc.ac.za", "fsiu.uwc.ac.za", "172.16.65.208"]
threads = []
queue = Queue.Queue()

for adr in pinglist:
threads.append(Pinger(queue, adr))

for thread in threads:
thread.start()

try:
while 1:
time.sleep(1)
except KeyboardInterrupt:
pass

for thread in threads:
thread.stop = 1

for thread in threads:
thread.join(2.0)

# Ok, I've had enough of you stoopid threads.
os._exit(0)

if __name__ == "__main__":
main()





Powered by Chisimba