Created
December 29, 2012 03:22
-
-
Save dbr/4404340 to your computer and use it in GitHub Desktop.
Script to ping a bunch of hosts, and send the resolve/ping time to Graphite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import time | |
import subprocess | |
import socket | |
class CannotResolve(Exception): | |
pass | |
def resolve(host): | |
start = time.time() | |
try: | |
ip = socket.gethostbyname(host) | |
except Exception, e: | |
import traceback | |
traceback.print_exc() | |
raise CannotResolve("Cannot resolve %s: %s" % (host, e)) | |
else: | |
end = time.time() | |
return (ip, (end-start)*1000) | |
def ping(host): | |
cmd = ['ping', '-c', '1', host] | |
start = time.time() | |
try: | |
p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) | |
so, se = p.communicate() | |
except Exception: | |
# On error, return 0 | |
print "Error running %s" % cmd | |
print "stderr:\n%s" % se | |
return 0 | |
else: | |
end = time.time() | |
return (end-start)*1000 | |
class Connection(object): | |
def __init__(self, host, port): | |
self.host = host | |
self.port = port | |
def connect(self): | |
retries = 10 | |
for x in range(retries): | |
try: | |
self.sock = socket.socket() | |
self.sock.connect( (self.host, self.port) ) | |
except Exception: | |
import traceback | |
traceback.print_exc() | |
print "Retry %s" % (x+1) | |
time.sleep(1) | |
else: | |
return True | |
print "Giving up after %s attempts" % retries | |
def send(self, msg): | |
print "Send: %r" % msg | |
self.connect() | |
retries = 10 | |
for x in range(retries): | |
try: | |
self.sock.sendall(msg) | |
except Exception: | |
import traceback | |
traceback.print_exc() | |
print "Retry %s" % (x+1) | |
time.sleep(1) | |
else: | |
return True | |
print "Giving up after %s attempts" % retries | |
def main(): | |
delay = 1 | |
hosts = ["google.com", "stackoverflow.com"] | |
conn = Connection('127.0.0.1', 2003) | |
while True: | |
for h in hosts: | |
now = int(time.time()) | |
hostname = socket.gethostname() | |
print "Resolving %s" % h | |
try: | |
ip, speedms = resolve(h) | |
except CannotResolve, e: | |
print "Cannot resolve", e | |
# Zero values | |
conn.send( | |
"pings.%s.dns_%s %s %d\n" % ( | |
hostname, h.replace(".", "_"), 0, now)) | |
conn.send( | |
"pings.%s.ping_%s %s %d\n" % ( | |
hostname, h.replace(".", "_"), 0, now)) | |
continue # Next host | |
else: | |
conn.send( | |
"pings.%s.dns_%s %s %d\n" % ( | |
hostname, h.replace(".", "_"), speedms, now)) | |
now = int(time.time()) | |
print "Pinging %s (%s)" % (ip, h) | |
p = ping(h) | |
print "done" | |
conn.send( | |
"pings.%s.ping_%s %s %d\n" % ( | |
hostname, h.replace(".", "_"), p, now)) | |
time.sleep(delay) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment