-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaybar_docker_status.py
More file actions
executable file
·95 lines (79 loc) · 2.65 KB
/
Copy pathwaybar_docker_status.py
File metadata and controls
executable file
·95 lines (79 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# Author: Sadykov Miron <MironSadykov@yandex.ru>
# License: MIT
# 2025
"""
Python rewrite of some bash script i found.
This is a Python script for Waybar that displays information
about running Docker containers.
If any containers are running,
the script shows their count along with CPU
and memory usage stats for each container in a formatted table.
If Docker is not running, it outputs an appropriate status message.
"""
import json
import subprocess
def count_running_containers() -> int:
try:
containers = subprocess.run(
["docker", "ps", "-q"],
capture_output=True,
text=True,
check=True,
)
return count_continers(containers)
except subprocess.CalledProcessError:
return False
except FileNotFoundError:
# There is no docker in system.
return False
def count_continers(containers: subprocess.CompletedProcess):
return len(containers.stdout.splitlines())
def get_containers() -> list[dict]:
"""Get a list of container stats as dictionaries."""
try:
result = subprocess.run(
["docker", "stats", "--no-stream", "--format", "json"],
capture_output=True,
check=True,
text=True,
)
lines = result.stdout.strip().splitlines()
return [json.loads(line) for line in lines if line.strip()]
except subprocess.CalledProcessError:
return []
except json.JSONDecodeError:
return []
def table_builder(containers: list[dict]) -> str:
"""Build a pretty table string with container stats."""
lines = []
for container in containers:
name = container.get("Name", "unknown")
cpu = container.get("CPUPerc", "0%")
mem = container.get("MemUsage", "0B / 0B").split(" /")[0]
lines.append(f"{name}|{cpu}|{mem}")
result = subprocess.run(
["column", "-t", "--table-columns", "Name,CPU,MEM", "--separator", "|"],
input="\n".join(lines),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=True,
text=True,
)
return result.stdout.strip()
def output_builder(status_class: str, text: str, tooltip: str = "") -> str:
"""Build JSON output for Waybar."""
data = {
"class": status_class,
"text": f"Docker: {text}",
}
if tooltip:
data["tooltip"] = tooltip
return json.dumps(data)
def main():
containers_counter = count_running_containers()
if containers_counter > 0:
tooltip = table_builder(get_containers())
print(output_builder("on", f"{containers_counter}", tooltip))
if __name__ == "__main__":
main()