From 2aaec150178e39630fa097249f1354aad240ad89 Mon Sep 17 00:00:00 2001 From: Clemens Klug Date: Mon, 23 Apr 2018 15:45:42 +0200 Subject: [PATCH 1/3] fix docker-compose --- docker-compose.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 59b286a..5692630 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,9 +2,6 @@ version: "2" services: influxdb: image: influxdb:1.5-alpine -# command: influxd -config /etc/influxdb/influxdb.conf -# ports: -# - "8086:8086" restart: on-failure:5 volumes: - ./data/influx:/var/lib/influxdb/ @@ -19,7 +16,7 @@ services: - "GF_SECURITY_ADMIN_USER=root" - "GF_SECURITY_ADMIN_PASSWORD=clkl" restart: on-failure:5 - networks:exhub.uni-bamberg.de + networks: - traefik_net - default labels: From 8361023a3ef788d8f9e06037d308cf98db923b40 Mon Sep 17 00:00:00 2001 From: Clemens Klug Date: Wed, 2 May 2018 14:56:14 +0200 Subject: [PATCH 2/3] add support for station tag --- src/measure.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/measure.py b/src/measure.py index 33f3d02..0bdf8c3 100644 --- a/src/measure.py +++ b/src/measure.py @@ -22,24 +22,26 @@ def influx_escape(string): string = string.replace(c, "\\"+c) return string -def send_data(date, status, time, url, protocol): - data = "response_time,url={url},status={status},protocol={protocol} value={time} {timestamp}".format( +def send_data(date, status, time, url, protocol, station=00): + data = "response_time,url={url},status={status},protocol={protocol},station={station} value={time} {timestamp}".format( url=influx_escape(url), status=influx_escape(str(status)), time=time, timestamp=int(date.timestamp()*1e9), - protocol=influx_escape(protocol) + protocol=influx_escape(protocol), + station=station ) r=requests.post(INFLUX_URL, data=data) log.warn(str(r) + " " + str(r.text)) class Measurement(threading.Thread): - def __init__(self, measure_fn, url, exlog): + def __init__(self, measure_fn, url, exlog, station=00): threading.Thread.__init__(self) self.url = url self.measure_fn = measure_fn self.exlog = exlog + self.station = station def run(self): start_date = datetime.now() @@ -50,7 +52,8 @@ class Measurement(threading.Thread): url=url, status=result.status, time=result.time, - protocol=result.protocol + protocol=result.protocol, + station ) def run_measurements(targets): @@ -69,6 +72,7 @@ if __name__ == "__main__": parser.add_argument("--smtp", "-s", nargs="*", help="SMTP STARTSSL hosts to measure") parser.add_argument("--pop3", "-p", nargs="*", help="POP3 SSL hosts to measure") parser.add_argument("--imap", "-m", nargs="*", help="IMAP SSL hosts to measure") + parser.add_argument("--station", default=00, help="set station name (default: 00)") args = parser.parse_args() exlog = logging.getLogger("exceptions") @@ -93,9 +97,9 @@ if __name__ == "__main__": parser.print_help() sys.exit(1) - schedule.every(args.interval).seconds.do(lambda: run_measurements(targets)) + schedule.every(args.interval).seconds.do(lambda: run_measurements(targets, args.station)) - run_measurements(targets) + run_measurements(targets, args.station) while True: try: From c3aaf8bf4be2f9735f53de44759f1a2911d9b784 Mon Sep 17 00:00:00 2001 From: Clemens Klug Date: Wed, 2 May 2018 15:41:29 +0200 Subject: [PATCH 3/3] refactor target out --- src/measure.py | 51 +++++++++++++++++++------------------------------- src/target.py | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 src/target.py diff --git a/src/measure.py b/src/measure.py index 0bdf8c3..3b7f590 100644 --- a/src/measure.py +++ b/src/measure.py @@ -6,60 +6,40 @@ import threading from datetime import datetime -import requests import schedule from sources import measure_http, measure_imap, measure_pop, measure_smtp +from target import TARGETS INFLUX_URL = "http://influxdb:8086/write?db=mydb" log = logging.getLogger(__name__) -INFLUX_CHARS = (",", "=", " ") - -def influx_escape(string): - for c in INFLUX_CHARS: - string = string.replace(c, "\\"+c) - return string - -def send_data(date, status, time, url, protocol, station=00): - data = "response_time,url={url},status={status},protocol={protocol},station={station} value={time} {timestamp}".format( - url=influx_escape(url), - status=influx_escape(str(status)), - time=time, - timestamp=int(date.timestamp()*1e9), - protocol=influx_escape(protocol), - station=station - ) - r=requests.post(INFLUX_URL, data=data) - log.warn(str(r) + " " + str(r.text)) class Measurement(threading.Thread): - def __init__(self, measure_fn, url, exlog, station=00): + def __init__(self, measure_fn, url, exlog, store): threading.Thread.__init__(self) - self.url = url self.measure_fn = measure_fn + self.url = url self.exlog = exlog - self.station = station + self.store = store def run(self): start_date = datetime.now() - url = self.url - result = self.measure_fn(url, exlog) - send_data( + result = self.measure_fn(self.url, self.exlog) + self.store.send_data( date=start_date, - url=url, status=result.status, time=result.time, - protocol=result.protocol, - station + url=self.url, + protocol=result.protocol ) -def run_measurements(targets): +def run_measurements(targets, store): for measure_fn in targets: for url in targets[measure_fn]: - Measurement(measure_fn, url, exlog).start() + Measurement(measure_fn, url, exlog, store).start() if __name__ == "__main__": @@ -73,8 +53,15 @@ if __name__ == "__main__": parser.add_argument("--pop3", "-p", nargs="*", help="POP3 SSL hosts to measure") parser.add_argument("--imap", "-m", nargs="*", help="IMAP SSL hosts to measure") parser.add_argument("--station", default=00, help="set station name (default: 00)") + parser.add_argument("--store", default="Influx", help="Target to POST measurements into") + parser.add_argument("--store-url", default="http://influxdb:8086/write?db=mydb", help="URL to the TARGET") args = parser.parse_args() + if not args.store in TARGETS: + print("invalid store, exit") + sys.exit(1) + store = TARGETS[args.store](args.store_url) + exlog = logging.getLogger("exceptions") if args.exceptions: filehandler = logging.FileHandler(args.exceptions) @@ -97,9 +84,9 @@ if __name__ == "__main__": parser.print_help() sys.exit(1) - schedule.every(args.interval).seconds.do(lambda: run_measurements(targets, args.station)) + schedule.every(args.interval).seconds.do(lambda: run_measurements(targets, store)) - run_measurements(targets, args.station) + run_measurements(targets, store) while True: try: diff --git a/src/target.py b/src/target.py new file mode 100644 index 0000000..09c9183 --- /dev/null +++ b/src/target.py @@ -0,0 +1,39 @@ +import logging +import requests + +log = logging.getLogger(__name__) + + +class Target: + def send_data(date, status, time, url, protocol, station): + raise NotImplementedError() + + +class Influx(Target): + INFLUX_CHARS = (",", "=", " ") + + def __init__(self, url): + self.url = url + + def escape(self, string): + for c in self.INFLUX_CHARS: + string = string.replace(c, "\\"+c) + return string + + def send_data(date, status, time, url, protocol, station=00): + data = "response_time,url={url},status={status},protocol={protocol},station={station} value={time} {timestamp}".format( + url=influx_escape(url), + status=influx_escape(str(status)), + time=time, + timestamp=int(date.timestamp()*1e9), + protocol=self.escape(protocol), + station=station + ) + r=requests.post(INFLUX_URL, data=data) + log.info(str(r) + " " + str(r.text)) + return r.ok + + +TARGETS = { + "Influx": Influx, +} \ No newline at end of file