From 062ee7a521afbf9322dd991e2d8997aa19e08041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Martin=20Sl=C3=A5en?= Date: Mon, 6 Feb 2012 00:36:03 +0100 Subject: [PATCH] trying to save objects at the end... --- scripts/ping_service.py | 43 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/scripts/ping_service.py b/scripts/ping_service.py index 86f3302..cac20ca 100755 --- a/scripts/ping_service.py +++ b/scripts/ping_service.py @@ -19,6 +19,10 @@ parser.add_argument('--num-pings', dest='num_pings', type=int, default=5,\ args = parser.parse_args() +#project_path = '/opt/django/dns_mdb/' +#if project_path not in sys.path: +# sys.path.append(project_path) + from django.core.management import setup_environ from django.core.mail import mail_admins from dns_mdb import settings @@ -29,6 +33,37 @@ PING_CMD='ping -W 1 -c %d -n -q %s | grep rtt | cut -d " " -f4' queue = Queue.Queue() + +RRD_CREATE = "%s/%s_%s.rrd " \ + "DS:ttl:GAUGE:600:U:U "\ + "RRA:AVERAGE:0.5:1:576 "\ + "RRA:AVERAGE:0.5:6:336 "\ + "RRA:AVERAGE:0.5:72:124 "\ + "RRA:AVERAGE:0.5:288:365" + +RRD_DIR = "../rrd" + +class RrdService(): + def __init__(self, host, interface): + self.host = host + self.interface = interface + + def log(self, avg_ping): + self.update_rrd(avg_ping) + + def update_rrd(self, avg_ping): + self.create_rrd() + + def create_rrd(self): + rrd_file = RRD_CREATE % (RRD_DIR, host.hostname, interface.name) + if args.debug: + print "RRD_FILE : %s " % rrd_file +# if not os.system(RRD_CREATE % (RRD_DIR, host.hostname, interface.name)): +# if args.debug: +# print "Could not create RRD database." +# return False + return True + class PingThread(threading.Thread): """ Threaded ping """ def __init__(self, host_queue): @@ -55,7 +90,7 @@ class PingThread(threading.Thread): return interface.ip4address.last_contact = datetime.datetime.now() interface.ip4address.ping_avg_rtt = float(res[1]) - interface.ip4address.save() +# interface.ip4address.save() if args.debug: print "%s (%s/%s) : %s" % (host, interface.name, \ interface.ip4address.address, res) @@ -100,3 +135,9 @@ for host in hosts: # Wait for all threads to finish, eg. queue is empty. queue.join() + +# Finally, we update all hosts. +for host in hosts: + for interface in host.interface_set.all(): + print "Saving interface %s " % interface.ip4address + interface.ip4address.save()