alfred.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/python
  2. from __future__ import print_function
  3. from copy import deepcopy
  4. import io
  5. import json
  6. import socket
  7. import subprocess
  8. import time
  9. import StringIO
  10. class AlfredParser:
  11. alfred_json = 'alfred-json'
  12. alfred_datatypes = [ ('static', 158), ('dynamic', 159) ]
  13. def sanitycheck(self):
  14. testdata = None
  15. try:
  16. testdata = subprocess.check_output([self.alfred_json, '-z', '-r', str(int(self.alfred_datatypes[0][1]))])
  17. except Exception as err:
  18. raise Exception("alfred-json not found or incompatible: " + str(err))
  19. try:
  20. check = json.loads(testdata)
  21. except Exception as err:
  22. raise Exception("alfred-json does not return valid JSON data: " + str(err))
  23. return True
  24. def fetch(self, alfred_dump=None, include_rawdata=False):
  25. data = { }
  26. ts = int(time.time())
  27. alfreddata = { }
  28. for datatype in self.alfred_datatypes:
  29. rawdata = subprocess.check_output([self.alfred_json, '-z', '-r', str(int(datatype[1]))])
  30. newdata = json.loads(rawdata)
  31. for item in newdata:
  32. if not item in alfreddata:
  33. alfreddata[item] = { }
  34. alfreddata[item][datatype[0]] = newdata[item]
  35. if not alfred_dump is None:
  36. jsondata = json.dumps(alfreddata, ensure_ascii=False)
  37. f = io.open(alfred_dump, 'w')
  38. f.write(jsondata)
  39. f.close()
  40. for alfredid in alfreddata:
  41. alfredinfo = alfreddata[alfredid]
  42. myid = alfredinfo['static']['node_id'] if 'node_id' in alfredinfo['static'] else alfredid.lower().replace(':', '')
  43. nodeinfo = { 'hostname': None, 'mac': None, 'software': {}, 'statistics': {} }
  44. data[myid] = nodeinfo
  45. nodestatic = alfredinfo['static']
  46. if 'hostname' in nodestatic: nodeinfo['hostname'] = nodestatic['hostname']
  47. if 'network' in nodestatic and 'mac' in nodestatic['network']:
  48. nodeinfo['mac']= nodestatic['network']['mac']
  49. if 'software' in nodestatic:
  50. sw = nodestatic['software']
  51. nodeinfo['software']['firmware'] = sw['firmware']['release'] if 'firmware' in sw and 'release' in sw['firmware'] else None
  52. nodeinfo['software']['autoupdater'] = sw['autoupdater']['branch'] if sw['autoupdater']['enabled'] else 'off'
  53. nodedyn = alfredinfo['dynamic'] if 'dynamic' in alfredinfo else nodestatic['statistics']
  54. if 'uptime' in nodedyn: nodeinfo['uptime'] = int(float(nodedyn['uptime']))
  55. if 'gateway' in nodedyn: nodeinfo['gateway'] = nodedyn['gateway']
  56. traffic = nodedyn["traffic"] if "traffic" in nodedyn else None
  57. if not traffic is None:
  58. if not 'traffic' in nodeinfo['statistics']: nodeinfo['statistics']['traffic'] = { }
  59. t = nodeinfo['statistics']['traffic']
  60. t['rxbytes'] = int(traffic["rx"]["bytes"])
  61. t['txbytes'] = int(traffic["tx"]["bytes"])
  62. return data
  63. if __name__ == "__main__":
  64. a = AlfredParser()
  65. try:
  66. a.sanitycheck()
  67. except Exception as err:
  68. print('SANITY-CHECK failed:', str(err))
  69. import sys
  70. sys.exit(1)
  71. data = a.fetch()
  72. print(json.dumps(data))