2
0

alfred.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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:
  48. if 'mac' in nodestatic['network']:
  49. nodeinfo['mac'] = nodestatic['network']['mac']
  50. if 'mesh_interfaces' in nodestatic['network']:
  51. nodeinfo['macs'] = [ x for x in nodestatic['network']['mesh_interfaces'] ]
  52. else:
  53. nodeinfo['macs'] = []
  54. if 'software' in nodestatic:
  55. sw = nodestatic['software']
  56. nodeinfo['software']['firmware'] = sw['firmware']['release'] if 'firmware' in sw and 'release' in sw['firmware'] else None
  57. nodeinfo['software']['autoupdater'] = sw['autoupdater']['branch'] if sw['autoupdater']['enabled'] else 'off'
  58. nodedyn = alfredinfo['dynamic'] if 'dynamic' in alfredinfo else nodestatic['statistics']
  59. if 'uptime' in nodedyn: nodeinfo['uptime'] = int(float(nodedyn['uptime']))
  60. if 'gateway' in nodedyn: nodeinfo['gateway'] = nodedyn['gateway']
  61. traffic = nodedyn["traffic"] if "traffic" in nodedyn else None
  62. if not traffic is None:
  63. if not 'traffic' in nodeinfo['statistics']: nodeinfo['statistics']['traffic'] = { }
  64. t = nodeinfo['statistics']['traffic']
  65. t['rxbytes'] = int(traffic["rx"]["bytes"])
  66. t['txbytes'] = int(traffic["tx"]["bytes"])
  67. return data
  68. if __name__ == "__main__":
  69. a = AlfredParser()
  70. try:
  71. a.sanitycheck()
  72. except Exception as err:
  73. print('SANITY-CHECK failed:', str(err))
  74. import sys
  75. sys.exit(1)
  76. data = a.fetch()
  77. print(json.dumps(data))