works
This commit is contained in:
1 parent
9b02a488bf
commit
d6d0f37c5a
1 file changed
+17
-1
+17
-1
@@ -1,7 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Collect server runtime stats (uptime, top, cgroup, mounts) and return as JSON."""
|
"""Collect server runtime stats (uptime, top, cgroup, mounts, hdd) and return as JSON."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
@@ -32,6 +33,20 @@ def read_file(path):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def disk_usage(path):
|
||||||
|
"""Total/used/available bytes for the filesystem at `path` (same figures as df)."""
|
||||||
|
try:
|
||||||
|
st = os.statvfs(path)
|
||||||
|
return {
|
||||||
|
"path": path,
|
||||||
|
"total_bytes": st.f_blocks * st.f_frsize,
|
||||||
|
"used_bytes": (st.f_blocks - st.f_bfree) * st.f_frsize,
|
||||||
|
"available_bytes": st.f_bavail * st.f_frsize,
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
return f"ERROR: {e}"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
stats = {}
|
stats = {}
|
||||||
|
|
||||||
@@ -39,6 +54,7 @@ def main():
|
|||||||
stats["top"] = cmd(["sh", "-c", "top -b -n 1 | head -n 3"], splitlines=True)
|
stats["top"] = cmd(["sh", "-c", "top -b -n 1 | head -n 3"], splitlines=True)
|
||||||
stats["cgroup"] = read_file("/proc/self/cgroup")
|
stats["cgroup"] = read_file("/proc/self/cgroup")
|
||||||
stats["mounts"] = cmd(["mount"], splitlines=True)
|
stats["mounts"] = cmd(["mount"], splitlines=True)
|
||||||
|
stats["hdd"] = disk_usage("/")
|
||||||
|
|
||||||
print(json.dumps(stats, indent=2))
|
print(json.dumps(stats, indent=2))
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user