12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #!/usr/bin/python
- from __future__ import print_function
- import io
- import json
- import socket
- import subprocess
- import time
- import StringIO
- class AlfredParser:
- prefix = "ffpb.nodes."
- target_host = "fdca:ffee:ff12:a254::da7a"
- target_port = 2003
- alfred_dump = '/www/alfred.json'
- whitelist = [ "24:a4:3c:f8:5e:fa", "24:a4:3c:f8:5e:db", "24:a4:3c:d9:4f:69", "24:a4:3c:a3:67:f0", "24:a4:3c:a3:68:07", "24:a4:3c:d2:21:d5" ]
- def execute(self):
- rawdata = subprocess.check_output(['alfred-json', '-z', '-r', '158'])
- data = json.loads(rawdata)
- ts = int(time.time())
- if not self.alfred_dump is None:
- f = io.open(self.alfred_dump, 'w')
- f.write(unicode(rawdata))
- f.close()
- output = StringIO.StringIO()
- for nodeid in data:
- if (not self.whitelist is None) and (not nodeid in self.whitelist):
- #print("Skipping node {0} as it is not in the configured whitelist.".format(nodeid))
- continue
- nodeinfo = data[nodeid]
- nodestats = None
- if "statistics" in nodeinfo: nodestats = nodeinfo["statistics"]
- if not nodestats is None:
- print(self.prefix, nodeid, ".uptime", " ", int(float(nodestats["uptime"])), " ", ts, sep='', file=output)
- traffic = None
- if "traffic" in nodestats: traffic = nodestats["traffic"]
- if not traffic is None:
- print(self.prefix, nodeid, ".rxbytes", " ", int(traffic["rx"]["bytes"]), " ", ts, sep='', file=output)
- print(self.prefix, nodeid, ".rxpackets", " ", int(traffic["rx"]["packets"]), " ", ts, sep='', file=output)
- print(self.prefix, nodeid, ".txbytes", " ", int(traffic["tx"]["bytes"]), " ", ts, sep='', file=output)
- print(self.prefix, nodeid, ".txpackets", " ", int(traffic["tx"]["packets"]), " ", ts, sep='', file=output)
- else:
- print("Node {0} does not provide statistics information.".format(nodeid))
- s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
- s.connect((self.target_host, self.target_port))
- all_output = output.getvalue()
- print(all_output)
- s.sendall(all_output)
- s.shutdown(socket.SHUT_WR)
- s.close()
- output.close()
- if __name__ == "__main__":
- a = AlfredParser()
- a.execute()
|