Skip to content

Commit 4487a96

Browse files
yozhao101qiluo-msft
authored andcommitted
[memory_checker] Do not check memory usage of containers which are not created (#11129)
Signed-off-by: Yong Zhao [email protected] Why I did it This PR aims to fix an issue (#10088) by enhancing the script memory_checker. Specifically, if container is not created successfully during device is booted/rebooted, then memory_checker do not need check its memory usage. How I did it In the script memory_checker, a function is added to get names of running containers. If the specified container name is not in current running container list, then this script will exit without checking its memory usage. How to verify it I tested on a lab device by following the steps: Stops telemetry container with command sudo systemctl stop telemetry.service Removes telemetry container with command docker rm telemetry Checks whether the script memory_checker ran by Monit will generate the syslog message saying it will exit without checking memory usage of telemetry.
1 parent d15a484 commit 4487a96

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

files/image_config/monit/memory_checker

+32-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import sys
2525
import syslog
2626
import re
2727

28+
import docker
29+
2830

2931
def get_command_result(command):
3032
"""Executes the command and return the resulting output.
@@ -86,14 +88,36 @@ def check_memory_usage(container_name, threshold_value):
8688
print("[{}]: Memory usage ({} Bytes) is larger than the threshold ({} Bytes)!"
8789
.format(container_name, mem_usage_bytes, threshold_value))
8890
syslog.syslog(syslog.LOG_INFO, "[{}]: Memory usage ({} Bytes) is larger than the threshold ({} Bytes)!"
89-
.format(container_name, mem_usage_bytes, threshold_value))
91+
.format(container_name, mem_usage_bytes, threshold_value))
9092
sys.exit(3)
9193
else:
9294
syslog.syslog(syslog.LOG_ERR, "[memory_checker] Failed to retrieve memory value from '{}'"
9395
.format(mem_usage))
9496
sys.exit(4)
9597

9698

99+
def get_running_container_names():
100+
"""Retrieves names of running containers by talking to the docker daemon.
101+
102+
Args:
103+
None.
104+
105+
Returns:
106+
running_container_names: A list indicates names of running containers.
107+
"""
108+
try:
109+
docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock')
110+
running_container_list = docker_client.containers.list(filters={"status": "running"})
111+
running_container_names = [ container.name for container in running_container_list ]
112+
except (docker.errors.APIError, docker.errors.DockerException) as err:
113+
syslog.syslog(syslog.LOG_ERR,
114+
"Failed to retrieve the running container list from docker daemon! Error message is: '{}'"
115+
.format(err))
116+
sys.exit(5)
117+
118+
return running_container_names
119+
120+
97121
def main():
98122
parser = argparse.ArgumentParser(description="Check memory usage of a container \
99123
and an alerting message will be written into syslog if memory usage \
@@ -104,7 +128,13 @@ def main():
104128
parser.add_argument("threshold_value", type=int, help="threshold value in bytes")
105129
args = parser.parse_args()
106130

107-
check_memory_usage(args.container_name, args.threshold_value)
131+
running_container_names = get_running_container_names()
132+
if args.container_name in running_container_names:
133+
check_memory_usage(args.container_name, args.threshold_value)
134+
else:
135+
syslog.syslog(syslog.LOG_INFO,
136+
"[memory_checker] Exits without checking memory usage since container '{}' is not running!"
137+
.format(args.container_name))
108138

109139

110140
if __name__ == "__main__":

0 commit comments

Comments
 (0)