Advertisement
Guest User

Untitled

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