dashing.py 1.3 KB

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