dockerclustermon 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
110
|
+
.pdm.toml
|
|
111
|
+
.pdm-python
|
|
112
|
+
.pdm-build/
|
|
113
|
+
|
|
114
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
115
|
+
__pypackages__/
|
|
116
|
+
|
|
117
|
+
# Celery stuff
|
|
118
|
+
celerybeat-schedule
|
|
119
|
+
celerybeat.pid
|
|
120
|
+
|
|
121
|
+
# SageMath parsed files
|
|
122
|
+
*.sage.py
|
|
123
|
+
|
|
124
|
+
# Environments
|
|
125
|
+
.env
|
|
126
|
+
.venv
|
|
127
|
+
env/
|
|
128
|
+
venv/
|
|
129
|
+
ENV/
|
|
130
|
+
env.bak/
|
|
131
|
+
venv.bak/
|
|
132
|
+
|
|
133
|
+
# Spyder project settings
|
|
134
|
+
.spyderproject
|
|
135
|
+
.spyproject
|
|
136
|
+
|
|
137
|
+
# Rope project settings
|
|
138
|
+
.ropeproject
|
|
139
|
+
|
|
140
|
+
# mkdocs documentation
|
|
141
|
+
/site
|
|
142
|
+
|
|
143
|
+
# mypy
|
|
144
|
+
.mypy_cache/
|
|
145
|
+
.dmypy.json
|
|
146
|
+
dmypy.json
|
|
147
|
+
|
|
148
|
+
# Pyre type checker
|
|
149
|
+
.pyre/
|
|
150
|
+
|
|
151
|
+
# pytype static type analyzer
|
|
152
|
+
.pytype/
|
|
153
|
+
|
|
154
|
+
# Cython debug symbols
|
|
155
|
+
cython_debug/
|
|
156
|
+
|
|
157
|
+
# PyCharm
|
|
158
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
159
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
160
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
161
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
162
|
+
.idea/
|
|
163
|
+
.vscode/
|
|
164
|
+
|
|
165
|
+
/uv.lock
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Michael Kennedy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: dockerclustermon
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A CLI tool for a live view of your docker containers running on a remote server.
|
|
5
|
+
Project-URL: Homepage, https://github.com/mikeckennedy/dockerclustermon
|
|
6
|
+
Author-email: Michael Kennedy <michael@talkpython.fm>
|
|
7
|
+
License: MIT
|
|
8
|
+
Keywords: Docker,Docker Compose,Monitoring
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: rich>=13.9.4
|
|
18
|
+
Requires-Dist: typer>=0.13.1
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# dockerclustermon - A TUI utility to monitor your docker containers
|
|
22
|
+
|
|
23
|
+
A TUI tool for a live view of your docker containers running on a remote server. Here's a graphic of it running (refreshes every 5 seconds or so automatically):
|
|
24
|
+
|
|
25
|
+

|
|
26
|
+
|
|
27
|
+
Notice that it uses color to communicate outlier values. For example, low CPU is green, middle-of-the-road CPU is cyan, and heavy CPU usage is red. Similarly for memory. The memory limit column reflects the deploy>memory limit in Docker Compose for that container and the percentage reported is for the imposed memory limit rather than the machine physical memory limits.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
To use the tool, it has one command, `dockerstatus` with a shorter alias `ds`. If you are running a set of Docker containers (say via Docker Compose) on server `my-docker-host`, then just run:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
dockerstatus my-docker-host
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
You can optionally pass the username if you're not logging in a `root`. Here is the full help text (thank you Typer):
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
Usage: dockerstatus [OPTIONS] HOST [USERNAME]
|
|
41
|
+
╭─ Arguments ───────────────────────────────────────────────────────────╮
|
|
42
|
+
│ * host TEXT The server DNS name or IP address (e.g. 91.7.5.1│
|
|
43
|
+
│ or google.com). [default: None] [required] │
|
|
44
|
+
│ username [USERNAME] The username of the ssh user for interacting │
|
|
45
|
+
│ with the server. [default: root] │
|
|
46
|
+
╰───────────────────────────────────────────────────────────────────────╯
|
|
47
|
+
╭─ Options ─────────────────────────────────────────────────────────────╮
|
|
48
|
+
│ --help Show this message and exit. │
|
|
49
|
+
╰───────────────────────────────────────────────────────────────────────╯
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
This package is available on PyPI as dockerclustermon. However, it is ideally used as a CLI tool
|
|
55
|
+
and not imported into programs. As such, using **uv** or **pipx** will be most useful. Take your pick:
|
|
56
|
+
|
|
57
|
+
### uv
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
uv tool install dockerclustermon
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Of course this requires that you have
|
|
64
|
+
[uv installed](https://docs.astral.sh/uv/getting-started/installation/)
|
|
65
|
+
and in the path.
|
|
66
|
+
|
|
67
|
+
### pipx
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pipx install dockerclustermon
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
And this requires that you have [pipx installed](https://pipx.pypa.io/stable/installation/)
|
|
74
|
+
and in the path.
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
Compatibility
|
|
78
|
+
-------------
|
|
79
|
+
|
|
80
|
+
Docker Cluster Monitor has been tested against Ubunutu Linux. It should work on any system that
|
|
81
|
+
supports SSH and has the Docker CLI tools installed. Note that it does **not** work on the local
|
|
82
|
+
machine at the moment. PRs are welcome. ;)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# dockerclustermon - A TUI utility to monitor your docker containers
|
|
2
|
+
|
|
3
|
+
A TUI tool for a live view of your docker containers running on a remote server. Here's a graphic of it running (refreshes every 5 seconds or so automatically):
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
Notice that it uses color to communicate outlier values. For example, low CPU is green, middle-of-the-road CPU is cyan, and heavy CPU usage is red. Similarly for memory. The memory limit column reflects the deploy>memory limit in Docker Compose for that container and the percentage reported is for the imposed memory limit rather than the machine physical memory limits.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
To use the tool, it has one command, `dockerstatus` with a shorter alias `ds`. If you are running a set of Docker containers (say via Docker Compose) on server `my-docker-host`, then just run:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
dockerstatus my-docker-host
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can optionally pass the username if you're not logging in a `root`. Here is the full help text (thank you Typer):
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
Usage: dockerstatus [OPTIONS] HOST [USERNAME]
|
|
21
|
+
╭─ Arguments ───────────────────────────────────────────────────────────╮
|
|
22
|
+
│ * host TEXT The server DNS name or IP address (e.g. 91.7.5.1│
|
|
23
|
+
│ or google.com). [default: None] [required] │
|
|
24
|
+
│ username [USERNAME] The username of the ssh user for interacting │
|
|
25
|
+
│ with the server. [default: root] │
|
|
26
|
+
╰───────────────────────────────────────────────────────────────────────╯
|
|
27
|
+
╭─ Options ─────────────────────────────────────────────────────────────╮
|
|
28
|
+
│ --help Show this message and exit. │
|
|
29
|
+
╰───────────────────────────────────────────────────────────────────────╯
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
This package is available on PyPI as dockerclustermon. However, it is ideally used as a CLI tool
|
|
35
|
+
and not imported into programs. As such, using **uv** or **pipx** will be most useful. Take your pick:
|
|
36
|
+
|
|
37
|
+
### uv
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
uv tool install dockerclustermon
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Of course this requires that you have
|
|
44
|
+
[uv installed](https://docs.astral.sh/uv/getting-started/installation/)
|
|
45
|
+
and in the path.
|
|
46
|
+
|
|
47
|
+
### pipx
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pipx install dockerclustermon
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
And this requires that you have [pipx installed](https://pipx.pypa.io/stable/installation/)
|
|
54
|
+
and in the path.
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
Compatibility
|
|
58
|
+
-------------
|
|
59
|
+
|
|
60
|
+
Docker Cluster Monitor has been tested against Ubunutu Linux. It should work on any system that
|
|
61
|
+
supports SSH and has the Docker CLI tools installed. Note that it does **not** work on the local
|
|
62
|
+
machine at the moment. PRs are welcome. ;)
|
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
"""dockerclustermon - A CLI tool for a live view of your docker containers running on a remote server."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.2.0"
|
|
4
|
+
__author__ = "Michael Kennedy <michael@talkpython.fm>"
|
|
5
|
+
__all__ = []
|
|
6
|
+
|
|
7
|
+
import datetime
|
|
8
|
+
import re
|
|
9
|
+
import subprocess
|
|
10
|
+
import time
|
|
11
|
+
from subprocess import CalledProcessError
|
|
12
|
+
from threading import Thread
|
|
13
|
+
from typing import Annotated, Callable, Tuple
|
|
14
|
+
|
|
15
|
+
# noinspection PyPackageRequirements
|
|
16
|
+
import rich.console
|
|
17
|
+
# noinspection PyPackageRequirements
|
|
18
|
+
import rich.live
|
|
19
|
+
# noinspection PyPackageRequirements
|
|
20
|
+
import rich.table
|
|
21
|
+
import typer
|
|
22
|
+
# noinspection PyPackageRequirements
|
|
23
|
+
from rich.text import Text
|
|
24
|
+
|
|
25
|
+
results = {
|
|
26
|
+
"ps": [],
|
|
27
|
+
"stat": [],
|
|
28
|
+
"free": (0.0, 0.0, 0.0),
|
|
29
|
+
"error": None,
|
|
30
|
+
}
|
|
31
|
+
workers = []
|
|
32
|
+
|
|
33
|
+
__host_type = Annotated[
|
|
34
|
+
str,
|
|
35
|
+
typer.Argument(
|
|
36
|
+
help="The server DNS name or IP address (e.g. 91.7.5.1 or google.com)."
|
|
37
|
+
),
|
|
38
|
+
]
|
|
39
|
+
__user_type = Annotated[
|
|
40
|
+
str,
|
|
41
|
+
typer.Argument(
|
|
42
|
+
help="The username of the ssh user for interacting with the server."
|
|
43
|
+
),
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def live_status(host: __host_type, username: __user_type = "root"):
|
|
48
|
+
try:
|
|
49
|
+
print()
|
|
50
|
+
if host == 'version':
|
|
51
|
+
print(f'dockerclustermon monitoring utility version {__version__}.')
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
table = build_table(username, host)
|
|
55
|
+
if not table:
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
with rich.live.Live(table, auto_refresh=False) as live:
|
|
59
|
+
while True:
|
|
60
|
+
table = build_table(username, host)
|
|
61
|
+
live.update(table)
|
|
62
|
+
live.refresh()
|
|
63
|
+
except KeyboardInterrupt:
|
|
64
|
+
for w in workers:
|
|
65
|
+
w.join()
|
|
66
|
+
print("kthxbye!")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def process_results():
|
|
70
|
+
ps_lines: list[dict[str, str]] = results["ps"]
|
|
71
|
+
stat_lines: list[dict[str, str]] = results["stat"]
|
|
72
|
+
total, used, avail = results["free"]
|
|
73
|
+
joined = join_results(ps_lines, stat_lines)
|
|
74
|
+
reduced = reduce_lines(joined)
|
|
75
|
+
total_cpu = total_percent(reduced, "CPU")
|
|
76
|
+
total_mem = total_sizes(reduced, "Mem")
|
|
77
|
+
return reduced, total, total_cpu, total_mem, used
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def run_update(username: str, host: str):
|
|
81
|
+
global workers
|
|
82
|
+
|
|
83
|
+
workers.clear()
|
|
84
|
+
workers.append(
|
|
85
|
+
Thread(target=lambda: run_stat_command(username, host), daemon=True)
|
|
86
|
+
)
|
|
87
|
+
workers.append(
|
|
88
|
+
Thread(target=lambda: run_ps_command(username, host), daemon=True)
|
|
89
|
+
)
|
|
90
|
+
workers.append(
|
|
91
|
+
Thread(target=lambda: run_free_command(username, host), daemon=True)
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
for w in workers:
|
|
95
|
+
w.start()
|
|
96
|
+
for w in workers:
|
|
97
|
+
w.join()
|
|
98
|
+
|
|
99
|
+
if results["error"]:
|
|
100
|
+
raise results["error"]
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def build_table(username: str, host: str):
|
|
104
|
+
# Keys: 'Name', 'Created', 'Status', 'CPU', 'Mem', 'Mem %', 'Limit'
|
|
105
|
+
formatted_date = datetime.datetime.now().strftime("%b %d, %Y @ %I:%M %p")
|
|
106
|
+
table = rich.table.Table(title=f"Docker cluster {host} status {formatted_date}")
|
|
107
|
+
|
|
108
|
+
table.add_column("Name", style="white", no_wrap=True)
|
|
109
|
+
# table.add_column("Created", style="white", no_wrap=True)
|
|
110
|
+
table.add_column("Status", style="green", no_wrap=True)
|
|
111
|
+
table.add_column("CPU %", justify="right", style="white")
|
|
112
|
+
table.add_column("Mem %", justify="right", style="white")
|
|
113
|
+
table.add_column("Mem", justify="right", style="white")
|
|
114
|
+
table.add_column("Limit", justify="right", style="white")
|
|
115
|
+
# noinspection PyBroadException
|
|
116
|
+
try:
|
|
117
|
+
run_update(username, host)
|
|
118
|
+
reduced, total, total_cpu, total_mem, used = process_results()
|
|
119
|
+
except CalledProcessError as cpe:
|
|
120
|
+
print(f"Error: {cpe}")
|
|
121
|
+
return None
|
|
122
|
+
except Exception as x:
|
|
123
|
+
table.add_row("Error", str(x), "", "", "", "")
|
|
124
|
+
time.sleep(1)
|
|
125
|
+
return table
|
|
126
|
+
|
|
127
|
+
for container in reduced:
|
|
128
|
+
table.add_row(
|
|
129
|
+
Text(container["Name"], style="bold"),
|
|
130
|
+
color_text(
|
|
131
|
+
container["Status"],
|
|
132
|
+
lambda t: not any(w in t for w in {"unhealthy", "restart"}),
|
|
133
|
+
),
|
|
134
|
+
color_number(container["CPU"], low=5, mid=25),
|
|
135
|
+
color_number(container["Mem %"], low=25, mid=65),
|
|
136
|
+
container["Mem"],
|
|
137
|
+
container["Limit"],
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
table.add_row()
|
|
141
|
+
table.add_row("Totals", "", f"{total_cpu:,.0f} %", "", f"{total_mem:,.2f} GB", "")
|
|
142
|
+
table.add_row()
|
|
143
|
+
|
|
144
|
+
total_server_mem_pct = used / total * 100
|
|
145
|
+
table.add_row(
|
|
146
|
+
"Server",
|
|
147
|
+
"",
|
|
148
|
+
"",
|
|
149
|
+
f"{total_server_mem_pct:,.0f} %",
|
|
150
|
+
f"{used:,.2f} GB",
|
|
151
|
+
f"{total:,.2f} GB",
|
|
152
|
+
)
|
|
153
|
+
return table
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def color_number(text: str, low: int, mid: int) -> Text:
|
|
157
|
+
num_text = (
|
|
158
|
+
text.replace("%", "").replace("GB", "").replace("MB", "").replace("KB", "")
|
|
159
|
+
)
|
|
160
|
+
num = float(num_text)
|
|
161
|
+
|
|
162
|
+
if num <= low:
|
|
163
|
+
return Text(text, style="green")
|
|
164
|
+
|
|
165
|
+
if num <= mid:
|
|
166
|
+
return Text(text, style="cyan")
|
|
167
|
+
|
|
168
|
+
return Text(text, style="red")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def color_text(text: str, good: Callable) -> Text:
|
|
172
|
+
if good(text):
|
|
173
|
+
return Text(text)
|
|
174
|
+
|
|
175
|
+
return Text(text, style="bold red")
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def run_free_command(username: str, host: str) -> Tuple[float, float, float]:
|
|
179
|
+
try:
|
|
180
|
+
# print("Starting free")
|
|
181
|
+
# Run the program and capture its output
|
|
182
|
+
output = subprocess.check_output(["ssh", f"{username}@{host}", "free -m"])
|
|
183
|
+
|
|
184
|
+
# Convert the output to a string
|
|
185
|
+
output_string = bytes.decode(output, "utf-8")
|
|
186
|
+
|
|
187
|
+
# Convert the string to individual lines
|
|
188
|
+
lines = [
|
|
189
|
+
line.strip() for line in output_string.split("\n") if line and line.strip()
|
|
190
|
+
]
|
|
191
|
+
|
|
192
|
+
# total used free shared buff/cache available
|
|
193
|
+
# Mem: 7937 4257 242 160 3436 3211
|
|
194
|
+
mem_line = lines[1]
|
|
195
|
+
while " " in mem_line:
|
|
196
|
+
mem_line = mem_line.replace(" ", " ")
|
|
197
|
+
|
|
198
|
+
parts = mem_line.split(" ")
|
|
199
|
+
used = int(parts[2]) / 1024
|
|
200
|
+
avail = int(parts[5]) / 1024
|
|
201
|
+
total = int(parts[1]) / 1024
|
|
202
|
+
|
|
203
|
+
t = total, used, avail
|
|
204
|
+
results["free"] = t
|
|
205
|
+
|
|
206
|
+
# print("Free done")
|
|
207
|
+
|
|
208
|
+
return t
|
|
209
|
+
except Exception as x:
|
|
210
|
+
results["error"] = x
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def total_sizes(rows: list[dict[str, str]], key: str) -> float:
|
|
214
|
+
# e.g. 1.5GB, 1.5MB, 1.5KB
|
|
215
|
+
total = 0
|
|
216
|
+
for row in rows:
|
|
217
|
+
value = row[key]
|
|
218
|
+
if "GB" in value:
|
|
219
|
+
value = float(value.replace("GB", ""))
|
|
220
|
+
elif "MB" in value:
|
|
221
|
+
value = float(value.replace("MB", ""))
|
|
222
|
+
value = value / 1024
|
|
223
|
+
elif "KB" in value:
|
|
224
|
+
value = float(value.replace("KB", ""))
|
|
225
|
+
value = value / 1024 / 1024
|
|
226
|
+
total += value
|
|
227
|
+
|
|
228
|
+
return total
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def total_percent(rows: list[dict[str, str]], key: str) -> float:
|
|
232
|
+
# e.g. 50.88%
|
|
233
|
+
total = 0
|
|
234
|
+
for row in rows:
|
|
235
|
+
value = float(row[key].replace("%", ""))
|
|
236
|
+
total += value
|
|
237
|
+
|
|
238
|
+
return total
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def reduce_lines(joined: list[dict[str, str]]) -> list[dict[str, str]]:
|
|
242
|
+
new_lines = []
|
|
243
|
+
# keep_keys = { 'NAME', 'CREATED', 'STATUS', 'CPU %', 'MEM USAGE / LIMIT', 'MEM %'}
|
|
244
|
+
|
|
245
|
+
for j in joined:
|
|
246
|
+
j = split_mem(j)
|
|
247
|
+
reduced = {
|
|
248
|
+
"Name": j["NAME"],
|
|
249
|
+
"Created": j["CREATED"],
|
|
250
|
+
"Status": j["STATUS"],
|
|
251
|
+
"CPU": str(int(float(j["CPU %"].replace("%", "")))) + " %",
|
|
252
|
+
"Mem": j["MEM USAGE"]
|
|
253
|
+
.replace("MB", " MB")
|
|
254
|
+
.replace("GB", " GB")
|
|
255
|
+
.replace(" ", " "),
|
|
256
|
+
"Mem %": str(int(float(j["MEM %"].replace("%", "")))) + " %",
|
|
257
|
+
"Limit": j["MEM LIMIT"]
|
|
258
|
+
.replace("MB", " MB")
|
|
259
|
+
.replace("GB", " GB")
|
|
260
|
+
.replace(" ", " "),
|
|
261
|
+
}
|
|
262
|
+
new_lines.append(reduced)
|
|
263
|
+
|
|
264
|
+
# Sort by uptime (youngest first), then by name.
|
|
265
|
+
new_lines.sort(
|
|
266
|
+
key=lambda d: (
|
|
267
|
+
get_seconds_key_from_string(d.get("Status", "")),
|
|
268
|
+
d.get("Name", "").lower().strip(),
|
|
269
|
+
)
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
return new_lines
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def split_mem(j: dict) -> dict:
|
|
276
|
+
key = "MEM USAGE / LIMIT"
|
|
277
|
+
# Example: 781.5MiB / 1.5GiB
|
|
278
|
+
value = j[key]
|
|
279
|
+
parts = [v.strip() for v in value.split("/")]
|
|
280
|
+
|
|
281
|
+
j["MEM USAGE"] = parts[0].replace("iB", "B")
|
|
282
|
+
j["MEM LIMIT"] = parts[1].replace("iB", "B")
|
|
283
|
+
|
|
284
|
+
return j
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def join_results(ps_lines, stat_lines) -> list[dict[str, str]]:
|
|
288
|
+
join_on = "NAME"
|
|
289
|
+
|
|
290
|
+
joined_lines = []
|
|
291
|
+
ps_dict: dict[str, str]
|
|
292
|
+
stat_lines: dict[str, str]
|
|
293
|
+
|
|
294
|
+
for ps_dict, stat_dict in zip(ps_lines, stat_lines):
|
|
295
|
+
if ps_dict[join_on] != stat_dict[join_on]:
|
|
296
|
+
raise Exception("Lines do not match")
|
|
297
|
+
|
|
298
|
+
joined = ps_dict.copy()
|
|
299
|
+
joined.update(**stat_dict)
|
|
300
|
+
|
|
301
|
+
joined_lines.append(joined)
|
|
302
|
+
|
|
303
|
+
return joined_lines
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def run_stat_command(username: str, host: str) -> list[dict[str, str]]:
|
|
307
|
+
# noinspection PyBroadException
|
|
308
|
+
try:
|
|
309
|
+
# print("Starring stat")
|
|
310
|
+
# Run the program and capture its output
|
|
311
|
+
output = subprocess.check_output(
|
|
312
|
+
["ssh", f"{username}@{host}", "docker stats --no-stream"]
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
# Convert the output to a string
|
|
316
|
+
output_string = bytes.decode(output, "utf-8")
|
|
317
|
+
|
|
318
|
+
# Convert the string to individual lines
|
|
319
|
+
lines = [
|
|
320
|
+
line.strip() for line in output_string.split("\n") if line and line.strip()
|
|
321
|
+
]
|
|
322
|
+
|
|
323
|
+
header = parse_stat_header(lines[0])
|
|
324
|
+
# print(header)
|
|
325
|
+
|
|
326
|
+
entries = []
|
|
327
|
+
for line in lines[1:]:
|
|
328
|
+
entries.append(parse_line(line, header))
|
|
329
|
+
|
|
330
|
+
results["stat"] = entries
|
|
331
|
+
|
|
332
|
+
# print("Done with stat")
|
|
333
|
+
return entries
|
|
334
|
+
except CalledProcessError as e:
|
|
335
|
+
results["error"] = e
|
|
336
|
+
except Exception as x:
|
|
337
|
+
results["error"] = x
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def parse_free_header(header_text: str) -> list[Tuple[str, int]]:
|
|
341
|
+
names = ["system", "used", "free", "shared", "buff/cache", "available"]
|
|
342
|
+
positions = []
|
|
343
|
+
for n in names:
|
|
344
|
+
idx = header_text.index(n)
|
|
345
|
+
item = (n, idx)
|
|
346
|
+
positions.append(item)
|
|
347
|
+
|
|
348
|
+
return positions
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def parse_stat_header(header_text: str) -> list[Tuple[str, int]]:
|
|
352
|
+
names = [
|
|
353
|
+
"CONTAINER ID",
|
|
354
|
+
"NAME",
|
|
355
|
+
"CPU %",
|
|
356
|
+
"MEM USAGE / LIMIT",
|
|
357
|
+
"MEM %",
|
|
358
|
+
"NET I/O",
|
|
359
|
+
"BLOCK I/O",
|
|
360
|
+
"PIDS",
|
|
361
|
+
]
|
|
362
|
+
positions = []
|
|
363
|
+
for n in names:
|
|
364
|
+
idx = header_text.index(n)
|
|
365
|
+
item = (n, idx)
|
|
366
|
+
positions.append(item)
|
|
367
|
+
|
|
368
|
+
return positions
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def run_ps_command(username: str, host: str) -> list[dict[str, str]]:
|
|
372
|
+
try:
|
|
373
|
+
# print("Starting ps ...")
|
|
374
|
+
# Run the program and capture its output
|
|
375
|
+
output = subprocess.check_output(["ssh", f"{username}@{host}", "docker ps"])
|
|
376
|
+
|
|
377
|
+
# Convert the output to a string
|
|
378
|
+
output_string = bytes.decode(output, "utf-8")
|
|
379
|
+
|
|
380
|
+
# Convert the string to individual lines
|
|
381
|
+
lines = [
|
|
382
|
+
line.strip() for line in output_string.split("\n") if line and line.strip()
|
|
383
|
+
]
|
|
384
|
+
|
|
385
|
+
header = parse_ps_header(lines[0])
|
|
386
|
+
# print(header)
|
|
387
|
+
|
|
388
|
+
entries = []
|
|
389
|
+
for line in lines[1:]:
|
|
390
|
+
entries.append(parse_line(line, header))
|
|
391
|
+
|
|
392
|
+
results["ps"] = entries
|
|
393
|
+
# print("Done with ps")
|
|
394
|
+
return entries
|
|
395
|
+
except Exception as x:
|
|
396
|
+
results["error"] = x
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def parse_line(line: str, header: list[Tuple[str, int]]) -> dict[str, str]:
|
|
400
|
+
local_results = {}
|
|
401
|
+
tmp_headers = header + [("END", 100000)]
|
|
402
|
+
total_len = 0
|
|
403
|
+
for (name, idx), (_, next_idx) in zip(tmp_headers[:-1], tmp_headers[1:]):
|
|
404
|
+
total_len += idx
|
|
405
|
+
|
|
406
|
+
# print("Going from {} to {}".format(idx, next_idx))
|
|
407
|
+
value = line[idx:next_idx].strip()
|
|
408
|
+
# print(name + ' -> ' + value)
|
|
409
|
+
if name == "NAMES":
|
|
410
|
+
name = "NAME"
|
|
411
|
+
local_results[name] = value
|
|
412
|
+
|
|
413
|
+
return local_results
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
def parse_ps_header(header_text: str) -> list[Tuple[str, int]]:
|
|
417
|
+
names = ["CONTAINER ID", "IMAGE", "COMMAND", "CREATED", "STATUS", "PORTS", "NAMES"]
|
|
418
|
+
positions = []
|
|
419
|
+
for n in names:
|
|
420
|
+
idx = header_text.index(n)
|
|
421
|
+
item = (n, idx)
|
|
422
|
+
positions.append(item)
|
|
423
|
+
|
|
424
|
+
return positions
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def get_seconds_key_from_string(uptime_str: str) -> int:
|
|
428
|
+
if match := re.search(r"(\d+) second", uptime_str):
|
|
429
|
+
dt = int(match.group(1))
|
|
430
|
+
return dt
|
|
431
|
+
|
|
432
|
+
if re.search(r"About a minute", uptime_str):
|
|
433
|
+
return 60
|
|
434
|
+
|
|
435
|
+
if match := re.search(r"(\d+) minute", uptime_str):
|
|
436
|
+
dt = int(match.group(1))
|
|
437
|
+
return dt * 60
|
|
438
|
+
|
|
439
|
+
if re.search(r"About an hour", uptime_str):
|
|
440
|
+
return 60 * 60
|
|
441
|
+
|
|
442
|
+
if match := re.search(r"(\d+) hour", uptime_str):
|
|
443
|
+
dt = int(match.group(1))
|
|
444
|
+
return dt * 60 * 60
|
|
445
|
+
|
|
446
|
+
if re.search(r"About a day", uptime_str):
|
|
447
|
+
return 60 * 60 * 24
|
|
448
|
+
|
|
449
|
+
if match := re.search(r"(\d+) day", uptime_str):
|
|
450
|
+
dt = int(match.group(1))
|
|
451
|
+
return dt * 60 * 60 * 24
|
|
452
|
+
|
|
453
|
+
return 1_000_000
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def run_live_status():
|
|
457
|
+
typer.run(live_status)
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
if __name__ == "__main__":
|
|
461
|
+
run_live_status()
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dockerclustermon"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "A CLI tool for a live view of your docker containers running on a remote server."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"rich>=13.9.4",
|
|
9
|
+
"typer>=0.13.1",
|
|
10
|
+
]
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Michael Kennedy", email = "michael@talkpython.fm" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["Docker", "Docker Compose", "Monitoring"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13"
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
"Homepage" = "https://github.com/mikeckennedy/dockerclustermon"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
ds = "dockerclustermon:run_live_status"
|
|
33
|
+
dockerstatus = "dockerclustermon:run_live_status"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["hatchling>=1.21.0", "hatch-vcs>=0.3.0"]
|
|
38
|
+
build-backend = "hatchling.build"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.sdist]
|
|
42
|
+
packages = ["dockerclustermon"]
|
|
43
|
+
exclude = [
|
|
44
|
+
"/.github",
|
|
45
|
+
"/dist",
|
|
46
|
+
"/tests",
|
|
47
|
+
"/example",
|
|
48
|
+
"/readme_resources",
|
|
49
|
+
"/venv",
|
|
50
|
+
"/.venv",
|
|
51
|
+
"tox.ini",
|
|
52
|
+
"uv.lock",
|
|
53
|
+
"ruff.toml",
|
|
54
|
+
"requirements-dev.txt",
|
|
55
|
+
"settings.json",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[tool.hatch.build.targets.wheel]
|
|
59
|
+
packages = ["dockerclustermon"]
|
|
60
|
+
exclude = [
|
|
61
|
+
"/.github",
|
|
62
|
+
"/dist",
|
|
63
|
+
"/tests",
|
|
64
|
+
"/example",
|
|
65
|
+
"/venv",
|
|
66
|
+
"/readme_resources",
|
|
67
|
+
"/.venv",
|
|
68
|
+
"tox.ini",
|
|
69
|
+
"uv.lock",
|
|
70
|
+
"ruff.toml",
|
|
71
|
+
"requirements-dev.txt",
|
|
72
|
+
"settings.json",
|
|
73
|
+
]
|