pvl/verkko/hosts.py
changeset 178 f9f5e669bace
parent 158 3ff66d4f401c
child 179 706972d09f05
equal deleted inserted replaced
177:b21b2efe1e6c 178:f9f5e669bace
     1 from pvl.verkko import db, web
     1 from pvl.verkko import web, db
     2 from pvl.verkko.utils import parse_timedelta, IPv4Network
     2 from pvl.verkko.utils import parse_timedelta, IPv4Network
     3 
     3 
     4 from pvl.web import html
     4 from pvl.web import html
     5 
     5 
     6 import re
     6 import re
     8 import socket # dns
     8 import socket # dns
     9 import math
     9 import math
    10 
    10 
    11 import logging; log = logging.getLogger('pvl.verkko.hosts')
    11 import logging; log = logging.getLogger('pvl.verkko.hosts')
    12 
    12 
    13 # XXX: this should actually be DHCPHost
    13 ## Model
       
    14 import json
       
    15 import time
       
    16 
       
    17 def dt2ts (dt) :
       
    18     return int(time.mktime(dt.timetuple()))
       
    19 
       
    20 def ts2dt (ts) :
       
    21     return datetime.datetime.fromtimestamp(ts)
       
    22 
       
    23 # TODO: this should be DHCPHost
    14 class Host (object) :
    24 class Host (object) :
    15     DATE_FMT = '%Y%m%d'
    25     DATE_FMT = '%Y%m%d'
    16     TIME_FMT = '%H:%M:%S'
    26     TIME_FMT = '%H:%M:%S'
    17     
    27     
    18     MAC_HEX = r'([A-Za-z0-9]{2})'
    28     MAC_HEX = r'([A-Za-z0-9]{2})'
   127 db.mapper(Host, db.dhcp_hosts, properties=dict(
   137 db.mapper(Host, db.dhcp_hosts, properties=dict(
   128     #id      = db.dhcp_hosts.c.rowid,
   138     #id      = db.dhcp_hosts.c.rowid,
   129     #state   = db.dhcp_hosts.c.,
   139     #state   = db.dhcp_hosts.c.,
   130 ))
   140 ))
   131 
   141 
   132 
   142 ## Controller 
   133    
   143 class BaseHandler (web.DatabaseHandler) :
   134  
   144     """
   135 class BaseHandler (web.Handler) :
   145         Common controller stuff for DHCP hosts
       
   146     """
       
   147 
       
   148     CSS = (
       
   149         "/static/dhcp/hosts.css", 
       
   150     )
       
   151     JS = (
       
   152         #"/static/jquery/jquery.js"
       
   153     )
       
   154 
   136     HOST_ATTRS = {
   155     HOST_ATTRS = {
   137         'id':       Host.id,
   156         'id':       Host.id,
   138         'net':      Host.gw,
   157         'net':      Host.gw,
   139         'ip':       Host.ip,
   158         'ip':       Host.ip,
   140         'mac':      Host.mac,
   159         'mac':      Host.mac,
   406                 html.input(type='hidden', name='sort', value=self.sorts),
   425                 html.input(type='hidden', name='sort', value=self.sorts),
   407                 table,
   426                 table,
   408             )
   427             )
   409 
   428 
   410 class ItemHandler (BaseHandler) :
   429 class ItemHandler (BaseHandler) :
       
   430     """
       
   431         A specific DHCP host, along with a list of related hosts.
       
   432     """
       
   433 
   411     def process (self, id) :
   434     def process (self, id) :
   412         self.hosts = self.query()
   435         self.hosts = self.query()
   413         self.host = self.hosts.get(id)
   436         self.host = self.hosts.get(id)
   414         
   437         
   415         if not self.host :
   438         if not self.host :
   454             html.a(href=self.url(ListHandler))(html('«'), 'Back'),
   477             html.a(href=self.url(ListHandler))(html('«'), 'Back'),
   455         )
   478         )
   456 
   479 
   457 
   480 
   458 class ListHandler (BaseHandler) :
   481 class ListHandler (BaseHandler) :
       
   482     """
       
   483         List of DHCP hosts for given filter.
       
   484     """
       
   485 
   459     # pagination
   486     # pagination
   460     PAGE = 10
   487     PAGE = 10
   461 
       
   462     # views
       
   463     VIEWS = (
       
   464         ("Last hour",   dict(seen='1h')),
       
   465         ("Last day",    dict(seen='24h')),
       
   466         ("All",         dict()),
       
   467     ) + tuple(
       
   468         ("Network " + network,          dict(ip=network)) for network in (
       
   469             '194.197.235.0/24',
       
   470             '10.1.0.0/16',
       
   471             '10.4.0.0/16',
       
   472             '10.5.0.0/16',
       
   473             '10.6.0.0/16',
       
   474             '10.10.0.0/16',
       
   475         )
       
   476     ) + (
       
   477         ("Valid",       dict(state=('DHCPACK', 'DHCPRELEASE'))),
       
   478         ("Incomplete",  dict(state=('DHCPDISCOVER', 'DHCPOFFER', 'DHCPREQUEST'))),
       
   479         ("Invalid",     dict(state=('DHCPNAK', ))),
       
   480     )
       
   481 
   488 
   482     def process (self) :
   489     def process (self) :
   483         hosts = self.query()
   490         hosts = self.query()
   484 
   491 
   485         # filter
   492         # filter
   509             self.render_hosts(self.hosts, filters=self.filters, page=self.page),
   516             self.render_hosts(self.hosts, filters=self.filters, page=self.page),
   510 
   517 
   511             html.a(href=self.url())(html('«'), 'Back') if self.filters else None,
   518             html.a(href=self.url())(html('«'), 'Back') if self.filters else None,
   512         )
   519         )
   513 
   520 
   514 import json
       
   515 import time
       
   516 
       
   517 def dt2ts (dt) :
       
   518     return int(time.mktime(dt.timetuple()))
       
   519 
       
   520 def ts2dt (ts) :
       
   521     return datetime.datetime.fromtimestamp(ts)
       
   522 
       
   523 class RealtimeHandler (BaseHandler) :
   521 class RealtimeHandler (BaseHandler) :
   524     TITLE = "Pseudo-Realtime hosts.."
   522     TITLE = "DHCP Pseudo-Realtime hosts.."
   525     CSS = web.Handler.CSS + (
   523     CSS = BaseHandler.CSS + (
   526         'http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css',
   524         'http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css',
   527     )
   525     )
   528     JS = (
   526     JS = (
   529         #"/static/jquery/jquery.js",
   527         #"/static/jquery/jquery.js",
   530         'http://code.jquery.com/jquery-1.8.2.js',
   528         'http://code.jquery.com/jquery-1.8.2.js',