Mercurial > code > home > repos > homeauto
comparison service/dhcpleases/dhcpleases.py @ 966:cce0107a78b4
scan dhcpd.leases to get more info about networked devices
Ignore-this: d281707203bd85ede94402568b73c205
darcs-hash:20150322073510-312f9-507aab994fadabe3e1fb1bc98519c2d7d91e68ee
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 22 Mar 2015 00:35:10 -0700 |
parents | |
children | bb70eaa45666 |
comparison
equal
deleted
inserted
replaced
965:a06d9921c2a3 | 966:cce0107a78b4 |
---|---|
1 """ | |
2 statements about dhcp leases (and maybe live-host pings) | |
3 """ | |
4 import sys | |
5 sys.path.append("/my/site/magma") | |
6 from stategraph import StateGraph | |
7 from rdflib import URIRef, Namespace, Literal, RDF, RDFS, XSD | |
8 from dateutil.tz import tzlocal | |
9 import cyclone.web | |
10 from twisted.internet import reactor | |
11 from isc_dhcp_leases.iscdhcpleases import IscDhcpLeases | |
12 | |
13 | |
14 DEV = Namespace("http://projects.bigasterisk.com/device/") | |
15 ROOM = Namespace("http://projects.bigasterisk.com/room/") | |
16 | |
17 def timeLiteral(dt): | |
18 return Literal(dt.replace(tzinfo=tzlocal()).isoformat(), datatype=XSD.dateTime) | |
19 | |
20 class GraphHandler(cyclone.web.RequestHandler): | |
21 def get(self): | |
22 g = StateGraph(ctx=DEV['dhcp']) | |
23 | |
24 for mac, lease in IscDhcpLeases('/var/lib/dhcp/dhcpd.leases' | |
25 ).get_current().items(): | |
26 uri = URIRef("http://bigasterisk.com/dhcpLease/%s" % lease.ethernet) | |
27 | |
28 g.add((uri, RDF.type, ROOM['DhcpLease'])) | |
29 g.add((uri, ROOM['leaseStartTime'], timeLiteral(lease.start))) | |
30 g.add((uri, ROOM['leaseEndTime'], timeLiteral(lease.end))) | |
31 ip = URIRef("http://bigasterisk.com/localNet/%s/" % lease.ip) | |
32 g.add((uri, ROOM['assignedIp'], ip)) | |
33 g.add((ip, RDFS.label, Literal(lease.ip))) | |
34 mac = URIRef("http://bigasterisk.com/mac/%s" % lease.ethernet) | |
35 g.add((uri, ROOM['ethernetAddress'], mac)) | |
36 g.add((mac, ROOM['macAddress'], Literal(lease.ethernet))) | |
37 if lease.hostname: | |
38 g.add((mac, ROOM['dhcpHostname'], Literal(lease.hostname))) | |
39 | |
40 self.set_header('Content-type', 'application/x-trig') | |
41 self.write(g.asTrig()) | |
42 | |
43 if __name__ == '__main__': | |
44 config = { | |
45 'servePort' : 9073, | |
46 } | |
47 #from twisted.python import log as twlog | |
48 #twlog.startLogging(sys.stdout) | |
49 #log.setLevel(10) | |
50 #log.setLevel(logging.DEBUG) | |
51 | |
52 reactor.listenTCP(config['servePort'], | |
53 cyclone.web.Application( | |
54 [ | |
55 (r'/graph', GraphHandler), | |
56 ], | |
57 )) | |
58 reactor.run() |