dashing.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from .basestorage import BaseStorage
  4. import json
  5. import logging
  6. import requests
  7. class DashingClient:
  8. base_url = None
  9. def __init__(self, base_url, auth_token):
  10. self.base_url = base_url
  11. self.auth_token = auth_token
  12. self.logger = logging.getLogger('dashing')
  13. def __str__(self):
  14. return 'Dashing at \'{0}\''.format(self.base_url)
  15. def send(self, metric, current, previous=None):
  16. info = {
  17. 'auth_token': self.auth_token,
  18. 'current': int(current),
  19. }
  20. if previous is not None:
  21. info['previous'] = previous
  22. url = self.base_url + metric
  23. resp = requests.post(url, data=json.dumps(info)).read()
  24. self.logger.debug('Sent metric "{0}" = "{1}"'.format(metric, current))
  25. return resp
  26. def push(self, storage):
  27. if not isinstance(storage, BaseStorage):
  28. self.logger.error('Mimimi, storage is not a BaseStorage.')
  29. return
  30. status = storage.status
  31. # {akt,max}{Knoten,Client}count
  32. self.send('aktKnotencount', status['nodes_active'])
  33. self.send('aktClientcount', status['clients_unique'])
  34. # aktTraffic (mit Historie ?!)
  35. # evtl. Liste der letzten Knoten, die dazukamen
  36. if __name__ == "__main__":
  37. client = DashingClient(
  38. 'http://dashing.krombel.de:3030/widgets/',
  39. 'bitnhmlj47hamrftxkiug')
  40. client.send('testNumber', 42)