Directories restructered

This commit is contained in:
Johannes Findeisen 2021-12-28 23:49:01 +01:00
commit 5a0b170610
7 changed files with 382 additions and 264 deletions

120
.gitignore vendored
View file

@ -3,4 +3,122 @@
__pycache__
config.json
log/*.log*
uplink.iml
uplink.iml
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/

173
uplink Executable file
View file

@ -0,0 +1,173 @@
#!/usr/bin/python3 -d
# Copyright (c) 2020 Johannes Findeisen <you@hanez.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import json
import logging
import logging.handlers
import os
import sys
import time
from threading import Thread
from uplink import Uplink
# Version format: MAJOR.FEATURE.FIXES
__version__ = "0.5.0-development"
# TODO: CHECK ALL ERROR HANDLING!!!
# TODO: IDEA: Implement a small webserver inline to get statistics and graphs over the network?
# Or maybe better as a separate daemon
# TODO: Make use of more args passed to the script
# TODO: Implement a speedtest that will also run regularly but in an individual interval
logger = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser(
description="uplink is a tool to monitor the link status of AVM FRITZ!Box Cable and DSL "
"based routers.",
epilog="uplink is not some program expecting uplinks to work!",
prog="uplink")
parser.add_argument("config", metavar="CONFIGFILE", help="the configfile to use")
mode = parser.add_mutually_exclusive_group()
mode.add_argument("-c", "--cron", default=False, dest="cron", action="store_true",
help="cron mode executes the script only once without loop (default: false)")
mode.add_argument("-d", "--daemon", default=False, dest="daemon", action="store_true",
help="run as native daemon (default: false)")
mode.add_argument("-f", "--foreground", default=False, dest="foreground", action="store_true",
help="run looped in foreground (default: false)")
parser.add_argument("-i", "--interval", type=int, help="poll interval in seconds. this "
"overrides config file settings. "
"(default: 60)")
"""
parser.add_argument("-b", "--database", metavar="DATABASE", help="database to use")
parser.add_argument("-u", "--user", type=str, help="database username")
parser.add_argument("-p", "--password", type=str, help="database password")
"""
parser.add_argument("-l", "--logfile", metavar="FILE", help="logfile to use")
parser.add_argument("-n", "--logcount", default=5, type=int,
help="maximum number of logfiles in rotation (default: 5)")
parser.add_argument("-m", "--logsize", default=10485760, type=int,
help="maximum logfile size in bytes (default: 1048576)")
parser.add_argument("-s", "--stdout", default=False, dest="stdout", action="store_true",
help="log to stdout")
output = parser.add_mutually_exclusive_group()
output.add_argument("-q", "--quiet", action="store_const", dest="loglevel",
const=logging.ERROR, help="output only errors")
output.add_argument("-w", "--warning", action="store_const", dest="loglevel",
const=logging.WARNING, help="output warnings")
output.add_argument("-v", "--verbose", action="store_const", dest="loglevel",
const=logging.INFO, help="output info messages")
output.add_argument("-e", "--debug", action="store_const", dest="loglevel",
const=logging.DEBUG, help="output debug messages")
output.set_defaults(loglevel=logging.ERROR)
parser.add_argument("--version", action="version", version="%(prog)s " + str(__version__))
return parser.parse_args()
def main():
args = parse_args()
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)
config_path = args.config
try:
with open(config_path, 'r', encoding='utf-8') as configfile:
config_data = configfile.read()
config = json.loads(config_data)
except Exception as err:
logger.error(str("uplink: configuration error! " + str(err)))
sys.exit(1)
try:
config["interval"]
except KeyError:
# interval not configured in configuration; using default value
config["interval"] = 60
if args.interval:
# interval set in args is overriding configuration and default
config["interval"] = args.interval
uplink = Uplink("/tmp/uplink.pid", config)
if args.cron:
for i in range(len(config["uplinks"])):
t = Thread(target=uplink.fetch_data, args=(config, i))
t.start()
elif args.daemon:
uplink.start()
elif args.foreground:
while True:
for i in range(len(config["uplinks"])):
t = Thread(target=uplink.fetch_data, args=(config, i))
t.start()
try:
time.sleep(config["interval"])
except KeyboardInterrupt:
logger.info(str("uplink: program terminated by user!"))
sys.exit(0)
else:
logger.error("uplink: no run mode selected; use --cron (-c), --daemon (-d) or "
"--foreground (-f) to run uplink. use --help for more information")
if __name__ == "__main__":
main()

214
uplink.py Executable file → Normal file
View file

@ -1,6 +1,4 @@
#!/usr/bin/python3 -d
# Copyright (c) 2020 Johannes Findeisen <you@hanez.org>
# Copyright (c) 2021 Johannes Findeisen <you@hanez.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@ -20,154 +18,98 @@
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import json
import logging
import logging.handlers
import os
from logging import getLogger
import socket
import sys
import time
import calendar
try:
from fritzconnection.fritzconnection.lib.fritzstatus import FritzStatus
except ImportError:
from fritzconnection.lib.fritzstatus import FritzStatus
from threading import Thread
from uplink.uplink import Uplink
import pymysql
from daemon import Daemon
# Version format: MAJOR.FEATURE.FIXES
__version__ = "0.5.0-development"
# TODO: CHECK ALL ERROR HANDLING!!!
# TODO: IDEA: Implement a small webserver inline to get statistics and graphs over the network?
# Or maybe better as a separate daemon
# TODO: Make use of more args passed to the script
# TODO: Implement a speedtest that will also run regularly but in an individual interval
logger = logging.getLogger(__name__)
logger = getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser(
description="uplink is a tool to monitor the link status of AVM FRITZ!Box Cable and DSL "
"based routers.",
epilog="uplink is not some program expecting uplinks to work!",
prog="uplink")
class Uplink(Daemon):
parser.add_argument("config", metavar="CONFIGFILE", help="the configfile to use")
def __init__(self, pid_file, config):
super().__init__(pid_file)
self.pid_file = pid_file
self.config = config
self.date = None
self.time = None
self.status = None
mode = parser.add_mutually_exclusive_group()
mode.add_argument("-c", "--cron", default=False, dest="cron", action="store_true",
help="cron mode executes the script only once without loop (default: false)")
def fetch_data(self, config, inc):
try:
# TODO: Think about using a DB ORM (SQLAlchemy?) to make this program supporting different databases like
# sqlite and Postgres
con = pymysql.connect(host=config["database_host"],
user=config["database_user"],
password=config["database_password"],
database=config["database"])
except Exception as err:
logger.error(str("uplink: Database connection failed: " + str(err)))
sys.exit(1)
mode.add_argument("-d", "--daemon", default=False, dest="daemon", action="store_true",
help="run as native daemon (default: false)")
timestamp = calendar.timegm(time.gmtime())
local_time = time.localtime(timestamp)
self.date = time.strftime("%Y-%m-%d", local_time)
self.time = time.strftime("%H:%M:%S", local_time)
mode.add_argument("-f", "--foreground", default=False, dest="foreground", action="store_true",
help="run looped in foreground (default: false)")
try:
fc = FritzStatus(address=config["uplinks"][inc]["ip"], password=config["uplinks"][inc]["password"])
except Exception as err:
logger.error(str(str(err) + " on device with ip: " + config["uplinks"][inc]["ip"]))
# 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, ' \
'source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(self.date) + '\",\"' + str(self.time) + '\",\"' + \
str(config["uplinks"][inc]["ip"]) + '\",\"' + "0" + '\",\"' + "0" + '\",\"' + \
str(config["uplinks"][inc]["provider"]) + '\",\"ERROR: ' + str(err) + '\",\"' + socket.gethostname() + \
'\")'
parser.add_argument("-i", "--interval", type=int, help="poll interval in seconds. this "
"overrides config file settings. "
"(default: 60)")
logger.debug(str(sql))
with con.cursor() as cur:
cur.execute(sql)
con.commit()
sys.exit(1)
"""
parser.add_argument("-b", "--database", metavar="DATABASE", help="database to use")
if fc.is_connected:
self.status = "UP"
else:
self.status = "DOWN"
parser.add_argument("-u", "--user", type=str, help="database username")
logger.info(str(config["uplinks"][inc]["provider"] + " " + self.status))
# 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, ' \
'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, source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(self.date) + '\",\"' + \
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(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"]) + '\",\"' + self.status + '\",\"' + \
socket.gethostname() + '\")'
parser.add_argument("-p", "--password", type=str, help="database password")
"""
parser.add_argument("-l", "--logfile", metavar="FILE", help="logfile to use")
logger.debug(str(sql))
with con.cursor() as cur:
cur.execute(sql)
con.commit()
con.close()
parser.add_argument("-n", "--logcount", default=5, type=int,
help="maximum number of logfiles in rotation (default: 5)")
parser.add_argument("-m", "--logsize", default=10485760, type=int,
help="maximum logfile size in bytes (default: 1048576)")
parser.add_argument("-s", "--stdout", default=False, dest="stdout", action="store_true",
help="log to stdout")
output = parser.add_mutually_exclusive_group()
output.add_argument("-q", "--quiet", action="store_const", dest="loglevel",
const=logging.ERROR, help="output only errors")
output.add_argument("-w", "--warning", action="store_const", dest="loglevel",
const=logging.WARNING, help="output warnings")
output.add_argument("-v", "--verbose", action="store_const", dest="loglevel",
const=logging.INFO, help="output info messages")
output.add_argument("-e", "--debug", action="store_const", dest="loglevel",
const=logging.DEBUG, help="output debug messages")
output.set_defaults(loglevel=logging.ERROR)
parser.add_argument("--version", action="version", version="%(prog)s " + str(__version__))
return parser.parse_args()
def main():
args = parse_args()
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)
config_path = args.config
try:
with open(config_path, 'r', encoding='utf-8') as configfile:
config_data = configfile.read()
config = json.loads(config_data)
except Exception as err:
logger.error(str("uplink: configuration error! " + str(err)))
sys.exit(1)
try:
config["interval"]
except KeyError:
# interval not configured in configuration; using default value
config["interval"] = 60
if args.interval:
# interval set in args is overriding configuration and default
config["interval"] = args.interval
uplink = Uplink("/tmp/uplink.pid", config)
if args.cron:
for i in range(len(config["uplinks"])):
t = Thread(target=uplink.fetch_data, args=(config, i))
t.start()
elif args.daemon:
uplink.start()
elif args.foreground:
def run(self):
while True:
for i in range(len(config["uplinks"])):
t = Thread(target=uplink.fetch_data, args=(config, i))
for i in range(len(self.config["uplinks"])):
t = Thread(target=self.fetch_data, args=(self.config, i))
t.start()
try:
time.sleep(config["interval"])
except KeyboardInterrupt:
logger.info(str("uplink: program terminated by user!"))
sys.exit(0)
else:
logger.error("uplink: no run mode selected; use --cron (-c), --daemon (-d) or "
"--foreground (-f) to run uplink. use --help for more information")
if __name__ == "__main__":
main()
time.sleep(self.config["interval"])

View file

View file

@ -1,115 +0,0 @@
# Copyright (c) 2021 Johannes Findeisen <you@hanez.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from logging import getLogger
import socket
import sys
import time
import calendar
try:
from fritzconnection.fritzconnection.lib.fritzstatus import FritzStatus
except ImportError:
from fritzconnection.lib.fritzstatus import FritzStatus
from threading import Thread
import pymysql
from uplink.daemon import Daemon
logger = getLogger(__name__)
class Uplink(Daemon):
def __init__(self, pid_file, config):
super().__init__(pid_file)
self.pid_file = pid_file
self.config = config
self.date = None
self.time = None
self.status = None
def fetch_data(self, config, inc):
try:
# TODO: Think about using a DB ORM (SQLAlchemy?) to make this program supporting different databases like
# sqlite and Postgres
con = pymysql.connect(host=config["database_host"],
user=config["database_user"],
password=config["database_password"],
database=config["database"])
except Exception as err:
logger.error(str("uplink: Database connection failed: " + str(err)))
sys.exit(1)
timestamp = calendar.timegm(time.gmtime())
local_time = time.localtime(timestamp)
self.date = time.strftime("%Y-%m-%d", local_time)
self.time = time.strftime("%H:%M:%S", local_time)
try:
fc = FritzStatus(address=config["uplinks"][inc]["ip"], password=config["uplinks"][inc]["password"])
except Exception as err:
logger.error(str(str(err) + " on device with ip: " + config["uplinks"][inc]["ip"]))
# 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, ' \
'source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(self.date) + '\",\"' + str(self.time) + '\",\"' + \
str(config["uplinks"][inc]["ip"]) + '\",\"' + "0" + '\",\"' + "0" + '\",\"' + \
str(config["uplinks"][inc]["provider"]) + '\",\"ERROR: ' + str(err) + '\",\"' + socket.gethostname() + \
'\")'
logger.debug(str(sql))
with con.cursor() as cur:
cur.execute(sql)
con.commit()
sys.exit(1)
if fc.is_connected:
self.status = "UP"
else:
self.status = "DOWN"
logger.info(str(config["uplinks"][inc]["provider"] + " " + self.status))
# 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, ' \
'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, source_host) VALUES (\"' + str(timestamp) + '\",\"' + str(self.date) + '\",\"' + \
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(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"]) + '\",\"' + self.status + '\",\"' + \
socket.gethostname() + '\")'
logger.debug(str(sql))
with con.cursor() as cur:
cur.execute(sql)
con.commit()
con.close()
def run(self):
while True:
for i in range(len(self.config["uplinks"])):
t = Thread(target=self.fetch_data, args=(self.config, i))
t.start()
time.sleep(self.config["interval"])