Skip to content

Monitor rsyslog feature flag change and update rsyslog config by restart rsyslog-config service #245

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

Merged
merged 5 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,7 @@ class DeviceMetaCfg(object):
def __init__(self):
self.hostname = ''
self.timezone = None
self.syslog_with_osversion = None

def load(self, dev_meta={}):
# Get hostname initial
Expand All @@ -1431,6 +1432,7 @@ class DeviceMetaCfg(object):

# Load appropriate config
self.timezone = dev_meta.get('localhost', {}).get('timezone')
self.syslog_with_osversion = dev_meta.get('localhost', {}).get('syslog_with_osversion')

def hostname_update(self, data):
"""
Expand Down Expand Up @@ -1491,6 +1493,32 @@ class DeviceMetaCfg(object):
syslog.syslog(syslog.LOG_INFO, 'DeviceMetaCfg: Restart rsyslog after '
'changing timezone')

def rsyslog_config(self, data):
"""
Apply syslog_with_osversion feature flag.
Run the following command in Linux: sudo systemctl restart rsyslog-config
Args:
data: Read table's key's data.
"""
new_syslog_with_osversion = data.get('syslog_with_osversion')
syslog.syslog(syslog.LOG_DEBUG,
f'DeviceMetaCfg: syslog with os version: {new_syslog_with_osversion}')

if new_syslog_with_osversion is None:
syslog.syslog(syslog.LOG_DEBUG,
f'DeviceMetaCfg: syslog with os version feature disabled')
return

if new_syslog_with_osversion == self.syslog_with_osversion:
syslog.syslog(syslog.LOG_DEBUG,
f'DeviceMetaCfg: syslog with os version feature flag does not change')
return

run_cmd(['systemctl', 'restart', 'rsyslog-config'], True, False)
syslog.syslog(syslog.LOG_INFO, 'DeviceMetaCfg: Restart rsyslog-config after '
'feature flag change to {}'.format(new_syslog_with_osversion))


class MgmtIfaceCfg(object):
"""
MgmtIfaceCfg Config Daemon
Expand Down Expand Up @@ -2281,6 +2309,7 @@ class HostConfigDaemon:
syslog.syslog(syslog.LOG_INFO, 'DeviceMeta handler...')
self.devmetacfg.hostname_update(data)
self.devmetacfg.timezone_update(data)
self.devmetacfg.rsyslog_config(data)

def rsyslog_handler(self):
rsyslog_config = self.config_db.get_table(
Expand Down
17 changes: 17 additions & 0 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def test_devicemeta_event(self):
Test handling DEVICE_METADATA events.
1) Hostname reload
1) Timezone reload
1) syslog_with_osversion flag change
"""
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
MockConfigDb.event_queue = [(swsscommon.CFG_DEVICE_METADATA_TABLE_NAME,
Expand Down Expand Up @@ -306,6 +307,22 @@ def test_devicemeta_event(self):
]
mocked_syslog.syslog.assert_has_calls(expected)

daemon.devmetacfg.syslog_with_osversion = "false"
HOSTCFG_DAEMON_CFG_DB["DEVICE_METADATA"]["localhost"]["syslog_with_osversion"] = 'true'
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
with mock.patch('hostcfgd.syslog') as mocked_syslog:
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
mocked_syslog.LOG_INFO = original_syslog.LOG_INFO
try:
daemon.start()
except TimeoutError:
pass

expected = [
call(original_syslog.LOG_INFO, 'DeviceMetaCfg: Restart rsyslog-config after feature flag change to true')
]
mocked_syslog.syslog.assert_has_calls(expected)

def test_mgmtiface_event(self):
"""
Test handling mgmt events.
Expand Down
Loading