|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +memory_checker |
| 5 | +
|
| 6 | +This script is part of the feature which will restart the container if memory |
| 7 | +usage of it is larger than the threshold value. |
| 8 | +
|
| 9 | +This script is used to check the memory usage of specified cotnainer and |
| 10 | +is intended to be run by Monit. It will write an alerting message into |
| 11 | +syslog if memory usage of the container is larger than the threshold value for X |
| 12 | +times within Y cycles/minutes. Note that if print(...) statement in this script |
| 13 | +was executed, the string in it will be appended to Monit syslog messages. |
| 14 | +
|
| 15 | +The following is an example in Monit configuration file to show how Monit will run |
| 16 | +this script: |
| 17 | +
|
| 18 | +check program container_memory_<container_name> with path "/usr/bin/memory_checker <container_name> <threshold_value>" |
| 19 | + if status == 3 for X times within Y cycles exec "/usr/bin/restart_service <container_name>" |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import subprocess |
| 24 | +import sys |
| 25 | +import syslog |
| 26 | +import re |
| 27 | + |
| 28 | + |
| 29 | +def get_command_result(command): |
| 30 | + """Executes the command and return the resulting output. |
| 31 | +
|
| 32 | + Args: |
| 33 | + command: A string contains the command to be executed. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + A string which contains the output of command. |
| 37 | + """ |
| 38 | + command_stdout = "" |
| 39 | + |
| 40 | + try: |
| 41 | + proc_instance = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 42 | + shell=True, universal_newlines=True) |
| 43 | + command_stdout, command_stderr = proc_instance.communicate() |
| 44 | + if proc_instance.returncode != 0: |
| 45 | + syslog.syslog(syslog.LOG_ERR, "[memory_checker] Failed to execute the command '{}'. Return code: '{}'" |
| 46 | + .format(command, proc_instance.returncode)) |
| 47 | + sys.exit(1) |
| 48 | + except (OSError, ValueError) as err: |
| 49 | + syslog.syslog(syslog.LOG_ERR, "[memory_checker] Failed to execute the command '{}'. Error: '{}'" |
| 50 | + .format(command, err)) |
| 51 | + sys.exit(2) |
| 52 | + |
| 53 | + return command_stdout.strip() |
| 54 | + |
| 55 | + |
| 56 | +def check_memory_usage(container_name, threshold_value): |
| 57 | + """Checks the memory usage of a container and writes an alerting messages into |
| 58 | + the syslog if the memory usage is larger than the threshold value. |
| 59 | +
|
| 60 | + Args: |
| 61 | + container_name: A string represtents name of a container |
| 62 | + threshold_value: An integer indicates the threshold value (Bytes) of memory usage. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + None. |
| 66 | + """ |
| 67 | + command = "docker stats --no-stream --format \{{\{{.MemUsage\}}\}} {}".format(container_name) |
| 68 | + command_stdout = get_command_result(command) |
| 69 | + mem_usage = command_stdout.split("/")[0].strip() |
| 70 | + match_obj = re.match(r"\d+\.?\d*", mem_usage) |
| 71 | + if match_obj: |
| 72 | + mem_usage_value = float(mem_usage[match_obj.start():match_obj.end()]) |
| 73 | + mem_usage_unit = mem_usage[match_obj.end():] |
| 74 | + |
| 75 | + mem_usage_bytes = 0.0 |
| 76 | + if mem_usage_unit == "B": |
| 77 | + mem_usage_bytes = mem_usage_value |
| 78 | + elif mem_usage_unit == "KiB": |
| 79 | + mem_usage_bytes = mem_usage_value * 1024 |
| 80 | + elif mem_usage_unit == "MiB": |
| 81 | + mem_usage_bytes = mem_usage_value * 1024 ** 2 |
| 82 | + elif mem_usage_unit == "GiB": |
| 83 | + mem_usage_bytes = mem_usage_value * 1024 ** 3 |
| 84 | + |
| 85 | + if mem_usage_bytes > threshold_value: |
| 86 | + print("[{}]: Memory usage ({} Bytes) is larger than the threshold ({} Bytes)!" |
| 87 | + .format(container_name, mem_usage_bytes, threshold_value)) |
| 88 | + sys.exit(3) |
| 89 | + else: |
| 90 | + syslog.syslog(syslog.LOG_ERR, "[memory_checker] Failed to retrieve memory value from '{}'" |
| 91 | + .format(mem_usage)) |
| 92 | + sys.exit(4) |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + parser = argparse.ArgumentParser(description="Check memory usage of a container \ |
| 97 | + and an alerting message will be written into syslog if memory usage \ |
| 98 | + is larger than the threshold value", usage="/usr/bin/memory_checker <container_name> <threshold_value_in_bytes>") |
| 99 | + parser.add_argument("container_name", help="container name") |
| 100 | + # TODO: Currently the threshold value is hard coded as a command line argument and will |
| 101 | + # remove this in the new version since we want to read this value from 'CONFIG_DB'. |
| 102 | + parser.add_argument("threshold_value", type=int, help="threshold value in bytes") |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + check_memory_usage(args.container_name, args.threshold_value) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments