128 lines
3.8 KiB
Python
Executable File
128 lines
3.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import time
|
|
import psutil
|
|
import docker
|
|
import os
|
|
from datetime import datetime
|
|
from datetime import datetime, timezone
|
|
|
|
OUTPUT_FILE_NAME = "~/Documents/AppRobotConfig/data/status.json"
|
|
OUTPUT_FILE = os.path.expanduser(OUTPUT_FILE_NAME)
|
|
|
|
client = docker.from_env()
|
|
|
|
def get_system_stats():
|
|
vm = psutil.virtual_memory()
|
|
cpu = psutil.cpu_percent(interval=None)
|
|
load1, load5, load15 = psutil.getloadavg()
|
|
|
|
return {
|
|
"cpu": {
|
|
"percent": cpu,
|
|
"load_avg": {
|
|
"1m": load1,
|
|
"5m": load5,
|
|
"15m": load15
|
|
}
|
|
},
|
|
"memory": {
|
|
"total_mb": round(vm.total / 1024 / 1024, 2),
|
|
"used_mb": round(vm.used / 1024 / 1024, 2),
|
|
"percent": vm.percent
|
|
}
|
|
}
|
|
|
|
def calculate_cpu_percent(stats):
|
|
print("Calculate Percentage")
|
|
cpu_delta = (
|
|
stats["cpu_stats"]["cpu_usage"]["total_usage"]
|
|
- stats["precpu_stats"]["cpu_usage"]["total_usage"]
|
|
)
|
|
system_delta = (
|
|
stats["cpu_stats"]["system_cpu_usage"]
|
|
- stats["precpu_stats"]["system_cpu_usage"]
|
|
)
|
|
|
|
if system_delta > 0 and cpu_delta > 0:
|
|
cpu_count = len(stats["cpu_stats"]["cpu_usage"].get("percpu_usage", []))
|
|
return round((cpu_delta / system_delta) * cpu_count * 100, 2)
|
|
|
|
return 0.0
|
|
|
|
def get_container_stats():
|
|
containers_data = []
|
|
|
|
for container in client.containers.list(all=True):
|
|
info = {
|
|
"id": container.short_id,
|
|
"name": container.name,
|
|
"image": container.image.tags[0] if container.image.tags else container.image.short_id,
|
|
"status": container.status,
|
|
"state": container.attrs["State"]["Status"],
|
|
}
|
|
|
|
# Add openPorts info
|
|
open_ports = []
|
|
network_settings = container.attrs.get("NetworkSettings", {})
|
|
port_bindings = network_settings.get("Ports", {})
|
|
|
|
for container_port, host_bindings in port_bindings.items():
|
|
if host_bindings:
|
|
for binding in host_bindings:
|
|
open_ports.append({
|
|
"container_port": container_port,
|
|
"host_ip": binding.get("HostIp"),
|
|
"host_port": binding.get("HostPort")
|
|
})
|
|
else:
|
|
# Port exposed but not published
|
|
open_ports.append({
|
|
"container_port": container_port,
|
|
"host_ip": None,
|
|
"host_port": None
|
|
})
|
|
|
|
info["openPorts"] = open_ports
|
|
|
|
|
|
if container.status == "running":
|
|
stats = container.stats(stream=False)
|
|
|
|
cpu_percent = calculate_cpu_percent(stats)
|
|
|
|
mem_usage = stats["memory_stats"]["usage"]
|
|
mem_limit = stats["memory_stats"]["limit"]
|
|
mem_percent = (mem_usage / mem_limit) * 100 if mem_limit > 0 else 0
|
|
|
|
info["resources"] = {
|
|
"cpu_percent": cpu_percent,
|
|
"memory_mb": round(mem_usage / 1024 / 1024, 2),
|
|
"memory_limit_mb": round(mem_limit / 1024 / 1024, 2),
|
|
"memory_percent": round(mem_percent, 2)
|
|
}
|
|
else:
|
|
info["resources"] = None
|
|
|
|
containers_data.append(info)
|
|
|
|
return containers_data
|
|
|
|
def main():
|
|
# while True:
|
|
data = {
|
|
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
"system": get_system_stats(),
|
|
"containers": get_container_stats()
|
|
}
|
|
|
|
print("output written")
|
|
with open(OUTPUT_FILE, "w+") as f:
|
|
json.dump(data, f, indent=2)
|
|
|
|
#time.sleep(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|