Mercurial > code > home > repos > homeauto
changeset 480:53ceedcc2809
dhcpleases partial rewrite
Ignore-this: 6a2b95612f0e3a2348397f2f068e5f58
author | drewp@bigasterisk.com |
---|---|
date | Sat, 20 Apr 2019 23:59:04 -0700 |
parents | 9ab114450284 |
children | f08a0ef88adc |
files | service/dhcpleases/dhcpleases.py |
diffstat | 1 files changed, 35 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/service/dhcpleases/dhcpleases.py Sat Apr 20 23:57:47 2019 -0700 +++ b/service/dhcpleases/dhcpleases.py Sat Apr 20 23:59:04 2019 -0700 @@ -1,15 +1,21 @@ """ statements about dhcp leases (and maybe live-host pings) + +also read 'arp -an' and our dns list """ import sys import datetime sys.path.append("/my/site/magma") from stategraph import StateGraph -from rdflib import URIRef, Namespace, Literal, RDF, RDFS, XSD +from rdflib import URIRef, Namespace, Literal, RDF, RDFS, XSD, ConjunctiveGraph from dateutil.tz import tzlocal import cyclone.web -from twisted.internet import reactor +from twisted.internet import reactor, task from isc_dhcp_leases.iscdhcpleases import IscDhcpLeases +sys.path.append("/my/proj/homeauto/lib") +from patchablegraph import PatchableGraph, CycloneGraphEventsHandler, CycloneGraphHandler +sys.path.append("/my/proj/rdfdb") +from rdfdb.patch import Patch DEV = Namespace("http://projects.bigasterisk.com/device/") ROOM = Namespace("http://projects.bigasterisk.com/room/") @@ -18,32 +24,29 @@ return Literal(dt.replace(tzinfo=tzlocal()).isoformat(), datatype=XSD.dateTime) -class GraphHandler(cyclone.web.RequestHandler): - def get(self): - pruneExpired = bool(self.get_argument('pruneExpired', '')) - g = StateGraph(ctx=DEV['dhcp']) +def update(masterGraph): + g = ConjunctiveGraph() + ctx = DEV['dhcp'] + + now = datetime.datetime.now() + for mac, lease in IscDhcpLeases('/var/lib/dhcp/dhcpd.leases' + ).get_current().items(): + uri = URIRef("http://bigasterisk.com/dhcpLease/%s" % lease.ethernet) - now = datetime.datetime.now() - for mac, lease in IscDhcpLeases('/var/lib/dhcp/dhcpd.leases' - ).get_current().items(): - if pruneExpired and lease.end < now: - continue - uri = URIRef("http://bigasterisk.com/dhcpLease/%s" % lease.ethernet) - - g.add((uri, RDF.type, ROOM['DhcpLease'])) - g.add((uri, ROOM['leaseStartTime'], timeLiteral(lease.start))) - g.add((uri, ROOM['leaseEndTime'], timeLiteral(lease.end))) - ip = URIRef("http://bigasterisk.com/localNet/%s/" % lease.ip) - g.add((uri, ROOM['assignedIp'], ip)) - g.add((ip, RDFS.label, Literal(lease.ip))) - mac = URIRef("http://bigasterisk.com/mac/%s" % lease.ethernet) - g.add((uri, ROOM['ethernetAddress'], mac)) - g.add((mac, ROOM['macAddress'], Literal(lease.ethernet))) - if lease.hostname: - g.add((mac, ROOM['dhcpHostname'], Literal(lease.hostname))) - - self.set_header('Content-Type', 'application/x-trig') - self.write(g.asTrig()) + g.add((uri, RDF.type, ROOM['DhcpLease'], ctx)) + g.add((uri, ROOM['leaseStartTime'], timeLiteral(lease.start), ctx)) + g.add((uri, ROOM['leaseEndTime'], timeLiteral(lease.end), ctx)) + if lease.end < now: + g.add((uri, RDF.type, ROOM['ExpiredLease'], ctx)) + ip = URIRef("http://bigasterisk.com/localNet/%s/" % lease.ip) + g.add((uri, ROOM['assignedIp'], ip, ctx)) + g.add((ip, RDFS.label, Literal(lease.ip), ctx)) + mac = URIRef("http://bigasterisk.com/mac/%s" % lease.ethernet) + g.add((uri, ROOM['ethernetAddress'], mac, ctx)) + g.add((mac, ROOM['macAddress'], Literal(lease.ethernet), ctx)) + if lease.hostname: + g.add((mac, ROOM['dhcpHostname'], Literal(lease.hostname), ctx)) + masterGraph.setToGraph(g) if __name__ == '__main__': config = { @@ -53,6 +56,8 @@ twlog.startLogging(sys.stdout) #log.setLevel(10) #log.setLevel(logging.DEBUG) + masterGraph = PatchableGraph() + task.LoopingCall(update, masterGraph).start(1) reactor.listenTCP( config['servePort'], @@ -61,7 +66,8 @@ (r"/()", cyclone.web.StaticFileHandler, {"path": ".", "default_filename": "index.html"}), - (r'/graph', GraphHandler), - ], + (r'/graph', CycloneGraphHandler, {'masterGraph': masterGraph}), + (r'/graph/events', CycloneGraphEventsHandler, {'masterGraph': masterGraph}), + ], masterGraph=masterGraph )) reactor.run()