alfred.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. import io
  5. import json
  6. import subprocess
  7. import time
  8. from .exceptions import SanityCheckError
  9. class AlfredParser:
  10. alfred_json = 'alfred-json'
  11. alfred_datatypes = [('static', 158), ('dynamic', 159)]
  12. def __str__(self):
  13. types = ['{1}=>{0}'.format(x[0], x[1]) for x in self.alfred_datatypes]
  14. return 'AlfredParser \'{0}\' {1}'.format(
  15. self.alfred_json,
  16. str.join(' ', types),
  17. )
  18. def sanitycheck(self):
  19. """
  20. Checks that the necessary tool 'alfred-json' is callable
  21. and outputs syntactically correct JSON data.
  22. """
  23. testdata = None
  24. try:
  25. first_datatype = int(self.alfred_datatypes[0][1])
  26. cmd = [self.alfred_json, '-z', '-r', str(first_datatype)]
  27. testdata = subprocess.check_output(cmd)
  28. except Exception as err:
  29. raise SanityCheckError(
  30. self, "alfred-json not found or incompatible", err)
  31. try:
  32. json.loads(testdata)
  33. except Exception as err:
  34. raise SanityCheckError(
  35. self, "alfred-json does not return valid JSON data", err)
  36. return True
  37. def fetch(self, alfred_dump=None):
  38. """Fetches the current data from alfred-json."""
  39. data = {}
  40. timestamp = int(time.time())
  41. alfreddata = {}
  42. for datatype in self.alfred_datatypes:
  43. # call 'alfred-json' and fetch the output
  44. cmd = [self.alfred_json, '-z', '-r', str(int(datatype[1]))]
  45. rawdata = subprocess.check_output(cmd)
  46. # convert raw input into unicode
  47. rawdata = unicode(rawdata, encoding='latin1', errors='replace')
  48. # parse it as JSON
  49. newdata = json.loads(rawdata)
  50. for item in newdata:
  51. if not item in alfreddata:
  52. alfreddata[item] = {}
  53. alfreddata[item][datatype[0]] = newdata[item]
  54. if alfred_dump is not None:
  55. jsondata = json.dumps(alfreddata, ensure_ascii=False)
  56. dumpfile = io.open(alfred_dump, 'w')
  57. dumpfile.write(jsondata)
  58. dumpfile.close()
  59. for alfredid in alfreddata:
  60. alfredinfo = alfreddata[alfredid]
  61. if 'node_id' in alfredinfo['static']:
  62. myid = alfredinfo['static']['node_id']
  63. else:
  64. myid = alfredid.lower().replace(':', '')
  65. nodeinfo = {
  66. 'hostname': None,
  67. 'mac': None,
  68. 'software': {},
  69. 'statistics': {},
  70. '__UPDATED__': {'alfred': timestamp},
  71. '__RAW__': {'alfred': alfredinfo},
  72. }
  73. data[myid] = nodeinfo
  74. nodestatic = alfredinfo['static']
  75. if 'hostname' in nodestatic:
  76. nodeinfo['hostname'] = nodestatic['hostname']
  77. if 'network' in nodestatic:
  78. if 'mac' in nodestatic['network']:
  79. nodeinfo['mac'] = nodestatic['network']['mac']
  80. if 'mesh_interfaces' in nodestatic['network']:
  81. macs = [x for x in nodestatic['network']['mesh_interfaces']]
  82. nodeinfo['macs'] = macs
  83. else:
  84. nodeinfo['macs'] = []
  85. if 'software' in nodestatic:
  86. software = nodestatic['software']
  87. nodeinfo['software']['firmware'] = \
  88. software.get('firmware', {}).get('release')
  89. autoupdater = software.get('autoupdater', {})
  90. autoupdater_enabled = autoupdater.get('enabled', False)
  91. nodeinfo['software']['autoupdater'] = \
  92. autoupdater.get('branch') if autoupdater_enabled else 'off'
  93. nodedyn = alfredinfo.get('dynamic',
  94. nodestatic.get('statistics', {}))
  95. if 'uptime' in nodedyn:
  96. nodeinfo['uptime'] = int(float(nodedyn['uptime']))
  97. if 'gateway' in nodedyn:
  98. nodeinfo['gateway'] = nodedyn['gateway']
  99. traffic = nodedyn["traffic"] if "traffic" in nodedyn else None
  100. if traffic is not None:
  101. if not 'traffic' in nodeinfo['statistics']:
  102. nodeinfo['statistics']['traffic'] = {}
  103. item = nodeinfo['statistics']['traffic']
  104. item['rxbytes'] = int(traffic["rx"]["bytes"])
  105. item['txbytes'] = int(traffic["tx"]["bytes"])
  106. if 'location' in nodestatic:
  107. nodeinfo['location'] = nodestatic['location']
  108. return data
  109. if __name__ == "__main__":
  110. parser = AlfredParser()
  111. try:
  112. parser.sanitycheck()
  113. except SanityCheckError as err:
  114. print('SANITY-CHECK failed:', str(err))
  115. import sys
  116. sys.exit(1)
  117. adata = parser.fetch()
  118. print(json.dumps(adata))