2
0

ffstatus.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. import io
  4. import json
  5. import socket
  6. import subprocess
  7. import time
  8. import StringIO
  9. class AlfredParser:
  10. prefix = "ffpb.nodes."
  11. target_host = "fdca:ffee:ff12:a254::da7a"
  12. target_port = 2003
  13. alfred_dump = '/www/alfred.json'
  14. 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" ]
  15. def execute(self):
  16. rawdata = subprocess.check_output(['alfred-json', '-z', '-r', '158'])
  17. data = json.loads(rawdata)
  18. ts = int(time.time())
  19. if not self.alfred_dump is None:
  20. f = io.open(self.alfred_dump, 'w')
  21. f.write(unicode(rawdata))
  22. f.close()
  23. output = StringIO.StringIO()
  24. for nodeid in data:
  25. if (not self.whitelist is None) and (not nodeid in self.whitelist):
  26. #print("Skipping node {0} as it is not in the configured whitelist.".format(nodeid))
  27. continue
  28. nodeinfo = data[nodeid]
  29. nodestats = None
  30. if "statistics" in nodeinfo: nodestats = nodeinfo["statistics"]
  31. if not nodestats is None:
  32. print(self.prefix, nodeid, ".uptime", " ", int(float(nodestats["uptime"])), " ", ts, sep='', file=output)
  33. traffic = None
  34. if "traffic" in nodestats: traffic = nodestats["traffic"]
  35. if not traffic is None:
  36. print(self.prefix, nodeid, ".rxbytes", " ", int(traffic["rx"]["bytes"]), " ", ts, sep='', file=output)
  37. print(self.prefix, nodeid, ".rxpackets", " ", int(traffic["rx"]["packets"]), " ", ts, sep='', file=output)
  38. print(self.prefix, nodeid, ".txbytes", " ", int(traffic["tx"]["bytes"]), " ", ts, sep='', file=output)
  39. print(self.prefix, nodeid, ".txpackets", " ", int(traffic["tx"]["packets"]), " ", ts, sep='', file=output)
  40. else:
  41. print("Node {0} does not provide statistics information.".format(nodeid))
  42. s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
  43. s.connect((self.target_host, self.target_port))
  44. all_output = output.getvalue()
  45. print(all_output)
  46. s.sendall(all_output)
  47. s.shutdown(socket.SHUT_WR)
  48. s.close()
  49. output.close()
  50. if __name__ == "__main__":
  51. a = AlfredParser()
  52. a.execute()