Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import logging
- import argparse
- import json
- import time
- from prometheus_client import start_http_server, Summary, Gauge, Counter
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
- level=logging.INFO)
- logger = logging.getLogger(__name__)
- parser = argparse.ArgumentParser(description='Script is monitor network status')
- parser.add_argument("-i","--destination", dest="hostname", type=str,default="localhost",help="Ping destination")
- parser.add_argument("-p","--parametres", dest="parametres", type=str,default="-c",help="Ping parametres")
- parser.add_argument("-c","--count", dest="count", type=str,default="1",help="Ping parametres")
- args = parser.parse_args()
- hostname = args.hostname
- parametres = args.parametres
- count = args.count
- if "EXTERNAL_URL" in os.environ :
- # export EXTERNAL_URL='["google.com","localhost"]'
- logging.debug("Variable exist")
- hostname=json.loads(os.environ["EXTERNAL_URL"])
- else:
- logging.debug("Variable not exist")
- hostname=[hostname]
- # Decorate function with metric.
- def ping_time(parametres, count, hostname):
- for ip in hostname:
- out = subprocess.check_output("ping " + parametres + " " + count + " " + ip + " | grep -oP '.*time=\\K\\d+'", shell=True)
- output = out.decode()
- logging.debug("Host + {i} + Time + {o}".format(i=ip, o=output))
- result = output
- return result
- ## Вот так работает
- # network_jitter = Gauge('ping_time', 'Network jitter to hostname')
- ## Вот так не работает
- # network_jitter = Gauge('ping_time', 'Network jitter to hostname', ['destination'])
- # network_jitter.labels(destination='localhost')
- if __name__ == '__main__':
- start_http_server(8000)
- while True:
- network_jitter.set(ping_time(parametres, count, hostname))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement