Implemented logging and configuration files
parent
5c6ecf6838
commit
4ba3cfb279
|
@ -1,3 +1,6 @@
|
||||||
|
# backups
|
||||||
|
*~
|
||||||
|
|
||||||
# script-generated
|
# script-generated
|
||||||
aliases*.json
|
aliases*.json
|
||||||
nodedb/
|
nodedb/
|
||||||
|
|
66
backend.py
66
backend.py
|
@ -11,9 +11,11 @@ Erweiterte Version von Freifunk Pinneberg
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import argparse
|
import argparse
|
||||||
|
import configparser
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import logging, logging.handlers
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
|
@ -29,6 +31,18 @@ from lib.validate import validate_nodeinfos
|
||||||
NODES_VERSION = 1
|
NODES_VERSION = 1
|
||||||
GRAPH_VERSION = 1
|
GRAPH_VERSION = 1
|
||||||
|
|
||||||
|
cfg = {
|
||||||
|
'cfgfile': '/etc/ffmap/ffmap-test.cfg',
|
||||||
|
'logfile': '/var/log/ffmap.log',
|
||||||
|
'loglevel': 5,
|
||||||
|
'dest_dir': '/var/lib/ffmap/mapdata',
|
||||||
|
'aliases': [],
|
||||||
|
'prune': 0,
|
||||||
|
'nodedb': '/var/lib/ffmap/nodedb',
|
||||||
|
'rrd_data': False,
|
||||||
|
'rrd_graphs': False,
|
||||||
|
'redis': False
|
||||||
|
}
|
||||||
|
|
||||||
def main(params):
|
def main(params):
|
||||||
os.makedirs(params['dest_dir'], exist_ok=True)
|
os.makedirs(params['dest_dir'], exist_ok=True)
|
||||||
|
@ -67,7 +81,7 @@ def main(params):
|
||||||
try:
|
try:
|
||||||
with open(nodes_fn, 'r') as nodedb_handle:
|
with open(nodes_fn, 'r') as nodedb_handle:
|
||||||
nodedb = json.load(nodedb_handle)
|
nodedb = json.load(nodedb_handle)
|
||||||
except IOError:
|
except (IOError, ValueError):
|
||||||
nodedb = {'nodes': dict()}
|
nodedb = {'nodes': dict()}
|
||||||
|
|
||||||
# flush nodedb if it uses the old format
|
# flush nodedb if it uses the old format
|
||||||
|
@ -171,6 +185,19 @@ def main(params):
|
||||||
if params['img']:
|
if params['img']:
|
||||||
rrd.update_images()
|
rrd.update_images()
|
||||||
|
|
||||||
|
def set_loglevel(nr):
|
||||||
|
"""
|
||||||
|
Umsetzen der Nummer auf einen für "logging" passenden Wert
|
||||||
|
Die Nummer kann ein Wert zwischen 0 - kein Logging und 5 - Debug sein
|
||||||
|
"""
|
||||||
|
level = (None, logging.CRITICAL, logging.ERROR, logging.WARNING,
|
||||||
|
logging.INFO, logging.DEBUG)
|
||||||
|
if nr > 5:
|
||||||
|
nr = 5
|
||||||
|
elif nr < 0:
|
||||||
|
nr = 0
|
||||||
|
return level[nr]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
# get options from command line
|
# get options from command line
|
||||||
|
@ -188,7 +215,9 @@ if __name__ == '__main__':
|
||||||
'bat0:/run/alfred0.sock.')
|
'bat0:/run/alfred0.sock.')
|
||||||
parser.add_argument('-d', '--dest-dir', action='store',
|
parser.add_argument('-d', '--dest-dir', action='store',
|
||||||
help='Write output to destination directory',
|
help='Write output to destination directory',
|
||||||
required=True)
|
required=False)
|
||||||
|
parser.add_argument('-c', '--config', action='store', metavar='FILE',
|
||||||
|
help='read configuration from FILE')
|
||||||
parser.add_argument('-V', '--vpn', nargs='+', metavar='MAC',
|
parser.add_argument('-V', '--vpn', nargs='+', metavar='MAC',
|
||||||
help='Assume MAC addresses are part of vpn')
|
help='Assume MAC addresses are part of vpn')
|
||||||
parser.add_argument('-p', '--prune', metavar='DAYS', type=int,
|
parser.add_argument('-p', '--prune', metavar='DAYS', type=int,
|
||||||
|
@ -204,4 +233,37 @@ if __name__ == '__main__':
|
||||||
'intensive)')
|
'intensive)')
|
||||||
|
|
||||||
options = vars(parser.parse_args())
|
options = vars(parser.parse_args())
|
||||||
|
if options['config']:
|
||||||
|
cfg['cfgfile'] = options['config']
|
||||||
|
|
||||||
|
config = configparser.ConfigParser(cfg)
|
||||||
|
if config.read(cfg['cfgfile']):
|
||||||
|
if not options['nodedb']:
|
||||||
|
options['nodedb'] = config.get('rrd', 'nodedb')
|
||||||
|
if not options['dest_dir']:
|
||||||
|
options['dest_dir'] = config.get('global', 'dest_dir')
|
||||||
|
if not options['rrd']:
|
||||||
|
options['rrd'] = config.getboolean('rrd', 'enabled')
|
||||||
|
if not options['img']:
|
||||||
|
options['img'] = config.getboolean('rrd', 'graphs')
|
||||||
|
cfg['logfile'] = config.get('global', 'logfile')
|
||||||
|
cfg['loglevel'] = config.getint('global', 'loglevel')
|
||||||
|
|
||||||
|
# At this point global configuration is available. Time to enable logging
|
||||||
|
# Logging is handled by the operating system, so use WatchedFileHandler
|
||||||
|
handler = logging.handlers.WatchedFileHandler(cfg['logfile'])
|
||||||
|
handler.setFormatter(logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s',
|
||||||
|
datefmt='%Y-%m-%d %H:%M:%S'))
|
||||||
|
log = logging.getLogger()
|
||||||
|
log.addHandler(handler)
|
||||||
|
loglevel = set_loglevel(cfg['loglevel'])
|
||||||
|
if loglevel:
|
||||||
|
log.setLevel(loglevel)
|
||||||
|
else:
|
||||||
|
log.disabled = True
|
||||||
|
|
||||||
|
log.info("%s started" % sys.argv[0])
|
||||||
|
if os.path.isfile(cfg['cfgfile']):
|
||||||
|
log.info("using configuration from '%s'" % cfg['cfgfile'])
|
||||||
main(options)
|
main(options)
|
||||||
|
log.info("%s finished" % sys.argv[0])
|
||||||
|
|
Loading…
Reference in New Issue