Initialer Commit
commit
cc3a50efb0
|
|
@ -0,0 +1,70 @@
|
|||
import argparse
|
||||
import logging
|
||||
import time
|
||||
import threading
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
import schedule
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class Measurement(threading.Thread):
|
||||
def __init__(self, url, log, start_date):
|
||||
threading.Thread.__init__(self)
|
||||
self.url = url
|
||||
self.log = log
|
||||
self.start_date = start_date
|
||||
|
||||
def run(self):
|
||||
url = self.url
|
||||
log = self.log
|
||||
try:
|
||||
response = requests.get(url, allow_redirects=False, timeout=256)
|
||||
log.info("{date},{status},{time},{url}".format(
|
||||
date=self.start_date,
|
||||
status=response.status_code,
|
||||
time=response.elapsed.total_seconds(),
|
||||
url=url
|
||||
))
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
log.info("{date},{status},{time},{url}".format(
|
||||
date=self.start_date,
|
||||
status="TIMEOUT > 256",
|
||||
time="-1",
|
||||
url=url
|
||||
))
|
||||
log.exception(e)
|
||||
|
||||
def run_measurements(urls):
|
||||
for url in args.urls:
|
||||
Measurement(url, log, str(datetime.now())).start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(format='%(asctime)s %(levelname)s %(name)s:%(message)s', level=logging.INFO, datefmt="%Y.%m.%d %H:%M:%S")
|
||||
|
||||
parser = argparse.ArgumentParser(description="Measure HTTP/HTTPS response time")
|
||||
parser.add_argument("--output", "-o", default="results.csv", help="Output file")
|
||||
parser.add_argument("--interval", "-i", default=30, help="Interval", type=int)
|
||||
parser.add_argument("urls", nargs="+", help="URLs to measure")
|
||||
args = parser.parse_args()
|
||||
|
||||
filehandler = logging.FileHandler(args.output)
|
||||
file_formatter = logging.Formatter("%(message)s")
|
||||
filehandler.setFormatter(file_formatter)
|
||||
log.addHandler(filehandler)
|
||||
|
||||
schedule.every(args.interval).seconds.do(lambda: run_measurements(args.urls))
|
||||
|
||||
run_measurements(args.urls)
|
||||
|
||||
while True:
|
||||
try:
|
||||
schedule.run_pending()
|
||||
time.sleep(args.interval/10)
|
||||
except Exception as e:
|
||||
log.exception(e)
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
requests==2.18.4
|
||||
schedule==0.5.0
|
||||
Loading…
Reference in New Issue