Fix segfault issue inside memory_checker ()

#### Why I did it

Segfault was occuring when running memory_checker

#### How I did it

Deinit publisher immediately after publishing

#### How to verify it

Manual testing
This commit is contained in:
Zain Budhwani 2023-01-24 15:30:41 -08:00 committed by mssonicbld
parent e182d03f57
commit b4e22e2752
2 changed files with 12 additions and 7 deletions
files/image_config/monit
src/sonic-eventd/tools

View File

@ -24,6 +24,7 @@ import subprocess
import sys import sys
import syslog import syslog
import re import re
import time
import docker import docker
@ -58,14 +59,18 @@ def get_command_result(command):
return command_stdout.strip() return command_stdout.strip()
def publish_events(events_handle, container_name, mem_usage_bytes, threshold_value):
def publish_events(container_name, mem_usage_bytes, threshold_value):
events_handle = swsscommon.events_init_publisher(EVENTS_PUBLISHER_SOURCE)
params = swsscommon.FieldValueMap() params = swsscommon.FieldValueMap()
params["ctr_name"] = container_name params["ctr_name"] = container_name
params["mem_usage"] = mem_usage_bytes params["mem_usage"] = mem_usage_bytes
params["threshold"] = threshold_value params["threshold"] = threshold_value
swsscommon.event_publish(events_handle, EVENTS_PUBLISHER_TAG, params) swsscommon.event_publish(events_handle, EVENTS_PUBLISHER_TAG, params)
swsscommon.events_deinit_publisher(events_handle)
def check_memory_usage(events_handle, container_name, threshold_value):
def check_memory_usage(container_name, threshold_value):
"""Checks the memory usage of a container and writes an alerting messages into """Checks the memory usage of a container and writes an alerting messages into
the syslog if the memory usage is larger than the threshold value. the syslog if the memory usage is larger than the threshold value.
@ -100,7 +105,7 @@ def check_memory_usage(events_handle, container_name, threshold_value):
syslog.syslog(syslog.LOG_INFO, "[{}]: Memory usage ({} Bytes) is larger than the threshold ({} Bytes)!" syslog.syslog(syslog.LOG_INFO, "[{}]: Memory usage ({} Bytes) is larger than the threshold ({} Bytes)!"
.format(container_name, mem_usage_bytes, threshold_value)) .format(container_name, mem_usage_bytes, threshold_value))
# publish event # publish event
publish_events(events_handle, container_name, str(mem_usage_bytes), str(threshold_value)) publish_events(container_name, "{:.2f}".format(mem_usage_bytes), str(threshold_value))
sys.exit(3) sys.exit(3)
else: else:
syslog.syslog(syslog.LOG_ERR, "[memory_checker] Failed to retrieve memory value from '{}'" syslog.syslog(syslog.LOG_ERR, "[memory_checker] Failed to retrieve memory value from '{}'"
@ -160,14 +165,13 @@ def main():
sys.exit(0) sys.exit(0)
running_container_names = get_running_container_names() running_container_names = get_running_container_names()
events_handle = swsscommon.events_init_publisher(EVENTS_PUBLISHER_SOURCE)
if args.container_name in running_container_names: if args.container_name in running_container_names:
check_memory_usage(events_handle, args.container_name, args.threshold_value) check_memory_usage(args.container_name, args.threshold_value)
else: else:
syslog.syslog(syslog.LOG_INFO, syslog.syslog(syslog.LOG_INFO,
"[memory_checker] Exits without checking memory usage since container '{}' is not running!" "[memory_checker] Exits without checking memory usage since container '{}' is not running!"
.format(args.container_name)) .format(args.container_name))
swsscommon.events_deinit_publisher(events_handle)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,4 +1,4 @@
from swsscommon.swsscommon import events_init_publisher, event_publish, FieldValueMap from swsscommon.swsscommon import events_init_publisher, events_deinit_publisher, event_publish, FieldValueMap
import time import time
import sys import sys
import ipaddress import ipaddress
@ -92,6 +92,7 @@ def main():
publishBGPEvents(publisher_handle, args.count, args.pause) publishBGPEvents(publisher_handle, args.count, args.pause)
else: else:
publishEventsFromFile(publisher_handle, args.file, args.count, args.pause) publishEventsFromFile(publisher_handle, args.file, args.count, args.pause)
events_deinit_publisher(publisher_handle)
if __name__ == "__main__": if __name__ == "__main__":
main() main()