{"id":13416090,"url":"https://github.com/decryptus/monit-docker","last_synced_at":"2025-09-03T14:32:58.530Z","repository":{"id":54914028,"uuid":"197409316","full_name":"decryptus/monit-docker","owner":"decryptus","description":"Monitor docker containers resources usage and execute docker commands or inside containers ","archived":false,"fork":false,"pushed_at":"2023-01-16T11:31:14.000Z","size":89,"stargazers_count":32,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-15T03:41:11.385Z","etag":null,"topics":["container","docker","management","mmonit","mmonit-docker","monit","monitor","monitoring","python","resources"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/decryptus.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-17T14:49:48.000Z","updated_at":"2024-04-23T13:22:39.000Z","dependencies_parsed_at":"2023-02-10T03:00:42.598Z","dependency_job_id":null,"html_url":"https://github.com/decryptus/monit-docker","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decryptus%2Fmonit-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decryptus%2Fmonit-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decryptus%2Fmonit-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decryptus%2Fmonit-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/decryptus","download_url":"https://codeload.github.com/decryptus/monit-docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231895815,"owners_count":18442361,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["container","docker","management","mmonit","mmonit-docker","monit","monitor","monitoring","python","resources"],"created_at":"2024-07-30T21:00:54.195Z","updated_at":"2024-12-30T18:01:19.720Z","avatar_url":"https://github.com/decryptus.png","language":"Python","readme":"# monit-docker project\n\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/monit-docker.svg)](https://pypi.org/project/monit-docker/)\n[![PyPI version shields.io](https://img.shields.io/pypi/v/monit-docker.svg)](https://pypi.org/project/monit-docker/)\n[![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/decryptus/monit-docker)](https://hub.docker.com/r/decryptus/monit-docker)\n[![Documentation Status](https://readthedocs.org/projects/monit-docker/badge/?version=latest)](https://monit-docker.readthedocs.io/)\n\nmonit-docker is a free and open-source, we develop it to monitor container status or resources\nand execute some commands inside containers or manage containers with dockerd, for example:\n - reload php-fpm if memory usage is too high\n - reload php-fpm if no free space in /dev/shm\n - restart container if status is not running\n - remove all containers\n\n## Table of contents\n1. [Quickstart](#quickstart)\n2. [Installation](#installation)\n3. [Environment variables](#environment_variables)\n4. [Sub-command: monit](#sub-command_monit)\n    1. [Basic commands](#monit_basic_commands)\n    2. [Advanced commands](#monit_advanced_commands)\n    3. [Container informations with exit codes](#monit_container_informations)\n    4. [monit-docker with M/Monit](#monit_with_mmonit)\n5. [Sub-command: stats](#sub-command_stats)\n    1. [Basic commands](#stats_basic_commands)\n    2. [Advanced commands](#stats_advanced_commands)\n\n## \u003ca name=\"quickstart\"\u003e\u003c/a\u003eQuickstart\n\nUsing monit-docker in Docker with crond\n\n`docker-compose up -d`\n\nSee [docker-compose.yml](docker-compose.yml) and MONIT\\_DOCKER\\_CRONS environment variable to configure commands.\n\n## \u003ca name=\"installation\"\u003e\u003c/a\u003eInstallation\n\n`pip install monit-docker`\n\n## \u003ca name=\"environment_variables\"\u003e\u003c/a\u003eEnvironment variables\n\n| Variable                   | Description                 | Default |\n|:---------------------------|:----------------------------|:--------|\n| `MONIT_DOCKER_CONFIG`      | Configuration file contents\u003cbr /\u003e(e.g. `export MONIT_DOCKER_CONFIG=\"$(cat monit-docker.yml)\"`) |  |\n| `MONIT_DOCKER_CONFFILE`    | Configuration file path     | /etc/monit-docker/monit-docker.yml |\n| `MONIT_DOCKER_LOGFILE`     | Log file path               | /var/log/monit-docker/monit-docker.log |\n| `MONIT_DOCKER_RUNTIMEDIR`  | Runtime directory path      | /run/monit-docker |\n\n## \u003ca name=\"sub-command_monit\"\u003e\u003c/a\u003eSub-command: monit\n\n### \u003ca name=\"monit_basic_commands\"\u003e\u003c/a\u003eBasic commands\n\nRestart containers with name starts with foo if memory usage percentage \u003e 60% or cpu usage percentage \u003e 90%:\n\n`monit-docker --name 'foo*' monit --cmd-if 'mem_percent \u003e 60 ? restart' --cmd-if 'cpu_percent \u003e 90 ? restart'`\n\nStop containers with name starts with bar or foo and if cpu usage percentage greater than 60% and less than 70%:\n\n`monit-docker --name 'bar*' --name 'foo*' monit --cmd-if '60 \u003e cpu_percent \u003c 70 ? stop'`\n\nKill containers with name starts with bar and status equal to pause or running:\n\n`monit-docker --name 'bar*' monit --cmd-if 'status in (pause,running) ? kill'`\n\nYou can also use status argument, for example, restart containers with status paused or exited:\n\n`monit-docker -s paused -s exited monit --cmd 'restart'`\n\nGenerate containers pidfile:\n\n`monit-docker monit --rsc pid`\n\nReload php-fpm in container with image name contains /php-fpm/ if memory usage greater than 100 MiB:\n\n`monit-docker --image '*/php-fpm/*' monit --cmd-if 'mem_usage \u003e 100 MiB ? (kill -USR2 1)'`\n\nReload php-fpm in container with image name contains /php-fpm/ if /dev/shm percentage usage greater than 80%:\n\n`monit-docker --image '*/php-fpm/*' monit --cmd '(bash -c \"[ $(df /dev/shm | sed \\\"s/\\%//;\\$!d\\\" | awk \\\"{print \\$5}\\\") -gt 80 ] \u0026\u0026 kill -USR2 1\")'`\n\n### \u003ca name=\"monit_advanced_commands\"\u003e\u003c/a\u003eAdvanced commands with configuration file or environment variable MONIT\\_DOCKER\\_CONFIG\n\n##### Run commands with aliases declared in configuration file (e.g.: [monit-docker.yml.example](etc/monit-docker/monit-docker.yml.example)):\n\nRestart container id 4c01db0b339c if condition alias @status\\_not\\_running is true:\n\n`monit-docker --id 4c01db0b339c monit --cmd-if '@status_not_running ? restart'`\n\nExecute commands alias @start\\_pause containers with name starts with foo if condition alias @status\\_not\\_running is true:\n\n`monit-docker --name 'foo*' monit --cmd-if '@status_not_running ? @start_pause'`\n\nRemove force container group php if status is equal to running:\n\n`monit-docker --ctn-group php monit --cmd-if 'status == running ? @remove_force'`\n\nRestart containers group nodejs if memory usage percentage \u003e 10% and cpu usage percentage \u003e 60%:\n\n`monit-docker --ctn-group nodejs monit --cmd-if '@mem_gt_10pct_and_cpu_gt_60pct ? restart'`\n\nRemove force all containers:\n\n`monit-docker monit --cmd '@remove_force'`\n\n### \u003ca name=\"monit_container_informations\"\u003e\u003c/a\u003eContainer informations with exit codes\n\n##### Container status\n\nRun command below to get status with exit code for container named foo\\_php\\_fpm:\n\n`monit-docker --name foo_php_fpm monit --rsc status`\n\nAn error occurred if exit code is greater than 100.\n\n| Exit code | Description |\n|:----------|:------------|\n| 0         | Running     |\n| 10        | Created     |\n| 20        | Paused      |\n| 30        | Restarting  |\n| 40        | Removing    |\n| 50        | Exited      |\n| 60        | Dead        |\n| 114       | Not found   |\n\n##### Container CPU usage percentage\n\nRun command below to get CPU usage percentage with exit code for container named foo\\_php\\_fpm:\n\n`monit-docker --name foo_php_fpm monit --rsc cpu_percent`\n\nAn error occurred if exit code is greater than 100.\n\n##### Container memory usage percentage\n\nRun command below to get memory usage percentage with exit code for container named foo\\_php\\_fpm:\n\n`monit-docker --name foo_php_fpm monit --rsc mem_percent`\n\nAn error occurred if exit code is greater than 100.\n\n### \u003ca name=\"monit_with_mmonit\"\u003e\u003c/a\u003emonit-docker with M/Monit\n\nWe can also monitoring containers cpu\\_percent and mem\\_percent resources with [M/Monit](https://mmonit.com).\n\n##### Configuration examples\n\n```\ncheck program docker.foo_php_fpm.status with path \"/usr/bin/monit-docker --name foo_php_fpm monit --rsc status\"\n    group monit-docker\n    if status = 114 for 2 cycles then alert # container not found\n    if status != 0 for 2 cycles then exec \"/usr/bin/monit-docker --name foo_php_fpm monit --cmd restart\" # container not running\n\ncheck program docker.foo_php_fpm.cpu with path \"/usr/bin/monit-docker -s running --name foo_php_fpm monit --rsc cpu_percent\"\n    group monit-docker\n    if status \u003e 100 for 2 cycles then alert\n    if status \u003e 70 for 2 cycles then alert\n    if status \u003e 80 for 4 cycles then exec \"/usr/bin/monit-docker --name foo_php_fpm monit --cmd reload\"\n\ncheck program docker.foo_php_fpm.mem with path \"/usr/bin/monit-docker -s running --name foo_php_fpm monit --rsc mem_percent\"\n    group monit-docker\n    if status \u003e 100 for 2 cycles then alert\n    if status \u003e 70 for 2 cycles then alert\n    if status \u003e 80 for 4 cycles then exec \"/usr/bin/monit-docker --name foo_php_fpm monit --cmd '(kill -USR2 1)'\"\n\ncheck program docker.foo_php_fpm.pid with pidfile /run/monit-docker/foo_php_fpm.pid\n    group monit-docker\n    if changed pid then alert\n\n```\n\n## \u003ca name=\"sub-command_stats\"\u003e\u003c/a\u003eSub-command: stats\n\n### \u003ca name=\"stats_basic_commands\"\u003e\u003c/a\u003eBasic commands\n\nGet all resources statistics for all containers in json format:\n\n`monit-docker stats --output json`\n\n```json\n{\n  \"flamboyant_chaplygin\": {\n    \"status\": \"running\",\n    \"mem_percent\": 0.03,\n    \"net_tx\": \"0.0 B\",\n    \"cpu_percent\": 0,\n    \"mem_usage\": \"2.52 MiB\",\n    \"io_read\": \"3.5 MB\",\n    \"io_write\": \"0.0 B\",\n    \"net_rx\": \"25.2 kB\",\n    \"mem_limit\": \"7.27 GiB\",\n    \"pid\": \"3943\"\n  }\n}\n{\n  \"practical_proskuriakova\": {\n    \"status\": \"running\",\n    \"mem_percent\": 0.04,\n    \"net_tx\": \"0.0 B\",\n    \"cpu_percent\": 0,\n    \"mem_usage\": \"2.61 MiB\",\n    \"io_read\": \"24.6 kB\",\n    \"io_write\": \"0.0 B\",\n    \"net_rx\": \"25.0 kB\",\n    \"mem_limit\": \"7.27 GiB\",\n    \"pid\": \"3990\"\n  }\n}\n```\n\nGet all resources statistics for all containers in text format:\n\n`monit-docker stats --output text`\n\n```\nflamboyant_chaplygin|mem_usage:2.52 MiB|mem_limit:7.27 GiB|mem_percent:0.03|cpu_percent:0.0|io_read:3.5 MB|io_write:0.0 B|net_tx:0.0 B|net_rx:43.5 kB|status:running\npractical_proskuriakova|mem_usage:2.61 MiB|mem_limit:7.27 GiB|mem_percent:0.04|cpu_percent:0.0|io_read:24.6 kB|io_write:0.0 B|net_tx:0.0 B|net_rx:43.3 kB|status:running\n```\n\n### \u003ca name=\"stats_advanced_commands\"\u003e\u003c/a\u003eAdvanced commands with configuration file or environment variable MONIT\\_DOCKER\\_CONFIG\n\nGet status and memory usage for group nodejs:\n\n`monit-docker --ctn-group nodejs stats --rsc status --rsc mem_usage`\n","funding_links":[],"categories":["Container Operations"],"sub_categories":["Monitoring"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecryptus%2Fmonit-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdecryptus%2Fmonit-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecryptus%2Fmonit-docker/lists"}