Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FOGL-6665 #789

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
33 changes: 26 additions & 7 deletions python/fledge/services/core/api/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
import json
import logging
import datetime
import urllib.parse
from pathlib import Path
from aiohttp import web
Expand All @@ -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']):
Expand Down Expand Up @@ -160,6 +161,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']
Expand All @@ -168,31 +170,40 @@ 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[
'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)
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()
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:
Expand All @@ -212,3 +223,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
17 changes: 17 additions & 0 deletions scripts/extras/logrotate.conf
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions scripts/extras/rsyslog.conf
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions scripts/extras/setup_logger.sh
Original file line number Diff line number Diff line change
@@ -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" rsyslog.conf > /tmp/rsyslog.conf.out
sed "s|\$FLEDGE_ROOT|${FLEDGE_ROOT}|g" logrotate.conf > /tmp/logrotate.conf.out

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
sudo chown -R root:syslog ${FLEDGE_ROOT}/data/logs

sudo systemctl restart rsyslog.service