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