alfred.py 3.2 KB

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