summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomek Dubrownik <t.dubrownik@gmail.com>2012-02-17 11:52:20 +0100
committerTomek Dubrownik <t.dubrownik@gmail.com>2012-02-17 11:52:20 +0100
commit3e285b8efc1a80a341bcedfece5116af6e887a1b (patch)
tree262c016075f7306cc0ea437a351cdf0b4e3c2b34
parentf0394c0cecf057208234167639c470a07b192eb0 (diff)
downloadcheckinator-3e285b8efc1a80a341bcedfece5116af6e887a1b.tar.gz
checkinator-3e285b8efc1a80a341bcedfece5116af6e887a1b.tar.bz2
checkinator-3e285b8efc1a80a341bcedfece5116af6e887a1b.zip
dhcpd updater, take 1
-rw-r--r--at.py50
1 files changed, 42 insertions, 8 deletions
diff --git a/at.py b/at.py
index 52b77b4..e6590c0 100644
--- a/at.py
+++ b/at.py
@@ -10,7 +10,7 @@ from werkzeug.contrib.fixers import ProxyFix
from datetime import datetime
from wsgiref import simple_server
from pesto import Response, dispatcher_app
-from time import sleep, time
+from time import sleep, time, mktime
from collections import namedtuple
from urllib import urlencode
from hashlib import sha256
@@ -122,12 +122,13 @@ class CapUpdater(Updater):
traceback.format_exc(e))
sleep(10.0)
-class DnsmasqUpdater(Updater):
- def __init__(self, lease_file, lease_offset, *a, **kw):
+class MtimeUpdater(Updater):
+ def __init__(self, lease_file, *a, **kw):
self.lease_file = lease_file
- self.lease_offset = lease_offset
self.last_modified = 0
Updater.__init__(self, *a, **kw)
+ def file_changed(self, f):
+ pass
def run(self):
import os
while True:
@@ -136,15 +137,48 @@ class DnsmasqUpdater(Updater):
if mtime > self.last_modified:
app.logger.info('Lease file changed, updating')
with open(self.lease_file, 'r') as f:
- for line in f:
- ts, hwaddr, ip, name, client_id = line.split(' ')
- self.update(hwaddr, int(ts) - self.lease_offset, ip, name)
+ self.file_changed(f)
self.last_modified = mtime
sleep(3.0)
except Exception as e:
app.logger.error('Updater got an exception:\n' + \
traceback.format_exc(e))
sleep(10.0)
+
+class DnsmasqUpdater(MtimeUpdater):
+ def __init__(self, lease_file, lease_offset, *a, **kw):
+ self.lease_offset = lease_offset
+ MtimeUpdater.__init__(self, *a, **kw)
+ def file_changed(self, f):
+ for line in f:
+ ts, hwaddr, ip, name, client_id = line.split(' ')
+ self.update(hwaddr, int(ts) - self.lease_offset, ip, name)
+
+class DhcpdUpdater(MtimeUpdater):
+ def file_changed(self, f):
+ lease = False
+ for line in f:
+ line = line.split('#')[0]
+ cmd = line.strip().split()
+ if not cmd:
+ continue
+ if lease:
+ field = cmd[0]
+ if(field == 'starts'):
+ dt = datetime.strptime(' '.join(cmd[2:]), '%Y/%m/%d %H:%M:%S;')
+ atime = mktime(dt.utctimetuple())
+ if(field == 'client-hostname'):
+ name = cmd[1]
+ if(field == 'hardware'):
+ hwaddr = cmd[2]
+ if(field.startswith('}')):
+ lease = False
+ if hwaddr:
+ self.update(hwaddr, atime, ip, name)
+ elif cmd[0] == 'lease':
+ ip = cmd[1]
+ name, hwaddr, atime = [None] * 3
+ lease = True
@app.route('/')
def main_view():
@@ -308,6 +342,6 @@ port = 8080
if __name__ == '__main__':
import logging
app.logger.setLevel(logging.DEBUG)
- updater = DnsmasqUpdater(config.lease_file, config.lease_offset, config.timeout)
+ updater = DhcpdUpdater(config.lease_file, config.timeout)
updater.start()
app.run('0.0.0.0', config.port, debug=config.debug)