Skip to content

Commit

Permalink
Add log rotation configuration mechanism
Browse files Browse the repository at this point in the history
* Handle changes in LOGGING table

Signed-off-by: Yevhen Fastiuk <yfastiuk@nvidia.com>
  • Loading branch information
fastiuk committed Jul 22, 2023
1 parent 11a44d5 commit 6a159b0
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,43 @@ class FipsCfg(object):
syslog.syslog(syslog.LOG_INFO, f'FipsCfg: update the FIPS enforce option {self.enforce}.')
loader.set_fips(image, self.enforce)

class LoggingCfg(object):
"""Logging Config Daemon
Handles changes in LOGGING table.
1) Handle change of debug/syslog log files config
"""
def __init__(self):
self.cache = {}

def load(self, logging_cfg={}):
# Get initial logging file configuration
self.cache = logging_cfg
syslog.syslog(syslog.LOG_DEBUG, f'Initial logging config: {self.cache}')

def update_logging_cfg(self, key, data):
"""Apply logging configuration
The daemon restarts logrotate-config which will regenerate logrotate
config files.
Args:
key: DB table's key that was triggered change (basically it is a
config file)
data: File's config data
"""
syslog.syslog(syslog.LOG_DEBUG, 'LoggingCfg: logging files cfg update')
if self.cache.get(key) != data:
syslog.syslog(syslog.LOG_INFO,
f'Set logging file {key} config: {data}')
try:
run_cmd('sudo systemctl restart logrotate-config', True, True)
except Exception:
syslog.syslog(syslog.LOG_ERR, f'Failed to update {key} message')
return

# Update cache
self.cache[key] = data

class HostConfigDaemon:
def __init__(self):
self.state_db_conn = DBConnector(STATE_DB, 0)
Expand Down Expand Up @@ -1952,6 +1989,9 @@ class HostConfigDaemon:
# Initialize FipsCfg
self.fipscfg = FipsCfg(self.state_db_conn)

# Initialize LoggingCfg
self.loggingcfg = LoggingCfg()

def load(self, init_data):
features = init_data['FEATURE']
aaa = init_data['AAA']
Expand All @@ -1972,6 +2012,7 @@ class HostConfigDaemon:
syslog_srv = init_data.get(swsscommon.CFG_SYSLOG_SERVER_TABLE_NAME, {})
dns = init_data.get('DNS_NAMESERVER', {})
fips_cfg = init_data.get('FIPS', {})
logging = init_data.get('LOGGING', {})

self.feature_handler.sync_state_field(features)
self.aaacfg.load(aaa, tacacs_global, tacacs_server, radius_global, radius_server)
Expand All @@ -1982,10 +2023,10 @@ class HostConfigDaemon:
self.sshscfg.load(ssh_server)
self.devmetacfg.load(dev_meta)
self.mgmtifacecfg.load(mgmt_ifc, mgmt_vrf)

self.rsyslogcfg.load(syslog_cfg, syslog_srv)
self.dnscfg.load(dns)
self.fipscfg.load(fips_cfg)
self.loggingcfg.load(logging)

# Update AAA with the hostname
self.aaacfg.hostname_update(self.devmetacfg.hostname)
Expand Down Expand Up @@ -2114,6 +2155,10 @@ class HostConfigDaemon:
data = self.config_db.get_table("FIPS")
self.fipscfg.fips_handler(data)

def logging_handler(self, key, op, data):
syslog.syslog(syslog.LOG_INFO, 'LOGGING table handler...')
self.loggingcfg.update_logging_cfg(key, data)

def wait_till_system_init_done(self):
# No need to print the output in the log file so using the "--quiet"
# flag
Expand Down Expand Up @@ -2173,6 +2218,10 @@ class HostConfigDaemon:
# Handle FIPS changes
self.config_db.subscribe('FIPS', make_callback(self.fips_config_handler))

# Handle LOGGING changes
self.config_db.subscribe(swsscommon.CFG_LOGGING_TABLE_NAME,
make_callback(self.logging_handler))

syslog.syslog(syslog.LOG_INFO,
"Waiting for systemctl to finish initialization")
self.wait_till_system_init_done()
Expand Down

0 comments on commit 6a159b0

Please sign in to comment.