Skip to content

Commit

Permalink
[memory_checker] If containers are not created during
Browse files Browse the repository at this point in the history
device is (re)booted, then exits without checking its memory usage.

Signed-off-by: Yong Zhao <yozhao@microsoft.com>
  • Loading branch information
yozhao101 committed Jun 14, 2022
1 parent a477dbb commit add2fbf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion files/image_config/monit/memory_checker
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ def check_memory_usage(container_name, threshold_value):
sys.exit(4)


def get_running_container_names():
"""Retrieves names of running containers by talking to the docker daemon.
Args:
None.
Returns:
running_container_names: A list indicates names of running containers.
"""
running_container_names = []
docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock')
container_obj = docker_client.containers
try:
container_list = container_obj.list(filters={"status": "running"})
for container in container_list:
running_container_names.append(container.name)
except docker.errors.APIError as err:
print("Failed to retrieve the running container list. Error: '{}'".format(err))

return running_container_names


def main():
parser = argparse.ArgumentParser(description="Check memory usage of a container \
and an alerting message will be written into syslog if memory usage \
Expand All @@ -104,7 +126,12 @@ def main():
parser.add_argument("threshold_value", type=int, help="threshold value in bytes")
args = parser.parse_args()

check_memory_usage(args.container_name, args.threshold_value)
running_container_names = get_running_container_names()
if args.container_name in running_container_names:
check_memory_usage(args.container_name, args.threshold_value)
else:
syslog.syslog(syslog.LOG_INFO,
"[memory_checker] Exits without checking memory usage since contain '{}' is not running!".format(args.container_name))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-utilities

0 comments on commit add2fbf

Please sign in to comment.