12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #!/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 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 and 'mac' in nodestatic['network']:
- nodeinfo['mac']= nodestatic['network']['mac']
- 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))
|