diff --git a/scripts/ping_service.py b/scripts/ping_service.py index 3985bde..6a95221 100755 --- a/scripts/ping_service.py +++ b/scripts/ping_service.py @@ -7,15 +7,15 @@ from commands import getstatusoutput parser = argparse.ArgumentParser(description = 'Ping hosts from mdb') parser.add_argument('-t', '--type', default='all',\ - help="Which type of host to operate on") + help="Which type of host to operate on") parser.add_argument('-d', '--debug', action='store_true',\ - help="Turn on debugging") + help="Turn on debugging") parser.add_argument('--show-types', dest='show_types', action='store_true',\ - help='Show available host types') + help='Show available host types') parser.add_argument('--num-threads', dest='num_threads', type=int, default=1,\ - help='Number of threads used for pinging hosts') + help='Number of threads used for pinging hosts') parser.add_argument('--num-pings', dest='num_pings', type=int, default=5,\ - help='Number of ping requests to each host. More requests gives a more accurate average calculation.') + help='Number of ping requests to each host. More requests gives a more accurate average calculation.') args = parser.parse_args() @@ -35,71 +35,71 @@ queue = Queue.Queue() class PingThread(threading.Thread): - """ Threaded ping """ - def __init__(self, host_queue): - threading.Thread.__init__(self) - self.queue = queue + """ Threaded ping """ + def __init__(self, host_queue): + threading.Thread.__init__(self) + self.queue = queue - def run(self): - while True: - host = self.queue.get() - self.do_ping_host(host) - self.queue.task_done() + def run(self): + while True: + host = self.queue.get() + self.do_ping_host(host) + self.queue.task_done() - def do_ping_host(self, host): - interfaces = host.interface_set.all() - for interface in interfaces: - self.do_ping_interface(interface, host) + def do_ping_host(self, host): + interfaces = host.interface_set.all() + for interface in interfaces: + self.do_ping_interface(interface, host) - def do_ping_interface(self, interface, host): - res = self.do_ping_ipaddr(interface.ip4address.address) - if res == None: - if args.debug: - print "%s (%s/%s) : fail " % (host, \ - interface.name, interface.ip4address.address) - return - interface.ip4address.last_contact = datetime.datetime.now() - interface.ip4address.ping_avg_rtt = float(res[1]) - interface.ip4address.save() - if args.debug: - print "%s (%s/%s) : %s" % (host, interface.name, \ - interface.ip4address.address, res) + def do_ping_interface(self, interface, host): + res = self.do_ping_ipaddr(interface.ip4address.address) + if res == None: + if args.debug: + print "%s (%s/%s) : fail " % (host, \ + interface.name, interface.ip4address.address) + return + interface.ip4address.last_contact = datetime.datetime.now() + interface.ip4address.ping_avg_rtt = float(res[1]) + interface.ip4address.save() + if args.debug: + print "%s (%s/%s) : %s" % (host, interface.name, \ + interface.ip4address.address, res) - def do_ping_ipaddr(self, ipaddr): - status, output = getstatusoutput(PING_CMD % (args.num_pings,ipaddr)) - if status == 0 and len(output) > 0: - return output.split("/") - else: - return None - + def do_ping_ipaddr(self, ipaddr): + status, output = getstatusoutput(PING_CMD % (args.num_pings,ipaddr)) + if status == 0 and len(output) > 0: + return output.split("/") + else: + return None + def show_host_types(): - types = HostType.objects.all() - for t in types: - print "%s (%s)" % (t.host_type, t.description) + types = HostType.objects.all() + for t in types: + print "%s (%s)" % (t.host_type, t.description) if args.show_types: - show_host_types() - sys.exit(0) + show_host_types() + sys.exit(0) if args.type == "all": - hosts = Host.objects.all() + hosts = Host.objects.all() else: - hosts = Host.objects.filter(host_type__host_type = args.type) + hosts = Host.objects.filter(host_type__host_type = args.type) # start num_threads threads for pinging for i in xrange(args.num_threads): - t = PingThread(queue) - t.setDaemon(True) - t.start() + t = PingThread(queue) + t.setDaemon(True) + t.start() if args.debug: - print "Pinging %d hosts using %d threads." % (len(hosts), args.num_threads) + print "Pinging %d hosts using %d threads." % (len(hosts), args.num_threads) # Add all hosts to the queue. Ping threads # will pick hosts from this queue. for host in hosts: - queue.put(host) + queue.put(host) # Wait for all threads to finish, eg. queue is empty. queue.join() diff --git a/scripts/rrd_graphgen.py b/scripts/rrd_graphgen.py index f424de0..f59b909 100755 --- a/scripts/rrd_graphgen.py +++ b/scripts/rrd_graphgen.py @@ -7,19 +7,19 @@ from commands import getstatusoutput parser = argparse.ArgumentParser(description = 'Ping hosts from mdb') parser.add_argument('-t', '--type', default='all',\ - help="Which type of host to operate on") + help="Which type of host to operate on") parser.add_argument('-d', '--debug', action='store_true',\ - help="Turn on debugging") + help="Turn on debugging") parser.add_argument('--show-types', dest='show_types', action='store_true',\ - help='Show available host types') + help='Show available host types') parser.add_argument('--num-threads', dest='num_threads', type=int, default=1,\ - help='Number of threads used for pinging hosts') + help='Number of threads used for pinging hosts') parser.add_argument('--num-pings', dest='num_pings', type=int, default=5,\ - help='Number of ping requests to each host. More requests gives a more accurate average calculation.') + help='Number of ping requests to each host. More requests gives a more accurate average calculation.') parser.add_argument('--rrd-path', dest='rrd_path', required=True,\ - help='Specify the path to the rrd files') + help='Specify the path to the rrd files') parser.add_argument('--graph-path', dest='graph_path', required=True,\ - help="Specify the output directory for the graphs") + help="Specify the output directory for the graphs") args = parser.parse_args() @@ -31,21 +31,21 @@ from mdb.models import * queue = Queue.Queue() -RRD_GRAPH = "%s " \ - "-w 785 -h 120 -a PNG " \ - "--slope-mode " \ - "--start -86400 --end now " \ - "--font DEFAULT:7: "\ - "--title \"%s\" " \ - "--watermark \"NEUF.NO\" " \ - "--vertical-label \"latency(ms)\" " \ - "--right-axis-label \"latency(ms)\" " \ - "--lower-limit 0 " \ - "--right-axis 1:0 " \ - "--x-grid MINUTE:10:HOUR:1:MINUTE:120:0:%%R " \ - "--alt-y-grid --rigid " \ - "DEF:roundtrip=%s:rrd:AVERAGE " \ - "LINE1:roundtrip#0000FF:\"latency(ms)\" " +RRD_GRAPH = "%s " \ + "-w 785 -h 120 -a PNG " \ + "--slope-mode " \ + "--start -86400 --end now " \ + "--font DEFAULT:7: "\ + "--title \"%s\" " \ + "--watermark \"NEUF.NO\" " \ + "--vertical-label \"latency(ms)\" " \ + "--right-axis-label \"latency(ms)\" " \ + "--lower-limit 0 " \ + "--right-axis 1:0 " \ + "--x-grid MINUTE:10:HOUR:1:MINUTE:120:0:%%R " \ + "--alt-y-grid --rigid " \ + "DEF:roundtrip=%s:rrd:AVERAGE " \ + "LINE1:roundtrip#0000FF:\"latency(ms)\" " # RRD_GRAPH ARGS: , , <rrd> @@ -54,101 +54,101 @@ RRD_GRAPH_DIR = args.graph_path RRDTOOL = "/usr/bin/rrdtool" class RrdService(): - def log(self, host, interface, avg_ping): - rrd = self.get_rrd(host, interface) - self.update_rrd(rrd, avg_ping) + def log(self, host, interface, avg_ping): + rrd = self.get_rrd(host, interface) + self.update_rrd(rrd, avg_ping) - def get_rrd(self, host, interface): - return "%s/%s_%s.rrd" % ( RRD_DIR, host.id,\ - interface.id) + def get_rrd(self, host, interface): + return "%s/%s_%s.rrd" % ( RRD_DIR, host.id,\ + interface.id) - def get_png(self, host, interface): - return "%s/%s_%s.png" % (RRD_GRAPH_DIR, host.id,\ - interface.id) + def get_png(self, host, interface): + return "%s/%s_%s.png" % (RRD_GRAPH_DIR, host.id,\ + interface.id) - def update_rrd(self, rrd, avg_ping): - self.create_rrd(rrd) - - self.rrd_exec("update", RRD_UPDATE % (rrd, avg_ping)) + def update_rrd(self, rrd, avg_ping): + self.create_rrd(rrd) + + self.rrd_exec("update", RRD_UPDATE % (rrd, avg_ping)) - def create_rrd(self, rrd): - if os.path.isfile(rrd): - return True - if args.debug: - print "Creating rrd: " + rrd - if not self.rrd_exec("create", RRD_CREATE % rrd): - if args.debug: - print "Could not create RRD database." - return False - return True + def create_rrd(self, rrd): + if os.path.isfile(rrd): + return True + if args.debug: + print "Creating rrd: " + rrd + if not self.rrd_exec("create", RRD_CREATE % rrd): + if args.debug: + print "Could not create RRD database." + return False + return True - def create_title(self, host, interface): -# return "%s (%s)" % (host.hostname, unicode(interface)) - return unicode(interface) + def create_title(self, host, interface): +# return "%s (%s)" % (host.hostname, unicode(interface)) + return unicode(interface) - def create_graph(self, host, interface): - rrd = self.get_rrd(host, interface) - if not os.path.isfile(rrd): return + def create_graph(self, host, interface): + rrd = self.get_rrd(host, interface) + if not os.path.isfile(rrd): return - self.rrd_exec("graph", RRD_GRAPH % (self.get_png(host, interface), \ - self.create_title(host, interface), rrd)) + self.rrd_exec("graph", RRD_GRAPH % (self.get_png(host, interface), \ + self.create_title(host, interface), rrd)) - def rrd_exec(self, function, rrd_cmd): - cmd = "%s %s %s" % (RRDTOOL, function, rrd_cmd) - if args.debug: - print "RRD_EXEC (%s)" % cmd - if os.system(cmd) != 0: - return False - else: - return True + def rrd_exec(self, function, rrd_cmd): + cmd = "%s %s %s" % (RRDTOOL, function, rrd_cmd) + if args.debug: + print "RRD_EXEC (%s)" % cmd + if os.system(cmd) != 0: + return False + else: + return True class WorkerThread(threading.Thread): - """ Threaded ping """ - def __init__(self, host_queue, rrd_service): - threading.Thread.__init__(self) - self.queue = queue - self.rrd_service = rrd_service + """ Threaded ping """ + def __init__(self, host_queue, rrd_service): + threading.Thread.__init__(self) + self.queue = queue + self.rrd_service = rrd_service - def run(self): - while True: - host = self.queue.get() - self.do_graph_host(host) - self.queue.task_done() + def run(self): + while True: + host = self.queue.get() + self.do_graph_host(host) + self.queue.task_done() - def do_graph_host(self, host): - interfaces = host.interface_set.all() - for interface in interfaces: - self.rrd_service.create_graph(host, interface) + def do_graph_host(self, host): + interfaces = host.interface_set.all() + for interface in interfaces: + self.rrd_service.create_graph(host, interface) def show_host_types(): - types = HostType.objects.all() - for t in types: - print "%s (%s)" % (t.host_type, t.description) + types = HostType.objects.all() + for t in types: + print "%s (%s)" % (t.host_type, t.description) if args.show_types: - show_host_types() - sys.exit(0) + show_host_types() + sys.exit(0) if args.type == "all": - hosts = Host.objects.all() + hosts = Host.objects.all() else: - hosts = Host.objects.filter(host_type__host_type = args.type) + hosts = Host.objects.filter(host_type__host_type = args.type) rrd_service = RrdService() # start num_threads threads for pinging for i in xrange(args.num_threads): - t = WorkerThread(queue, rrd_service) - t.setDaemon(True) - t.start() + t = WorkerThread(queue, rrd_service) + t.setDaemon(True) + t.start() if args.debug: - print "Pinging %d hosts using %d threads." % (len(hosts), args.num_threads) + print "Pinging %d hosts using %d threads." % (len(hosts), args.num_threads) # Add all hosts to the queue. Ping threads # will pick hosts from this queue. for host in hosts: - queue.put(host) + queue.put(host) # Wait for all threads to finish, eg. queue is empty. queue.join() diff --git a/scripts/rrd_service.py b/scripts/rrd_service.py index 3e9fe91..94f6fb8 100755 --- a/scripts/rrd_service.py +++ b/scripts/rrd_service.py @@ -7,17 +7,17 @@ from commands import getstatusoutput parser = argparse.ArgumentParser(description = 'Ping hosts from mdb') parser.add_argument('-t', '--type', default='all',\ - help="Which type of host to operate on") + help="Which type of host to operate on") parser.add_argument('-d', '--debug', action='store_true',\ - help="Turn on debugging") + help="Turn on debugging") parser.add_argument('--show-types', dest='show_types', action='store_true',\ - help='Show available host types') + help='Show available host types') parser.add_argument('--num-threads', dest='num_threads', type=int, default=1,\ - help='Number of threads used for pinging hosts') + help='Number of threads used for pinging hosts') parser.add_argument('--num-pings', dest='num_pings', type=int, default=5,\ - help='Number of ping requests to each host. More requests gives a more accurate average calculation.') + help='Number of ping requests to each host. More requests gives a more accurate average calculation.') parser.add_argument('--rrd-path', dest='rrd_path', required=True,\ - help='Specify the path to the rrd files') + help='Specify the path to the rrd files') args = parser.parse_args() @@ -33,11 +33,11 @@ queue = Queue.Queue() RRD_CREATE = "%s " \ - "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" + "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_UPDATE = "%s --template rtt N:%s" @@ -45,105 +45,105 @@ RRD_DIR = args.rrd_path RRDTOOL = "/usr/bin/rrdtool" class RrdService(): - def log(self, host, interface, avg_ping): - rrd = self.get_rrd(host, interface) - self.update_rrd(rrd, avg_ping) + def log(self, host, interface, avg_ping): + rrd = self.get_rrd(host, interface) + self.update_rrd(rrd, avg_ping) - def get_rrd(self, host, interface): - return "%s/%s_%s.rrd" % ( RRD_DIR, host.id,\ - interface.id) + def get_rrd(self, host, interface): + return "%s/%s_%s.rrd" % ( RRD_DIR, host.id,\ + interface.id) - def update_rrd(self, rrd, avg_ping): - self.create_rrd(rrd) - - self.rrd_exec("update", RRD_UPDATE % (rrd, avg_ping)) + def update_rrd(self, rrd, avg_ping): + self.create_rrd(rrd) + + self.rrd_exec("update", RRD_UPDATE % (rrd, avg_ping)) - def create_rrd(self, rrd): - if os.path.isfile(rrd): - return True - if args.debug: - print "Creating rrd: " + rrd - if not self.rrd_exec("create", RRD_CREATE % rrd): - if args.debug: - print "Could not create RRD database." - return False - return True - def rrd_exec(self, function, rrd_cmd): - cmd = "%s %s %s" % (RRDTOOL, function, rrd_cmd) - if args.debug: - print "RRD_EXEC (%s)" % cmd - if os.system(cmd) != 0: - return False - else: - return True + def create_rrd(self, rrd): + if os.path.isfile(rrd): + return True + if args.debug: + print "Creating rrd: " + rrd + if not self.rrd_exec("create", RRD_CREATE % rrd): + if args.debug: + print "Could not create RRD database." + return False + return True + def rrd_exec(self, function, rrd_cmd): + cmd = "%s %s %s" % (RRDTOOL, function, rrd_cmd) + if args.debug: + print "RRD_EXEC (%s)" % cmd + if os.system(cmd) != 0: + return False + else: + return True class PingThread(threading.Thread): - """ Threaded ping """ - def __init__(self, host_queue, rrd_service): - threading.Thread.__init__(self) - self.queue = queue - self.rrd_service = rrd_service + """ Threaded ping """ + def __init__(self, host_queue, rrd_service): + threading.Thread.__init__(self) + self.queue = queue + self.rrd_service = rrd_service - def run(self): - while True: - host = self.queue.get() - self.do_ping_host(host) - self.queue.task_done() + def run(self): + while True: + host = self.queue.get() + self.do_ping_host(host) + self.queue.task_done() - def do_ping_host(self, host): - interfaces = host.interface_set.all() - for interface in interfaces: - self.do_ping_interface(interface, host) + def do_ping_host(self, host): + interfaces = host.interface_set.all() + for interface in interfaces: + self.do_ping_interface(interface, host) - def do_ping_interface(self, interface, host): - res = self.do_ping_ipaddr(interface.ip4address.address) - if res == None: - if args.debug: - print "%s (%s/%s) : fail " % (host, \ - interface.name, interface.ip4address.address) - return - if args.debug: - print "%s (%s/%s) : %s" % (host, interface.name, \ - interface.ip4address.address, res) - self.rrd_service.log(host,interface, res[1]) + def do_ping_interface(self, interface, host): + res = self.do_ping_ipaddr(interface.ip4address.address) + if res == None: + if args.debug: + print "%s (%s/%s) : fail " % (host, \ + interface.name, interface.ip4address.address) + return + if args.debug: + print "%s (%s/%s) : %s" % (host, interface.name, \ + interface.ip4address.address, res) + self.rrd_service.log(host,interface, res[1]) - def do_ping_ipaddr(self, ipaddr): - status, output = getstatusoutput(PING_CMD % (args.num_pings,ipaddr)) - if status == 0 and len(output) > 0: - return output.split("/") - else: - return None - + def do_ping_ipaddr(self, ipaddr): + status, output = getstatusoutput(PING_CMD % (args.num_pings,ipaddr)) + if status == 0 and len(output) > 0: + return output.split("/") + else: + return None + def show_host_types(): - types = HostType.objects.all() - for t in types: - print "%s (%s)" % (t.host_type, t.description) + types = HostType.objects.all() + for t in types: + print "%s (%s)" % (t.host_type, t.description) if args.show_types: - show_host_types() - sys.exit(0) + show_host_types() + sys.exit(0) if args.type == "all": - hosts = Host.objects.all() + hosts = Host.objects.all() else: - hosts = Host.objects.filter(host_type__host_type = args.type) + hosts = Host.objects.filter(host_type__host_type = args.type) rrd_service = RrdService() # start num_threads threads for pinging for i in xrange(args.num_threads): - t = PingThread(queue, rrd_service) - t.setDaemon(True) - t.start() + t = PingThread(queue, rrd_service) + t.setDaemon(True) + t.start() if args.debug: - print "Pinging %d hosts using %d threads." % (len(hosts), args.num_threads) + print "Pinging %d hosts using %d threads." % (len(hosts), args.num_threads) # Add all hosts to the queue. Ping threads # will pick hosts from this queue. for host in hosts: - queue.put(host) + queue.put(host) # Wait for all threads to finish, eg. queue is empty. queue.join()