Totally refactored. Split everything in classes. Integrated HTTP server support for a status page. bottle, celery and pyramid supported but I will choose only one. Currently cherrypy looks perfect. Writing results to a database will become optionally and will use SQLAlchemy in the next days. This all is a work in progress and the code should not really be used. I am refreshing my Python skills and I hope the next release will be modular and usable for everybody... stay tuned
This commit is contained in:
parent
3c4aa09bf1
commit
a14fccfb71
16 changed files with 780 additions and 167 deletions
186
bin/uplink
186
bin/uplink
|
|
@ -3,7 +3,7 @@
|
|||
# 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
|
||||
# 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
|
||||
|
|
@ -13,14 +13,24 @@
|
|||
# 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
|
||||
# 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.
|
||||
|
||||
# TODO: CHECK ALL ERROR HANDLING!!!
|
||||
# TODO: Make use of less args passed to the script and set values in the config file
|
||||
# TODO: Implement a speedtest/ping that will also run regularly but in an individual interval
|
||||
# TODO: become more verbose in each log level and log only to error when it really is an error
|
||||
# else log to info and even add debug messages in DEBUG level
|
||||
# TODO: Maybe make path to pid_file an arg to use /var/run/user/$uid/uplink.pid instead of
|
||||
# /tmp/uplink.pid or check user id and write it to /var/run or var/run/user/$uid automatically
|
||||
# TODO: make all args consistent and clean... there is some stuff to do!
|
||||
|
||||
import argparse
|
||||
import calendar
|
||||
import json
|
||||
import logging
|
||||
import logging.handlers
|
||||
|
|
@ -28,87 +38,73 @@ import os
|
|||
import sys
|
||||
import time
|
||||
|
||||
from uplink.configuration import Configuration
|
||||
from uplink.uplink import Uplink
|
||||
|
||||
# Version format: MAJOR.FEATURE.FIXES
|
||||
__version__ = "0.6.1"
|
||||
|
||||
# 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
|
||||
# TODO: become more verbose in each log level and log only to error when it really is an error
|
||||
# else log to info
|
||||
# TODO: Maybe make path to pid_file an arg to use /var/run/$user/uplink.pid instead of
|
||||
# /tmp/uplink.pid
|
||||
__version__ = '0.7.0'
|
||||
__author__ = 'Johannes Findeisen <you@hanez.org>'
|
||||
|
||||
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")
|
||||
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")
|
||||
parser.add_argument('configuration_file', metavar='CONFIGFILE',
|
||||
help='the configuration file 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('-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('-d', '--daemon', default=False, dest='daemon', action='store_true',
|
||||
help='run as native daemon (default: false)')
|
||||
|
||||
parser.add_argument("-k", "--kill", default=False, dest="kill", action="store_true",
|
||||
help="kill the daemon if it is running (default: false)")
|
||||
parser.add_argument('-k', '--kill', default=False, dest='kill', action='store_true',
|
||||
help='kill the daemon if it is running (default: false)')
|
||||
|
||||
parser.add_argument("-r", "--restart", default=False, dest="restart", action="store_true",
|
||||
help="restart the daemon if it is running(default: false)")
|
||||
parser.add_argument('-r', '--restart', default=False, dest='restart', action='store_true',
|
||||
help='restart the daemon if it is running(default: false)')
|
||||
|
||||
mode.add_argument("-f", "--foreground", default=False, dest="foreground", action="store_true",
|
||||
help="run looped in foreground (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('--server', default=False, dest='server', action='store_true',
|
||||
help='start http server for status information and statistics')
|
||||
|
||||
"""
|
||||
TODO: maybe make database connection information as args... not sure.
|
||||
parser.add_argument("-b", "--database", metavar="DATABASE", help="database to use")
|
||||
parser.add_argument('-i', '--interval', type=int, help='poll interval in seconds. this '
|
||||
'overrides config file settings. (default: 60)')
|
||||
|
||||
parser.add_argument("-u", "--user", type=str, help="database username")
|
||||
parser.add_argument('-l', '--logfile', metavar='LOGFILE', help='logfile to use')
|
||||
|
||||
parser.add_argument("-p", "--password", type=str, help="database password")
|
||||
"""
|
||||
parser.add_argument('-n', '--logcount', default=5, type=int,
|
||||
help='maximum number of logfiles in rotation (default: 5)')
|
||||
|
||||
parser.add_argument("-l", "--logfile", metavar="LOGFILE", help="logfile to use")
|
||||
parser.add_argument('-m', '--logsize', default=10485760, type=int,
|
||||
help='maximum logfile size in bytes (default: 1048576)')
|
||||
|
||||
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")
|
||||
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('-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('-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('-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.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__))
|
||||
parser.add_argument('--version', action='version', version='%(prog)s ' + str(__version__))
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
|
@ -117,53 +113,57 @@ def main():
|
|||
args = parse_args()
|
||||
|
||||
root_logger = logging.getLogger()
|
||||
|
||||
logger.error("TEST")
|
||||
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")
|
||||
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")
|
||||
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)
|
||||
with open(args.configuration_file, 'r', encoding='utf-8') as configuration_file:
|
||||
configuration_data = configuration_file.read()
|
||||
configuration = Configuration(json.loads(configuration_data))
|
||||
except Exception as err:
|
||||
logger.error(str("uplink: configuration error! {0}".format(err)))
|
||||
logger.error(str('[uplink] configuration error! {0}'.format(err)))
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
config["interval"]
|
||||
except KeyError:
|
||||
# interval not configured in configuration; using default value
|
||||
config["interval"] = 60
|
||||
configuration.set_env_var('_start_date', time.strftime('%Y-%m-%d %H:%M:%S',
|
||||
time.localtime(
|
||||
calendar.timegm(time.gmtime()))))
|
||||
|
||||
# interval set in args is overriding configuration and default
|
||||
if args.interval:
|
||||
# interval set in args is overriding configuration and default
|
||||
config["interval"] = args.interval
|
||||
configuration.set_interval(args.interval)
|
||||
|
||||
pid_file = "/tmp/uplink.pid"
|
||||
u = Uplink(pid_file, config)
|
||||
configuration.set_env_var('_server', False)
|
||||
if args.server:
|
||||
configuration.set_env_var('_server', True)
|
||||
|
||||
try:
|
||||
u = Uplink(configuration.get_pid_file(), configuration)
|
||||
except Exception as err:
|
||||
logger.error(str('[uplink] core error! {0}'.format(err)))
|
||||
sys.exit(1)
|
||||
|
||||
if args.cron:
|
||||
from threading import Thread
|
||||
for i in range(len(config["uplinks"])):
|
||||
t = Thread(target=u.fetch_data, args=(config, i))
|
||||
t.start()
|
||||
for i in range(len(configuration.get_uplinks())):
|
||||
ct = Thread(target=u.fetch_data, daemon=True, args=(i,))
|
||||
ct.start()
|
||||
elif args.daemon:
|
||||
if args.kill:
|
||||
u.stop()
|
||||
|
|
@ -173,19 +173,39 @@ def main():
|
|||
u.start()
|
||||
elif args.foreground:
|
||||
from threading import Thread
|
||||
if configuration.get_env_var('_server'):
|
||||
if configuration.get_httpserver_framework() == 'bottle':
|
||||
from uplink.server_bottle import Server
|
||||
s = Server(configuration)
|
||||
st = Thread(target=s.run, daemon=True)
|
||||
st.start()
|
||||
elif configuration.get_httpserver_framework() == 'cherrypy':
|
||||
from uplink.server_cherrypy import Server
|
||||
s = Server(configuration)
|
||||
st = Thread(target=s.run, daemon=True)
|
||||
st.start()
|
||||
elif configuration.get_httpserver_framework() == 'pyramid':
|
||||
from uplink.server_pyramid import Server
|
||||
s = Server(configuration)
|
||||
st = Thread(target=s.run, daemon=True)
|
||||
st.start()
|
||||
else:
|
||||
logger.error('[uplink] no http framework configured! '
|
||||
'(bottle, cherrypy and pyramid are supported)')
|
||||
|
||||
while True:
|
||||
for i in range(len(config["uplinks"])):
|
||||
t = Thread(target=u.fetch_data, args=(config, i))
|
||||
t.start()
|
||||
for i in range(len(configuration.get_uplinks())):
|
||||
ft = Thread(target=u.fetch_data, daemon=True, args=(i,))
|
||||
ft.start()
|
||||
try:
|
||||
time.sleep(config["interval"])
|
||||
time.sleep(configuration.get_interval())
|
||||
except KeyboardInterrupt:
|
||||
logger.info(str("uplink: program terminated by user!"))
|
||||
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")
|
||||
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__":
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue