Mercurial > code > home > repos > homeauto
comparison service/dhcpleases/dhcpleases.py @ 1283:d36cd59b145a
dhcpleases partial rewrite
Ignore-this: 6a2b95612f0e3a2348397f2f068e5f58
darcs-hash:c0872b2f45c0c82492615f612e9655fdc48798c5
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sat, 20 Apr 2019 23:59:04 -0700 |
parents | 084f95932677 |
children | f372e9d358d2 |
comparison
equal
deleted
inserted
replaced
1282:61ce2830dea7 | 1283:d36cd59b145a |
---|---|
1 """ | 1 """ |
2 statements about dhcp leases (and maybe live-host pings) | 2 statements about dhcp leases (and maybe live-host pings) |
3 | |
4 also read 'arp -an' and our dns list | |
3 """ | 5 """ |
4 import sys | 6 import sys |
5 import datetime | 7 import datetime |
6 sys.path.append("/my/site/magma") | 8 sys.path.append("/my/site/magma") |
7 from stategraph import StateGraph | 9 from stategraph import StateGraph |
8 from rdflib import URIRef, Namespace, Literal, RDF, RDFS, XSD | 10 from rdflib import URIRef, Namespace, Literal, RDF, RDFS, XSD, ConjunctiveGraph |
9 from dateutil.tz import tzlocal | 11 from dateutil.tz import tzlocal |
10 import cyclone.web | 12 import cyclone.web |
11 from twisted.internet import reactor | 13 from twisted.internet import reactor, task |
12 from isc_dhcp_leases.iscdhcpleases import IscDhcpLeases | 14 from isc_dhcp_leases.iscdhcpleases import IscDhcpLeases |
15 sys.path.append("/my/proj/homeauto/lib") | |
16 from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler | |
17 sys.path.append("/my/proj/rdfdb") | |
18 from rdfdb.patch import Patch | |
13 | 19 |
14 DEV = Namespace("http://projects.bigasterisk.com/device/") | 20 DEV = Namespace("http://projects.bigasterisk.com/device/") |
15 ROOM = Namespace("http://projects.bigasterisk.com/room/") | 21 ROOM = Namespace("http://projects.bigasterisk.com/room/") |
16 | 22 |
17 def timeLiteral(dt): | 23 def timeLiteral(dt): |
18 return Literal(dt.replace(tzinfo=tzlocal()).isoformat(), | 24 return Literal(dt.replace(tzinfo=tzlocal()).isoformat(), |
19 datatype=XSD.dateTime) | 25 datatype=XSD.dateTime) |
20 | 26 |
21 class GraphHandler(cyclone.web.RequestHandler): | 27 def update(masterGraph): |
22 def get(self): | 28 g = ConjunctiveGraph() |
23 pruneExpired = bool(self.get_argument('pruneExpired', '')) | 29 ctx = DEV['dhcp'] |
24 g = StateGraph(ctx=DEV['dhcp']) | |
25 | 30 |
26 now = datetime.datetime.now() | 31 now = datetime.datetime.now() |
27 for mac, lease in IscDhcpLeases('/var/lib/dhcp/dhcpd.leases' | 32 for mac, lease in IscDhcpLeases('/var/lib/dhcp/dhcpd.leases' |
28 ).get_current().items(): | 33 ).get_current().items(): |
29 if pruneExpired and lease.end < now: | 34 uri = URIRef("http://bigasterisk.com/dhcpLease/%s" % lease.ethernet) |
30 continue | |
31 uri = URIRef("http://bigasterisk.com/dhcpLease/%s" % lease.ethernet) | |
32 | |
33 g.add((uri, RDF.type, ROOM['DhcpLease'])) | |
34 g.add((uri, ROOM['leaseStartTime'], timeLiteral(lease.start))) | |
35 g.add((uri, ROOM['leaseEndTime'], timeLiteral(lease.end))) | |
36 ip = URIRef("http://bigasterisk.com/localNet/%s/" % lease.ip) | |
37 g.add((uri, ROOM['assignedIp'], ip)) | |
38 g.add((ip, RDFS.label, Literal(lease.ip))) | |
39 mac = URIRef("http://bigasterisk.com/mac/%s" % lease.ethernet) | |
40 g.add((uri, ROOM['ethernetAddress'], mac)) | |
41 g.add((mac, ROOM['macAddress'], Literal(lease.ethernet))) | |
42 if lease.hostname: | |
43 g.add((mac, ROOM['dhcpHostname'], Literal(lease.hostname))) | |
44 | 35 |
45 self.set_header('Content-Type', 'application/x-trig') | 36 g.add((uri, RDF.type, ROOM['DhcpLease'], ctx)) |
46 self.write(g.asTrig()) | 37 g.add((uri, ROOM['leaseStartTime'], timeLiteral(lease.start), ctx)) |
38 g.add((uri, ROOM['leaseEndTime'], timeLiteral(lease.end), ctx)) | |
39 if lease.end < now: | |
40 g.add((uri, RDF.type, ROOM['ExpiredLease'], ctx)) | |
41 ip = URIRef("http://bigasterisk.com/localNet/%s/" % lease.ip) | |
42 g.add((uri, ROOM['assignedIp'], ip, ctx)) | |
43 g.add((ip, RDFS.label, Literal(lease.ip), ctx)) | |
44 mac = URIRef("http://bigasterisk.com/mac/%s" % lease.ethernet) | |
45 g.add((uri, ROOM['ethernetAddress'], mac, ctx)) | |
46 g.add((mac, ROOM['macAddress'], Literal(lease.ethernet), ctx)) | |
47 if lease.hostname: | |
48 g.add((mac, ROOM['dhcpHostname'], Literal(lease.hostname), ctx)) | |
49 masterGraph.setToGraph(g) | |
47 | 50 |
48 if __name__ == '__main__': | 51 if __name__ == '__main__': |
49 config = { | 52 config = { |
50 'servePort' : 9073, | 53 'servePort' : 9073, |
51 } | 54 } |
52 from twisted.python import log as twlog | 55 from twisted.python import log as twlog |
53 twlog.startLogging(sys.stdout) | 56 twlog.startLogging(sys.stdout) |
54 #log.setLevel(10) | 57 #log.setLevel(10) |
55 #log.setLevel(logging.DEBUG) | 58 #log.setLevel(logging.DEBUG) |
59 masterGraph = PatchableGraph() | |
60 task.LoopingCall(update, masterGraph).start(1) | |
56 | 61 |
57 reactor.listenTCP( | 62 reactor.listenTCP( |
58 config['servePort'], | 63 config['servePort'], |
59 cyclone.web.Application( | 64 cyclone.web.Application( |
60 [ | 65 [ |
61 (r"/()", cyclone.web.StaticFileHandler, | 66 (r"/()", cyclone.web.StaticFileHandler, |
62 {"path": ".", "default_filename": "index.html"}), | 67 {"path": ".", "default_filename": "index.html"}), |
63 | 68 |
64 (r'/graph', GraphHandler), | 69 (r'/graph', CycloneGraphHandler, {'masterGraph': masterGraph}), |
65 ], | 70 (r'/graph/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), |
71 ], masterGraph=masterGraph | |
66 )) | 72 )) |
67 reactor.run() | 73 reactor.run() |