From b9a747188e54ddf5cf888addb175bd205fbc6822 Mon Sep 17 00:00:00 2001 From: Amandeep Singh Arora Date: Tue, 16 Aug 2022 20:20:37 +0530 Subject: [PATCH 1/6] FOGL-6665: Changes for maintaining separate log files for fledge services and corresponding support in syslog API Signed-off-by: Amandeep Singh Arora --- python/fledge/services/core/api/support.py | 17 +++++++++++++++++ scripts/extras/fledge_logger.conf | 11 +++++++++++ scripts/extras/fledge_logrotate.conf | 17 +++++++++++++++++ scripts/extras/setup_fledge_logger.sh | 15 +++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 scripts/extras/fledge_logger.conf create mode 100644 scripts/extras/fledge_logrotate.conf create mode 100755 scripts/extras/setup_fledge_logger.sh diff --git a/python/fledge/services/core/api/support.py b/python/fledge/services/core/api/support.py index b50e3d2add..2f7a3ffb75 100644 --- a/python/fledge/services/core/api/support.py +++ b/python/fledge/services/core/api/support.py @@ -160,6 +160,7 @@ async def get_syslog_entries(request): template = __GET_SYSLOG_CMD_TEMPLATE lines = __GET_SYSLOG_TOTAL_MATCHED_LINES non_total_template = __GET_SYSLOG_TEMPLATE_WITH_NON_TOTALS + level = "debug" # default log level if 'level' in request.query and request.query['level'] != '': level = request.query['level'].lower() supported_level = ['info', 'warning', 'error', 'debug'] @@ -192,7 +193,15 @@ async def get_syslog_entries(request): cmd = template.format(valid_source[source], _SYSLOG_FILE, total_lines - offset, limit) else: cmd = non_total_template.format(valid_source[source], _SYSLOG_FILE, offset, limit) + _logger.info("prev cmd={}".format(cmd)) + log_file = os.path.join(_get_logs_dir(), "{}.log".format(level)) + cmd = "tail -n {} {} | head -n {}".format(offset+limit, log_file, limit) + _logger.info("new cmd={}".format(cmd)) + + t1 = datetime.datetime.now() a = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() + t2 = datetime.datetime.now() + _logger.debug('********* Time taken for extracting logs: {} msec'.format((t2 - t1).total_seconds()*1000)) c = [b.decode() for b in a] # Since "a" contains return value in bytes, convert it to string response['logs'] = c except ValueError as err: @@ -212,3 +221,11 @@ def _get_support_dir(): support_dir = os.path.expanduser(_FLEDGE_ROOT + '/data/support') return support_dir + +def _get_logs_dir(): + if _FLEDGE_DATA: + logs_dir = os.path.expanduser(_FLEDGE_DATA + '/logs') + else: + logs_dir = os.path.expanduser(_FLEDGE_ROOT + '/data/logs') + + return logs_dir diff --git a/scripts/extras/fledge_logger.conf b/scripts/extras/fledge_logger.conf new file mode 100644 index 0000000000..9e6664a8d6 --- /dev/null +++ b/scripts/extras/fledge_logger.conf @@ -0,0 +1,11 @@ +# Log Fledge messages to logfiles according to severity + +if ($syslogseverity-text == 'err' or $syslogseverity-text == 'warning' or $syslogseverity-text == 'info' or $syslogseverity-text == 'debug') and $programname contains 'Fledge' then $FLEDGE_ROOT/data/logs/debug.log +if ($syslogseverity-text == 'err' or $syslogseverity-text == 'warning' or $syslogseverity-text == 'info') and $programname contains 'Fledge' then $FLEDGE_ROOT/data/logs/info.log +if ($syslogseverity-text == 'err' or $syslogseverity-text == 'warning') and $programname contains 'Fledge' then $FLEDGE_ROOT/data/logs/warning.log +if ($syslogseverity-text == 'err') and $programname contains 'Fledge' then $FLEDGE_ROOT/data/logs/error.log + +# Uncomment the following to stop logging anything that matches the last rule. +# Doing this will stop logging Fledge log messages to the file +# normally containing syslog messages (eg, /var/log/syslog) +#& stop diff --git a/scripts/extras/fledge_logrotate.conf b/scripts/extras/fledge_logrotate.conf new file mode 100644 index 0000000000..d89ac0bb62 --- /dev/null +++ b/scripts/extras/fledge_logrotate.conf @@ -0,0 +1,17 @@ +# Log rotation config for Fledge logfiles + +$FLEDGE_ROOT/data/logs/error.log +$FLEDGE_ROOT/data/logs/warning.log +$FLEDGE_ROOT/data/logs/info.log +$FLEDGE_ROOT/data/logs/debug.log +{ + rotate 7 + daily + missingok + notifempty + delaycompress + compress + postrotate + /usr/lib/rsyslog/rsyslog-rotate + endscript +} diff --git a/scripts/extras/setup_fledge_logger.sh b/scripts/extras/setup_fledge_logger.sh new file mode 100755 index 0000000000..031d9c29c1 --- /dev/null +++ b/scripts/extras/setup_fledge_logger.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Set FLEDGE_ROOT properly so that Fledge logfiles may be setup correctly in this script +sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" fledge_logger.conf > /tmp/fledge_logger.conf.out +sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" fledge_logrotate.conf > /tmp/fledge_logrotate.conf.out + +sudo mv /tmp/fledge_logger.conf.out /etc/rsyslog.d/fledge.conf +sudo mv /tmp/fledge_logrotate.conf.out /etc/logrotate.d/fledge + +sudo chmod 644 /etc/rsyslog.d/fledge.conf /etc/logrotate.d/fledge +sudo chown -R root:root /etc/rsyslog.d/fledge.conf /etc/logrotate.d/fledge +sudo chown -R root:syslog ${FLEDGE_ROOT}/data/logs + +sudo systemctl restart rsyslog.service + From a915b7293171cbf11055ade402364d62386da5f3 Mon Sep 17 00:00:00 2001 From: Amandeep Singh Arora Date: Thu, 18 Aug 2022 15:42:09 +0530 Subject: [PATCH 2/6] Renamed files Signed-off-by: Amandeep Singh Arora --- scripts/extras/{fledge_logrotate.conf => logrotate.conf} | 0 scripts/extras/{fledge_logger.conf => rsyslog.conf} | 0 .../extras/{setup_fledge_logger.sh => setup_logger.sh} | 8 ++++---- 3 files changed, 4 insertions(+), 4 deletions(-) rename scripts/extras/{fledge_logrotate.conf => logrotate.conf} (100%) rename scripts/extras/{fledge_logger.conf => rsyslog.conf} (100%) rename scripts/extras/{setup_fledge_logger.sh => setup_logger.sh} (52%) diff --git a/scripts/extras/fledge_logrotate.conf b/scripts/extras/logrotate.conf similarity index 100% rename from scripts/extras/fledge_logrotate.conf rename to scripts/extras/logrotate.conf diff --git a/scripts/extras/fledge_logger.conf b/scripts/extras/rsyslog.conf similarity index 100% rename from scripts/extras/fledge_logger.conf rename to scripts/extras/rsyslog.conf diff --git a/scripts/extras/setup_fledge_logger.sh b/scripts/extras/setup_logger.sh similarity index 52% rename from scripts/extras/setup_fledge_logger.sh rename to scripts/extras/setup_logger.sh index 031d9c29c1..a6cc55d588 100755 --- a/scripts/extras/setup_fledge_logger.sh +++ b/scripts/extras/setup_logger.sh @@ -1,11 +1,11 @@ #!/bin/bash # Set FLEDGE_ROOT properly so that Fledge logfiles may be setup correctly in this script -sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" fledge_logger.conf > /tmp/fledge_logger.conf.out -sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" fledge_logrotate.conf > /tmp/fledge_logrotate.conf.out +sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" rsyslog.conf > /tmp/rsyslog.conf.out +sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" logrotate.conf > /tmp/logrotate.conf.out -sudo mv /tmp/fledge_logger.conf.out /etc/rsyslog.d/fledge.conf -sudo mv /tmp/fledge_logrotate.conf.out /etc/logrotate.d/fledge +sudo mv /tmp/rsyslog.conf.out /etc/rsyslog.d/fledge.conf +sudo mv /tmp/logrotate.conf.out /etc/logrotate.d/fledge sudo chmod 644 /etc/rsyslog.d/fledge.conf /etc/logrotate.d/fledge sudo chown -R root:root /etc/rsyslog.d/fledge.conf /etc/logrotate.d/fledge From 8e22786745c058f8e364bed396316752e2b3096e Mon Sep 17 00:00:00 2001 From: Amandeep Singh Arora Date: Thu, 18 Aug 2022 17:14:19 +0530 Subject: [PATCH 3/6] Minor fix Signed-off-by: Amandeep Singh Arora --- python/fledge/services/core/api/support.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/fledge/services/core/api/support.py b/python/fledge/services/core/api/support.py index 2f7a3ffb75..c7fed252ee 100644 --- a/python/fledge/services/core/api/support.py +++ b/python/fledge/services/core/api/support.py @@ -9,6 +9,7 @@ import subprocess import json import logging +import datetime import urllib.parse from pathlib import Path from aiohttp import web @@ -22,7 +23,7 @@ __license__ = "Apache 2.0" __version__ = "${VERSION}" -_logger = logger.setup(__name__, level=logging.INFO) +_logger = logger.setup(__name__, level=logging.DEBUG) _SYSLOG_FILE = '/var/log/syslog' if any(x in platform.platform() for x in ['centos', 'redhat']): From b1a6ffb6f4f85b3614ae12724f3620f5a5becca3 Mon Sep 17 00:00:00 2001 From: Amandeep Singh Arora Date: Thu, 18 Aug 2022 17:43:42 +0530 Subject: [PATCH 4/6] Minor updates Signed-off-by: Amandeep Singh Arora --- python/fledge/services/core/api/support.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python/fledge/services/core/api/support.py b/python/fledge/services/core/api/support.py index c7fed252ee..d0ca57570e 100644 --- a/python/fledge/services/core/api/support.py +++ b/python/fledge/services/core/api/support.py @@ -185,19 +185,20 @@ async def get_syslog_entries(request): 'nontotals'] != '' else "false" if non_totals not in ("true", "false"): raise ValueError('nontotals must either be in True or False.') + + log_file = os.path.join(_get_logs_dir(), "{}.log".format(level)) if non_totals != "true": # Get total lines - cmd = lines.format(valid_source[source], _SYSLOG_FILE) + cmd = lines.format(valid_source[source], log_file) + _logger.debug("cmd 1={}".format(cmd)) t = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() total_lines = int(t[0].decode()) response['count'] = total_lines - cmd = template.format(valid_source[source], _SYSLOG_FILE, total_lines - offset, limit) + cmd = template.format(valid_source[source], log_file, total_lines - offset, limit) + _logger.debug("cmd 2={}".format(cmd)) else: - cmd = non_total_template.format(valid_source[source], _SYSLOG_FILE, offset, limit) - _logger.info("prev cmd={}".format(cmd)) - log_file = os.path.join(_get_logs_dir(), "{}.log".format(level)) - cmd = "tail -n {} {} | head -n {}".format(offset+limit, log_file, limit) - _logger.info("new cmd={}".format(cmd)) + cmd = non_total_template.format(valid_source[source], log_file, offset, limit) + _logger.debug("cmd={}".format(cmd)) t1 = datetime.datetime.now() a = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.readlines() From 2863f0a5ac4ee8f3306c16c49b2f46248121d40c Mon Sep 17 00:00:00 2001 From: Amandeep Singh Arora Date: Thu, 18 Aug 2022 17:45:46 +0530 Subject: [PATCH 5/6] Minor change Signed-off-by: Amandeep Singh Arora --- python/fledge/services/core/api/support.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/fledge/services/core/api/support.py b/python/fledge/services/core/api/support.py index d0ca57570e..abb43e21d8 100644 --- a/python/fledge/services/core/api/support.py +++ b/python/fledge/services/core/api/support.py @@ -23,7 +23,7 @@ __license__ = "Apache 2.0" __version__ = "${VERSION}" -_logger = logger.setup(__name__, level=logging.DEBUG) +_logger = logger.setup(__name__, level=logging.INFO) _SYSLOG_FILE = '/var/log/syslog' if any(x in platform.platform() for x in ['centos', 'redhat']): From 2954fb09374969575eb855bcc2d9fdd89beaa47b Mon Sep 17 00:00:00 2001 From: Amandeep Singh Arora Date: Mon, 22 Aug 2022 11:40:38 +0530 Subject: [PATCH 6/6] Further update Signed-off-by: Amandeep Singh Arora --- python/fledge/services/core/api/support.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/fledge/services/core/api/support.py b/python/fledge/services/core/api/support.py index abb43e21d8..4eef9f8142 100644 --- a/python/fledge/services/core/api/support.py +++ b/python/fledge/services/core/api/support.py @@ -23,7 +23,7 @@ __license__ = "Apache 2.0" __version__ = "${VERSION}" -_logger = logger.setup(__name__, level=logging.INFO) +_logger = logger.setup(__name__, level=logging.DEBUG) _SYSLOG_FILE = '/var/log/syslog' if any(x in platform.platform() for x in ['centos', 'redhat']): @@ -170,15 +170,15 @@ async def get_syslog_entries(request): if level == 'info': template = __GET_SYSLOG_CMD_WITH_INFO_TEMPLATE lines = __GET_SYSLOG_INFO_MATCHED_LINES - non_total_template = __GET_SYSLOG_INFO_TEMPLATE_WITH_NON_TOTALS + # non_total_template = __GET_SYSLOG_INFO_TEMPLATE_WITH_NON_TOTALS elif level == 'warning': template = __GET_SYSLOG_CMD_WITH_WARNING_TEMPLATE lines = __GET_SYSLOG_WARNING_MATCHED_LINES - non_total_template = __GET_SYSLOG_WARNING_TEMPLATE_WITH_NON_TOTALS + # non_total_template = __GET_SYSLOG_WARNING_TEMPLATE_WITH_NON_TOTALS elif level == 'error': template = __GET_SYSLOG_CMD_WITH_ERROR_TEMPLATE lines = __GET_SYSLOG_ERROR_MATCHED_LINES - non_total_template = __GET_SYSLOG_ERROR_TEMPLATE_WITH_NON_TOTALS + # non_total_template = __GET_SYSLOG_ERROR_TEMPLATE_WITH_NON_TOTALS response = {} # nontotals non_totals = request.query['nontotals'].lower() if 'nontotals' in request.query and request.query[