Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import logging
  4. import argparse
  5. import json
  6. import time
  7. from prometheus_client import start_http_server, Summary, Gauge, Counter
  8. from prometheus_client.core import GaugeMetricFamily
  9.  
  10. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  11. level=logging.DEBUG)
  12. logger = logging.getLogger(__name__)
  13.  
  14.  
  15. parser = argparse.ArgumentParser(description='Script is monitor network status')
  16.  
  17. parser.add_argument("-i","--destination", dest="hostname", type=str,default="localhost",help="Ping destination")
  18. parser.add_argument("-p","--parametres", dest="parametres", type=str,default="-c",help="Ping parametres")
  19. parser.add_argument("-c","--count", dest="count", type=str,default="1",help="Ping parametres")
  20.  
  21. args = parser.parse_args()
  22.  
  23. hostname = args.hostname
  24. parametres = args.parametres
  25. count = args.count
  26.  
  27. if "EXTERNAL_URL" in os.environ :
  28. # export EXTERNAL_URL='["google.com","localhost"]'
  29. logging.debug("Variable exist")
  30. hostname=json.loads(os.environ["EXTERNAL_URL"])
  31. else:
  32. logging.debug("Variable not exist")
  33. hostname=[hostname]
  34.  
  35. # Decorate function with metric.
  36.  
  37. def ping_time( parametres, count, hostname):
  38. for ip in hostname:
  39. out = subprocess.check_output("ping " + parametres + " " + count + " " + ip + " | grep -oP '.*time=\\K\\d+'", shell=True)
  40. output = out.decode()
  41. logging.debug("Host + {i} + Time + {o}".format(i=ip, o=output))
  42. result = output
  43. return result
  44.  
  45. network_jitter = Gauge('ping_time', 'Network jitter to hostname', ["destination"])
  46. print(network_jitter)
  47.  
  48. if __name__ == '__main__':
  49. start_http_server(8000)
  50. while True:
  51. network_jitter.labels(hostname).set(ping_time(parametres, count, hostname))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement