from datetime import datetime
from subprocess import Popen, PIPE
from time import sleep
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from hostmonitor.models import Host

FPING = 'fping'

def ping_hosts(hosts):
    args = [FPING] + hosts
    print " ".join(args)
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    (fping_out, fping_err) = p.communicate()
    out_lines = fping_out.split("\n")
    err_lines = fping_err.split("\n")
    up = [x.split(" ",1)[0] for x in out_lines if ' is alive' in x]
    down = [x.split(" ",1)[0] for x in out_lines if ' is unreachable' in x]
    #down += [x.split(" ",1)[0] for x in err_lines]
    return (set(up), set(down))

class Command(BaseCommand):
    args = ''
    help = 'Add the specified hosts or CIDR networks (not network/broadcast)'
    option_list = BaseCommand.option_list + (
        make_option('--loop', action="store_true", dest="loop"),
        )

    def handle(self, *args, **options):
        if len(args) > 0:
            self.stderr.write("This command does not take arguments\n")
            return
        while True:
            self.ping_once()
            if not options['loop']:
                break
            sleep(10)

    def ping_once(self):
        hosts = []
        for host in Host.objects.filter(monitor=True):
            hosts.append(host.ip)
        print hosts
        self.stdout.write("Pinging all monitored hosts\n")
        (up, down) = ping_hosts(hosts)
        for ip in up:
            self.stdout.write("%s up\n" % ip)
            h = Host.objects.get(ip=ip)
            h.up = True
            h.last_up = datetime.now()
            if not h.up_since:
                h.up_since = datetime.now()
            h.save()
        for ip in down:
            self.stdout.write("%s down\n" % ip)
            h = Host.objects.get(ip=ip)
            h.up = False
            h.up_since = None
            h.save()

@@ -113,16 +113,30 @@
 e_option
+%0Aimport socket
 %0A%0Afrom d
@@ -304,33 +304,8 @@
 sts%0A
-    print %22 %22.join(args)%0A
     
@@ -708,16 +708,272 @@
 down))%0A%0A
+def dns_lookup(hosts):%0A    if hasattr(socket, 'setdefaulttimeout'):%0A        # Set the default timeout on sockets to 5 seconds%0A        socket.setdefaulttimeout(5)%0A    names = %5Bsocket.gethostbyaddr(ip)%5B0%5D for ip in hosts%5D%0A    return dict(zip(hosts, names))%0A%0A
 class Co
@@ -1630,28 +1630,8 @@
 ip)%0A
-        print hosts%0A
     
@@ -1724,16 +1724,101 @@
 (hosts)%0A
+        self.stdout.write(%22Resolving DNS names%5Cn%22)%0A        names = dns_lookup(hosts)%0A
         
@@ -1909,32 +1909,63 @@
 ects.get(ip=ip)%0A
+            h.name = names%5Bip%5D%0A
             h.up
@@ -2171,32 +2171,32 @@
 s down%5Cn%22 %25 ip)%0A
-
             h = 
@@ -2211,32 +2211,63 @@
 ects.get(ip=ip)%0A
+            h.name = names%5Bip%5D%0A
             h.up

Implement DNS lookups