refactor target out
parent
8361023a3e
commit
c3aaf8bf4b
|
|
@ -6,60 +6,40 @@ import threading
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import requests
|
|
||||||
import schedule
|
import schedule
|
||||||
|
|
||||||
from sources import measure_http, measure_imap, measure_pop, measure_smtp
|
from sources import measure_http, measure_imap, measure_pop, measure_smtp
|
||||||
|
from target import TARGETS
|
||||||
|
|
||||||
INFLUX_URL = "http://influxdb:8086/write?db=mydb"
|
INFLUX_URL = "http://influxdb:8086/write?db=mydb"
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
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):
|
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)
|
threading.Thread.__init__(self)
|
||||||
self.url = url
|
|
||||||
self.measure_fn = measure_fn
|
self.measure_fn = measure_fn
|
||||||
|
self.url = url
|
||||||
self.exlog = exlog
|
self.exlog = exlog
|
||||||
self.station = station
|
self.store = store
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
start_date = datetime.now()
|
start_date = datetime.now()
|
||||||
url = self.url
|
result = self.measure_fn(self.url, self.exlog)
|
||||||
result = self.measure_fn(url, exlog)
|
self.store.send_data(
|
||||||
send_data(
|
|
||||||
date=start_date,
|
date=start_date,
|
||||||
url=url,
|
|
||||||
status=result.status,
|
status=result.status,
|
||||||
time=result.time,
|
time=result.time,
|
||||||
protocol=result.protocol,
|
url=self.url,
|
||||||
station
|
protocol=result.protocol
|
||||||
)
|
)
|
||||||
|
|
||||||
def run_measurements(targets):
|
def run_measurements(targets, store):
|
||||||
for measure_fn in targets:
|
for measure_fn in targets:
|
||||||
for url in targets[measure_fn]:
|
for url in targets[measure_fn]:
|
||||||
Measurement(measure_fn, url, exlog).start()
|
Measurement(measure_fn, url, exlog, store).start()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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("--pop3", "-p", nargs="*", help="POP3 SSL hosts to measure")
|
||||||
parser.add_argument("--imap", "-m", nargs="*", help="IMAP 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("--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()
|
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")
|
exlog = logging.getLogger("exceptions")
|
||||||
if args.exceptions:
|
if args.exceptions:
|
||||||
filehandler = logging.FileHandler(args.exceptions)
|
filehandler = logging.FileHandler(args.exceptions)
|
||||||
|
|
@ -97,9 +84,9 @@ if __name__ == "__main__":
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
sys.exit(1)
|
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:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue