From ee233e65e0bc49e675312396d03ee617cb744c1c Mon Sep 17 00:00:00 2001 From: berhsi Date: Wed, 11 Sep 2019 21:24:28 +0200 Subject: [PATCH] statusd.py: default verbosity value can changed verbosity value can now changed with a entry in statusd.conf --- statusd.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/statusd.py b/statusd.py index deb876b..8a5ba7f 100755 --- a/statusd.py +++ b/statusd.py @@ -34,7 +34,6 @@ def read_config(CONFIGFILE, CONFIG): key = strip_argument(key).upper() value = strip_argument(value) CONFIG[key] = value - logging.debug('Set {} to {}'.format(key, value)) else: logging.error('Failed to read {}'.format(CONFIGFILE)) logging.error('Using default values') @@ -189,6 +188,28 @@ def set_values(raw_data): return (status, timestamp) +def read_loglevel(CONFIG): + ''' + The function translates the value string from config verbosity option to + a valid logging option. + param1: dictionary + return: boolean or integer + ''' + if CONFIG['VERBOSITY'] == 'critical': + loglevel = logging.CRITICAL + elif CONFIG['VERBOSITY'] == 'error': + loglevel = logging.ERROR + elif CONFIG['VERBOSITY'] == 'warning': + loglevel = logging.WARNING + elif CONFIG['VERBOSITY'] == 'info': + loglevel = logging.INFO + elif CONFIG['VERBOSITY'] == 'debug': + loglevel = logging.DEBUG + else: loglevel = False + print(loglevel) + return(loglevel) + + def main(): ''' The main function - opens a socket, create a ssl context, load certs and @@ -197,8 +218,12 @@ def main(): PROTOCOL_TLS: only use tls OP_NO_COMPRESSION: prevention against crime attack OP_DONT_ISERT_EMPTY_FRAGMENTS: prevention agains cbc 4 attack (cve-2011-3389) - ''' + + loglevel = logging.WARNING + formatstring = '%(asctime)s: %(levelname)s: %(message)s' + logging.basicConfig(format=formatstring, level=loglevel) + CONFIG = { 'HOST': 'localhost', 'PORT': 10001, @@ -208,13 +233,15 @@ def main(): 'TIMEOUT': 3.0, 'API': './api', 'API_TEMPLATE': './api_template', - 'VERBOSITY': 'info' + 'VERBOSITY': 'warning' } CONFIG_FILE = './statusd.conf' - - loglevel = logging.INFO - logging.basicConfig(format='%(levelname)s: %(message)s', level=loglevel) read_config(CONFIG_FILE, CONFIG) + loglevel = read_loglevel(CONFIG) + if loglevel != False: + logger = logging.getLogger() + logger.setLevel(loglevel) + print_config(CONFIG) if certs_readable(CONFIG) == False: