Added basic logging to file and stdout. Set fritzconnection to version 1.8.0
This commit is contained in:
parent
16892247fc
commit
ac1369657f
3 changed files with 73 additions and 54 deletions
|
|
@ -1 +1 @@
|
||||||
Subproject commit ea24777f4032814b145d7d972d089d2f22e47e0f
|
Subproject commit 33e031d3fcc811611c5c25adf07a073e1b32e2a5
|
||||||
79
uplink.py
79
uplink.py
|
|
@ -22,8 +22,9 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
# import logging
|
import logging
|
||||||
# import logging.handlers
|
import logging.handlers
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
@ -31,16 +32,16 @@ from threading import Thread
|
||||||
from uplink.uplink import Uplink
|
from uplink.uplink import Uplink
|
||||||
|
|
||||||
# Version format: MAJOR.FEATURE.FIXES
|
# Version format: MAJOR.FEATURE.FIXES
|
||||||
__version__ = "0.4.2-development"
|
__version__ = "0.5.0-development"
|
||||||
|
|
||||||
# TODO: CHECK ALL ERROR HANDLING!!!
|
# TODO: CHECK ALL ERROR HANDLING!!!
|
||||||
# TODO: Implement logging
|
|
||||||
# TODO: Implement CLI output messages
|
|
||||||
# TODO: IDEA: Implement a small webserver inline to get statistics and graphs over the network? Or maybe better as a
|
# TODO: IDEA: Implement a small webserver inline to get statistics and graphs over the network? Or maybe better as a
|
||||||
# separate daemon
|
# separate daemon
|
||||||
# TODO: Make use of more args passed to the script
|
# TODO: Make use of more args passed to the script
|
||||||
# TODO: Implement a speedtest that will also run regularly but in an individual interval
|
# TODO: Implement a speedtest that will also run regularly but in an individual interval
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
|
|
@ -71,51 +72,69 @@ def parse_args():
|
||||||
parser.add_argument("-u", "--user", type=str, help="database username")
|
parser.add_argument("-u", "--user", type=str, help="database username")
|
||||||
|
|
||||||
parser.add_argument("-p", "--password", type=str, help="database password")
|
parser.add_argument("-p", "--password", type=str, help="database password")
|
||||||
|
"""
|
||||||
parser.add_argument("-l", "--logfile", metavar="FILE", help="logfile to use")
|
parser.add_argument("-l", "--logfile", metavar="FILE", help="logfile to use")
|
||||||
|
|
||||||
parser.add_argument("-c", "--log-count", default=5, type=int,
|
parser.add_argument("-n", "--logcount", default=5, type=int,
|
||||||
help="maximum number of logfiles in rotation (default: 5)")
|
help="maximum number of logfiles in rotation (default: 5)")
|
||||||
|
|
||||||
parser.add_argument("-m", "--log-size", default=10485760, type=int,
|
parser.add_argument("-m", "--logsize", default=10485760, type=int,
|
||||||
help="maximum logfile size in bytes (default: 10485760)")
|
help="maximum logfile size in bytes (default: 10485760)")
|
||||||
|
|
||||||
parser.add_argument("-s", "--stdout", default=False, dest="stdout", action="store_true", help="log to stdout")
|
parser.add_argument("-s", "--stdout", default=False, dest="stdout", action="store_true",
|
||||||
|
help="log to stdout")
|
||||||
|
|
||||||
output = parser.add_mutually_exclusive_group()
|
output = parser.add_mutually_exclusive_group()
|
||||||
output.add_argument("-q", "--quiet", action="store_const", dest="log_level", const=logging.ERROR,
|
output.add_argument("-q", "--quiet", action="store_const", dest="loglevel",
|
||||||
help="output only errors")
|
const=logging.ERROR, help="output only errors")
|
||||||
|
|
||||||
output.add_argument("-w", "--warning", action="store_const", dest="log_level", const=logging.WARNING,
|
output.add_argument("-w", "--warning", action="store_const", dest="loglevel",
|
||||||
help="output warnings")
|
const=logging.WARNING, help="output warnings")
|
||||||
|
|
||||||
output.add_argument("-v", "--verbose", action="store_const", dest="log_level", const=logging.INFO,
|
output.add_argument("-v", "--verbose", action="store_const", dest="loglevel",
|
||||||
help="output info messages")
|
const=logging.INFO, help="output info messages")
|
||||||
|
|
||||||
output.add_argument("-d", "--debug", action="store_const", dest="log_level", const=logging.DEBUG,
|
output.add_argument("-e", "--debug", action="store_const", dest="loglevel",
|
||||||
help="output debug messages")
|
const=logging.DEBUG, help="output debug messages")
|
||||||
output.set_defaults(loglevel=logging.ERROR)
|
output.set_defaults(loglevel=logging.ERROR)
|
||||||
"""
|
|
||||||
|
|
||||||
parser.add_argument("--version", action="version", version="%(prog)s " + str(__version__))
|
parser.add_argument("--version", action="version", version="%(prog)s " + str(__version__))
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def main():
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
|
||||||
# TODO: Read --version argument and print program version; then exit.
|
root_logger = logging.getLogger()
|
||||||
|
|
||||||
|
if args.logfile:
|
||||||
|
logfile = os.path.expanduser(args.logfile)
|
||||||
|
if not os.path.exists(os.path.dirname(logfile)):
|
||||||
|
os.makedirs(os.path.dirname(logfile))
|
||||||
|
|
||||||
|
formatter1 = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s")
|
||||||
|
handler1 = logging.handlers.RotatingFileHandler(args.logfile, maxBytes=args.logsize,
|
||||||
|
backupCount=args.logcount)
|
||||||
|
handler1.setFormatter(formatter1)
|
||||||
|
root_logger.addHandler(handler1)
|
||||||
|
|
||||||
|
if args.stdout:
|
||||||
|
formatter2 = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s")
|
||||||
|
handler2 = logging.StreamHandler(sys.stdout)
|
||||||
|
handler2.setFormatter(formatter2)
|
||||||
|
root_logger.addHandler(handler2)
|
||||||
|
|
||||||
|
root_logger.setLevel(args.loglevel)
|
||||||
|
|
||||||
# TODO: Cleanup config.json and only define what really is needed
|
# TODO: Cleanup config.json and only define what really is needed
|
||||||
config_path = args.config
|
config_path = args.config
|
||||||
try:
|
try:
|
||||||
with open(config_path, 'r') as configfile:
|
with open(config_path, 'r', encoding='utf-8') as configfile:
|
||||||
config_data = configfile.read()
|
config_data = configfile.read()
|
||||||
config = json.loads(config_data)
|
config = json.loads(config_data)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
# TODO: Replace all lines like this with generic Python logging
|
logger.error(str("uplink: configuration error! " + str(err)))
|
||||||
print(str("uplink: Configuration Error! " + str(err)))
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -132,20 +151,24 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
if args.cron:
|
if args.cron:
|
||||||
for i in range(len(config["uplinks"])):
|
for i in range(len(config["uplinks"])):
|
||||||
t = Thread(target=uplink.get_data, args=(config, i))
|
t = Thread(target=uplink.fetch_data, args=(config, i))
|
||||||
t.start()
|
t.start()
|
||||||
elif args.daemon:
|
elif args.daemon:
|
||||||
uplink.start()
|
uplink.start()
|
||||||
elif args.foreground:
|
elif args.foreground:
|
||||||
while True:
|
while True:
|
||||||
for i in range(len(config["uplinks"])):
|
for i in range(len(config["uplinks"])):
|
||||||
t = Thread(target=uplink.get_data, args=(config, i))
|
t = Thread(target=uplink.fetch_data, args=(config, i))
|
||||||
t.start()
|
t.start()
|
||||||
try:
|
try:
|
||||||
time.sleep(config["interval"])
|
time.sleep(config["interval"])
|
||||||
except KeyboardInterrupt as err:
|
except KeyboardInterrupt as err:
|
||||||
print(str("uplink: program terminated by user!"))
|
logger.info(str("uplink: program terminated by user!"))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
print("uplink: error: no run mode selected; use --cron (-c), --daemon (-d) or "
|
logger.error("uplink: no run mode selected; use --cron (-c), --daemon (-d) or "
|
||||||
"--foreground (-f) to run uplink. use --help for more information")
|
"--foreground (-f) to run uplink. use --help for more information")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
from logging import getLogger
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
@ -33,6 +34,8 @@ from threading import Thread
|
||||||
import pymysql
|
import pymysql
|
||||||
from uplink.daemon import Daemon
|
from uplink.daemon import Daemon
|
||||||
|
|
||||||
|
logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Uplink(Daemon):
|
class Uplink(Daemon):
|
||||||
|
|
||||||
|
|
@ -40,9 +43,11 @@ class Uplink(Daemon):
|
||||||
super().__init__(pid_file)
|
super().__init__(pid_file)
|
||||||
self.pid_file = pid_file
|
self.pid_file = pid_file
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self.date = None
|
||||||
|
self.time = None
|
||||||
|
self.status = None
|
||||||
|
|
||||||
@staticmethod
|
def fetch_data(self, config, inc):
|
||||||
def get_data(config, inc):
|
|
||||||
try:
|
try:
|
||||||
# TODO: Think about using a DB ORM (SQLAlchemy?) to make this program supporting different databases like
|
# TODO: Think about using a DB ORM (SQLAlchemy?) to make this program supporting different databases like
|
||||||
# sqlite and Postgres
|
# sqlite and Postgres
|
||||||
|
|
@ -51,69 +56,60 @@ class Uplink(Daemon):
|
||||||
password=config["database_password"],
|
password=config["database_password"],
|
||||||
database=config["database"])
|
database=config["database"])
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
# TODO: Replace all lines like this with generic Python logging
|
logger.error(str("uplink: Database connection failed: " + str(err)))
|
||||||
print(str("Uplink: Database connection failed: " + str(err)))
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
timestamp = calendar.timegm(time.gmtime())
|
timestamp = calendar.timegm(time.gmtime())
|
||||||
local_time = time.localtime(timestamp)
|
local_time = time.localtime(timestamp)
|
||||||
d = time.strftime("%Y-%m-%d", local_time)
|
self.date = time.strftime("%Y-%m-%d", local_time)
|
||||||
t = time.strftime("%H:%M:%S", local_time)
|
self.time = time.strftime("%H:%M:%S", local_time)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fc = FritzStatus(address=config["uplinks"][inc]["ip"], password=config["uplinks"][inc]["password"])
|
fc = FritzStatus(address=config["uplinks"][inc]["ip"], password=config["uplinks"][inc]["password"])
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
# TODO: Replace all lines like this with generic Python logging
|
logger.error(str(str(err) + " on device with ip: " + config["uplinks"][inc]["ip"]))
|
||||||
print(str(str(err) + " on device with ip: " + config["uplinks"][inc]["ip"]))
|
|
||||||
# TODO: Fix to long lines and make the SQL statement more readable
|
# TODO: Fix to long lines and make the SQL statement more readable
|
||||||
sql = 'INSERT INTO log (timestamp, date, time, internal_ip, is_linked, is_connected, provider, message, ' \
|
sql = 'INSERT INTO log (timestamp, date, time, internal_ip, is_linked, is_connected, provider, message, ' \
|
||||||
'source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(d) + '\",\"' + str(t) + '\",\"' + \
|
'source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(self.date) + '\",\"' + str(self.time) + '\",\"' + \
|
||||||
str(config["uplinks"][inc]["ip"]) + '\",\"' + "0" + '\",\"' + "0" + '\",\"' + \
|
str(config["uplinks"][inc]["ip"]) + '\",\"' + "0" + '\",\"' + "0" + '\",\"' + \
|
||||||
str(config["uplinks"][inc]["provider"]) + '\",\"ERROR: ' + str(err) + '\",\"' + socket.gethostname() + \
|
str(config["uplinks"][inc]["provider"]) + '\",\"ERROR: ' + str(err) + '\",\"' + socket.gethostname() + \
|
||||||
'\")'
|
'\")'
|
||||||
|
|
||||||
# TODO: Replace all lines like this with generic Python logging
|
logger.debug(str(sql))
|
||||||
# print(str(sql))
|
|
||||||
with con.cursor() as cur:
|
with con.cursor() as cur:
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
con.commit()
|
con.commit()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if fc.is_connected:
|
if fc.is_connected:
|
||||||
status = "UP"
|
self.status = "UP"
|
||||||
else:
|
else:
|
||||||
status = "DOWN"
|
self.status = "DOWN"
|
||||||
|
|
||||||
# TODO: Replace all lines like this with generic Python logging
|
|
||||||
print(str("[" + str(d) + " " + str(t) + "] " + config["uplinks"][inc]["provider"] + " " + status))
|
|
||||||
|
|
||||||
|
logger.info(str(config["uplinks"][inc]["provider"] + " " + self.status))
|
||||||
# TODO: Fix to long lines and make the SQL statement more readable
|
# TODO: Fix to long lines and make the SQL statement more readable
|
||||||
sql = 'INSERT INTO log (timestamp, date, time, uptime, internal_ip, external_ip, external_ipv6, is_linked, ' \
|
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, ' \
|
'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, ' \
|
'str_max_bit_rate_down, str_max_linked_bit_rate_up, str_max_linked_bit_rate_down, modelname, ' \
|
||||||
'system_version, provider, message, source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(d) + '\",\"' + \
|
'system_version, provider, message, source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(self.date) + '\",\"' + \
|
||||||
str(t) + '\",\"' + str(fc.uptime) + '\",\"' + str(config["uplinks"][inc]["ip"]) + '\",\"' + \
|
str(self.time) + '\",\"' + str(fc.uptime) + '\",\"' + str(config["uplinks"][inc]["ip"]) + '\",\"' + \
|
||||||
str(fc.external_ip) + '\",\"' + str(fc.external_ipv6) + '\",\"' + str(int(fc.is_linked)) + '\",\"' + \
|
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(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_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_bit_rate[1]) + '\",\"' + str(fc.str_max_linked_bit_rate[0]) + '\",\"' + \
|
||||||
str(fc.str_max_linked_bit_rate[1]) + '\",\"' + str(fc.modelname) + '\",\"' + \
|
str(fc.str_max_linked_bit_rate[1]) + '\",\"' + str(fc.modelname) + '\",\"' + \
|
||||||
str(fc.fc.system_version) + '\",\"' + str(config["uplinks"][inc]["provider"]) + '\",\"' + status + '\",\"' + \
|
str(fc.fc.system_version) + '\",\"' + str(config["uplinks"][inc]["provider"]) + '\",\"' + self.status + '\",\"' + \
|
||||||
socket.gethostname() + '\")'
|
socket.gethostname() + '\")'
|
||||||
|
|
||||||
# TODO: Replace all lines like this with generic Python logging
|
logger.debug(str(sql))
|
||||||
# print(str(sql))
|
|
||||||
with con.cursor() as cur:
|
with con.cursor() as cur:
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
con.commit()
|
con.commit()
|
||||||
con.close()
|
con.close()
|
||||||
|
|
||||||
def get_config(self):
|
|
||||||
return self.config
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
for i in range(len(self.config["uplinks"])):
|
for i in range(len(self.config["uplinks"])):
|
||||||
t = Thread(target=self.get_data, args=(self.get_config(), i))
|
t = Thread(target=self.fetch_data, args=(self.config, i))
|
||||||
t.start()
|
t.start()
|
||||||
time.sleep(self.config["interval"])
|
time.sleep(self.config["interval"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue