added threading and a new field in the database

This commit is contained in:
Johannes Findeisen 2020-09-10 23:06:58 +02:00
commit 7a97b0a043
2 changed files with 52 additions and 45 deletions

View file

@ -2,6 +2,8 @@
**THIS CODE IS AT A VERY EARLY STAGE OF DEVELOPMENT! USE AT YOUR OWN RISK!**
**SINCE THIS CODE CHANGES EVERY DAY THE DOCUMENTATION MAY NOT BE CORRECT!**
uplink is a tool to monitor the uplink status of AVM FRITZ!Box Cable and DSL based routers. It uses the TR-064 protocol over UPnP.
## Features
@ -78,10 +80,12 @@ can move fast-forward.
- A lot... :)
- Make all config vars as ARGS and vice versa. ARGS have higher priority. Chain: default -> config -> ARGS.
- Parallelize queries using threads to improve performance; Does not work using SQLite because of exclusive access to the database
- SQL server backend; PostgreSQL? Or peewee ORM to support PostgreSQL, MySQL and SQLite.
- Gtk+ frontend. wxGlade?
- ~~A cron mode to not let uplink run in an endless loop to be scheduled and executed by cron.~~
- ~~Parallelize queries using threads to improve performance; Does not work using SQLite because of exclusive access to the database~~
- ~~SQL server backend; MariaDB/MySQL?~~ DONE
- Bring back SQLite support
- Switch to ORM (peewee?) to support PostgreSQL, MySQL and SQLite.
- Gtk+ frontend connecting to the database or loading a local copy of a SQLite file. wxGlade?
- ~~A cron mode to not let uplink run in an endless loop to be scheduled and executed by cron.~~ DONE
- A daemon mode to be a real UNIX daemon. For now, it's just sleep() based.
- Always keep platform independence in mind but not if uplink looses nice features on Linux.

85
uplink
View file

@ -33,6 +33,7 @@ import sys
import time
from fritzconnection.lib.fritzstatus import FritzStatus
from threading import Thread
__version__ = "0.1"
@ -102,8 +103,7 @@ def parse_args():
return parser.parse_args()
def get_data(config):
def get_data(config, inc):
#conn = sqlite3.connect(config["database"])
#c = conn.cursor()
@ -112,59 +112,62 @@ def get_data(config):
config["database_password"],
config["database"])
for i in range(len(config["uplinks"])):
timestamp = calendar.timegm(time.gmtime())
local_time = time.localtime(timestamp)
d = time.strftime("%Y-%m-%d ", local_time)
t = time.strftime("%H:%M:%S", local_time)
#for i in range(len(config["uplinks"])):
timestamp = calendar.timegm(time.gmtime())
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()
with con.cursor() as cur:
cur.execute(sql)
con.commit()
break
if fc.is_connected:
status = "UP"
else:
status = "DOWN"
logger.info(config["uplinks"][i]["provider"] + " " + status)
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) + '\",\"' + status + '\")'
try:
fc = FritzStatus(address=config["uplinks"][inc]["ip"], password=config["uplinks"][inc]["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, provider, message) ' \
'VALUES (\"' + str(timestamp) + '\",\"' + str(d) + '\",\"' + str(t) + '\",\"' + \
str(config["uplinks"][inc]["ip"]) + '\",\"' + "0" + '\",\"' + "0" + '\",\"' + \
str(config["uplinks"][inc]["provider"]) + '\",\"ERROR: ' + str(err) + '\")'
logging.debug(sql)
#c.execute(sql)
#conn.commit()
with con.cursor() as cur:
cur.execute(sql)
con.commit()
exit()
if fc.is_connected:
status = "UP"
else:
status = "DOWN"
logger.info(config["uplinks"][inc]["provider"] + " " + status)
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, provider, message) VALUES (\"' + str(timestamp) + '\",\"' + str(d) + '\",\"' + str(t) + \
'\",\"' + str(fc.uptime) + '\",\"' + str(config["uplinks"][inc]["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(config["uplinks"][inc]["provider"]) + '\",\"' + status + '\")'
logging.debug(sql)
#c.execute(sql)
#conn.commit()
with con.cursor() as cur:
cur.execute(sql)
con.commit()
#conn.close()
con.close()
def run(config):
while True:
get_data(config)
for i in range(len(config["uplinks"])):
t = Thread(target=get_data, args=(config, i))
t.start()
if config["cron"]:
exit(0)
break
time.sleep(config["interval"])