From 99a931c1997ba6c19550b1698b75153086528908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Martin=20Sl=C3=A5en?= Date: Sat, 4 Feb 2012 13:16:43 +0100 Subject: [PATCH] Created script which pings a specific host-type and writes to the ip4address object when it was last seen. Also made a change to the models in order to facilitate the logging. --- mdb/models.py | 1 + scripts/ping_service.py | 49 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 scripts/ping_service.py diff --git a/mdb/models.py b/mdb/models.py index ef43c6e..8e85b15 100644 --- a/mdb/models.py +++ b/mdb/models.py @@ -372,6 +372,7 @@ class DhcpCustomField(models.Model): class Ip4Address(models.Model): subnet = models.ForeignKey(Ip4Subnet) address = models.IPAddressField() + last_contact = models.DateTimeField() def __unicode__(self): if self.interface_set.count() == 0: diff --git a/scripts/ping_service.py b/scripts/ping_service.py new file mode 100755 index 0000000..675691c --- /dev/null +++ b/scripts/ping_service.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# coding: utf-8 + +import os,sys,datetime + +try: + host_type = sys.argv[1] +except: + print "USAGE: %s " % sys.argv[0] + sys.exit(1) + +from django.core.management import setup_environ +from django.core.mail import mail_admins +from dns_mdb import settings + +setup_environ(settings) + +from mdb.models import * + +debugging = False + + + +def do_ping(ipaddr): + if os.system("ping -c 1 -W 1 %s > /dev/null 2>&1" % ipaddr) == 0: + return 1 + else: + return None + +hosts = Host.objects.filter(host_type__host_type = host_type) + + +for host in hosts: + interfaces = host.interface_set.all() + + for interface in interfaces: + if do_ping(interface.ip4address.address): + interface.ip4address.last_contact = datetime.datetime.now() + interface.ip4address.save() + if debugging: + print "%s (%s) : success" % (host, interface.ip4address.address) + else: + if debugging: + print "%s (%s) : fail " % (host, interface.ip4address.address) + + + + +