2
0

alfred.py 3.2 KB

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