added exception handling, more fields to the database and some small fixes
This commit is contained in:
parent
a16d1d4dc6
commit
3ba39ed05c
2 changed files with 38 additions and 16 deletions
46
uplink.py
46
uplink.py
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
import argparse
|
||||
import calendar
|
||||
import datetime
|
||||
# import datetime
|
||||
# import daemon
|
||||
import json
|
||||
import logging
|
||||
|
|
@ -31,6 +31,7 @@ import os
|
|||
import os.path as path
|
||||
import sqlite3
|
||||
import time
|
||||
import tzlocal
|
||||
|
||||
from fritzconnection.lib.fritzstatus import FritzStatus
|
||||
|
||||
|
|
@ -41,7 +42,7 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="uplink.",
|
||||
description="uplink is a tool to monitor the link status of AVM FRITZ!Box based Cable and DSL routers.",
|
||||
epilog="uplink is not some program expecting uplinks to work!",
|
||||
prog="uplink")
|
||||
|
||||
|
|
@ -83,23 +84,41 @@ def get_data(config):
|
|||
c = conn.cursor()
|
||||
for i in range(len(config["uplinks"])):
|
||||
timestamp = calendar.timegm(time.gmtime())
|
||||
fc = FritzStatus(address=config["uplinks"][i]["ip"], password=config["uplinks"][i]["password"])
|
||||
local_timezone = tzlocal.get_localzone()
|
||||
logger.debug("Timezone:" + str(local_timezone))
|
||||
local_time = time.localtime(timestamp)
|
||||
d = time.strftime("%Y-%m-%d ", local_time)
|
||||
t = time.strftime("%H:%M:%S", local_time)
|
||||
|
||||
try:
|
||||
fc = FritzStatus(address=config["uplinks"][i]["ip"], password=config["uplinks"][i]["password"])
|
||||
except Exception as err:
|
||||
logger.error("ERROR: " + str(err) + " on device with ip:" + config["uplinks"][i]["ip"])
|
||||
sql = 'INSERT INTO log (timestamp, date, time, internal_ip, is_linked, is_connected, message) ' \
|
||||
'VALUES (\"' + str(timestamp) + '\",\"' + str(d) + '\",\"' + str(t) + '\",\"' + \
|
||||
str(config["uplinks"][i]["ip"]) + '\",\"' + "0" + '\",\"' + "0" + '\",\"ERROR: ' + str(err) + '\")'
|
||||
logging.debug(sql)
|
||||
c.execute(sql)
|
||||
conn.commit()
|
||||
break
|
||||
|
||||
if fc.is_connected:
|
||||
status = "UP"
|
||||
else:
|
||||
status = "DOWN"
|
||||
|
||||
logger.info(config["uplinks"][i]["provider"] + ": " + status)
|
||||
sql = 'INSERT INTO log (datetime, uptime, external_ip, external_ipv6,is_linked,is_connected, ' \
|
||||
'str_transmission_rate_up, str_transmission_rate_down, str_max_bit_rate_up, str_max_bit_rate_down, ' \
|
||||
'str_max_linked_bit_rate_up, str_max_linked_bit_rate_down, modelname, system_version) VALUES (\"' + \
|
||||
str(timestamp) + '\",\"' + str(fc.uptime) + '\",\"' + str(fc.external_ip) + '\",\"' + \
|
||||
sql = 'INSERT INTO log (timestamp, date, time, uptime, internal_ip, external_ip, external_ipv6, is_linked, ' \
|
||||
'is_connected, str_transmission_rate_up, str_transmission_rate_down, str_max_bit_rate_up, ' \
|
||||
'str_max_bit_rate_down, str_max_linked_bit_rate_up, str_max_linked_bit_rate_down, modelname, ' \
|
||||
'system_version, message) VALUES (\"' + str(timestamp) + '\",\"' + str(d) + '\",\"' + str(t) + '\",\"' + \
|
||||
str(fc.uptime) + '\",\"' + str(config["uplinks"][i]["ip"]) + '\",\"' + str(fc.external_ip) + '\",\"' + \
|
||||
str(fc.external_ipv6) + '\",\"' + str(int(fc.is_linked)) + '\",\"' + \
|
||||
str(int(fc.is_connected)) + '\",\"' + str(fc.str_transmission_rate[0]) + '\",\"' + \
|
||||
str(fc.str_transmission_rate[1]) + '\",\"' + str(fc.str_max_bit_rate[0]) + '\",\"' + \
|
||||
str(fc.str_max_bit_rate[1]) + '\",\"' + str(fc.str_max_linked_bit_rate[0]) + '\",\"' + \
|
||||
str(fc.str_max_linked_bit_rate[1]) + '\",\"' + str(fc.modelname) + '\",\"' + \
|
||||
str(fc.fc.system_version) + '\")'
|
||||
str(fc.fc.system_version) + '\",\"' + status + '\")'
|
||||
logging.debug(sql)
|
||||
c.execute(sql)
|
||||
conn.commit()
|
||||
|
|
@ -116,12 +135,6 @@ def run(config):
|
|||
if __name__ == "__main__":
|
||||
args = parse_args()
|
||||
|
||||
configpath = args.config
|
||||
|
||||
with open(configpath, 'r') as configfile:
|
||||
config_data = configfile.read()
|
||||
_config = json.loads(config_data)
|
||||
|
||||
logfile = path.expanduser(args.logfile)
|
||||
if not path.exists(path.dirname(logfile)):
|
||||
os.makedirs(path.dirname(logfile))
|
||||
|
|
@ -133,4 +146,9 @@ if __name__ == "__main__":
|
|||
root_logger.addHandler(handler)
|
||||
root_logger.setLevel(args.loglevel)
|
||||
|
||||
config_path = args.config
|
||||
with open(config_path, 'r') as configfile:
|
||||
config_data = configfile.read()
|
||||
_config = json.loads(config_data)
|
||||
|
||||
run(_config)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
CREATE TABLE "log" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
|
||||
"datetime" TEXT NOT NULL,
|
||||
"timestamp" TEXT NOT NULL,
|
||||
"date" TEXT,
|
||||
"time" TEXT,
|
||||
"uptime" INTEGER,
|
||||
"internal_ip" TEXT,
|
||||
"external_ip" TEXT,
|
||||
"external_ipv6" TEXT,
|
||||
"is_linked" INTEGER,
|
||||
|
|
@ -13,5 +16,6 @@ CREATE TABLE "log" (
|
|||
"str_max_linked_bit_rate_up" TEXT,
|
||||
"str_max_linked_bit_rate_down" TEXT,
|
||||
"modelname" TEXT,
|
||||
"system_version" TEXT
|
||||
"system_version" TEXT,
|
||||
"message" TEXT
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue