123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #!/usr/bin/python
- from __future__ import print_function
- from copy import deepcopy
- import io
- import json
- import socket
- import subprocess
- import time
- import StringIO
- class AlfredParser:
- alfred_json = 'alfred-json'
- alfred_datatypes = [ ('static', 158), ('dynamic', 159) ]
- def __str__(self):
- return 'AlfredParser \'{0}\' {1}'.format(self.alfred_json, str.join(' ', [ '{1}=>{0}'.format(x[0], x[1]) for x in self.alfred_datatypes ]))
- def sanitycheck(self):
- testdata = None
- try:
- testdata = subprocess.check_output([self.alfred_json, '-z', '-r', str(int(self.alfred_datatypes[0][1]))])
- except Exception as err:
- raise Exception("alfred-json not found or incompatible: " + str(err))
- try:
- check = json.loads(testdata)
- except Exception as err:
- raise Exception("alfred-json does not return valid JSON data: " + str(err))
- return True
- def fetch(self, alfred_dump=None, include_rawdata=False):
- data = { }
- ts = int(time.time())
- alfreddata = { }
- for datatype in self.alfred_datatypes:
- rawdata = subprocess.check_output([self.alfred_json, '-z', '-r', str(int(datatype[1]))])
- newdata = json.loads(rawdata)
-
- for item in newdata:
- if not item in alfreddata:
- alfreddata[item] = { }
- alfreddata[item][datatype[0]] = newdata[item]
- if not alfred_dump is None:
- jsondata = json.dumps(alfreddata, ensure_ascii=False)
- f = io.open(alfred_dump, 'w')
- f.write(jsondata)
- f.close()
- for alfredid in alfreddata:
- alfredinfo = alfreddata[alfredid]
- myid = alfredinfo['static']['node_id'] if 'node_id' in alfredinfo['static'] else alfredid.lower().replace(':', '')
- nodeinfo = { 'hostname': None, 'mac': None, 'software': {}, 'statistics': {} }
- data[myid] = nodeinfo
- nodestatic = alfredinfo['static']
- if 'hostname' in nodestatic: nodeinfo['hostname'] = nodestatic['hostname']
- if 'network' in nodestatic:
- if 'mac' in nodestatic['network']:
- nodeinfo['mac'] = nodestatic['network']['mac']
- if 'mesh_interfaces' in nodestatic['network']:
- nodeinfo['macs'] = [ x for x in nodestatic['network']['mesh_interfaces'] ]
- else:
- nodeinfo['macs'] = []
- if 'software' in nodestatic:
- sw = nodestatic['software']
- nodeinfo['software']['firmware'] = sw['firmware']['release'] if 'firmware' in sw and 'release' in sw['firmware'] else None
- nodeinfo['software']['autoupdater'] = sw['autoupdater']['branch'] if sw['autoupdater']['enabled'] else 'off'
- nodedyn = alfredinfo['dynamic'] if 'dynamic' in alfredinfo else nodestatic['statistics']
- if 'uptime' in nodedyn: nodeinfo['uptime'] = int(float(nodedyn['uptime']))
- if 'gateway' in nodedyn: nodeinfo['gateway'] = nodedyn['gateway']
-
- traffic = nodedyn["traffic"] if "traffic" in nodedyn else None
- if not traffic is None:
- if not 'traffic' in nodeinfo['statistics']: nodeinfo['statistics']['traffic'] = { }
- t = nodeinfo['statistics']['traffic']
- t['rxbytes'] = int(traffic["rx"]["bytes"])
- t['txbytes'] = int(traffic["tx"]["bytes"])
- return data
- if __name__ == "__main__":
- a = AlfredParser()
- try:
- a.sanitycheck()
- except Exception as err:
- print('SANITY-CHECK failed:', str(err))
- import sys
- sys.exit(1)
- data = a.fetch()
- print(json.dumps(data))
|