1
0
mirror of https://github.com/librenms/librenms-agent.git synced 2024-05-09 09:54:52 +00:00

Improve docker stats (#450)

This commit is contained in:
Henne Van Och
2023-03-01 01:09:43 +01:00
committed by GitHub
parent fd78c041d5
commit bd4c946428
2 changed files with 112 additions and 38 deletions

112
snmp/docker-stats.py Normal file
View File

@@ -0,0 +1,112 @@
#!/usr/bin/env python3
import datetime
import json
import subprocess
from dateutil import parser
VERSION = 2
ONLY_RUNNING_CONTAINERS = True
def run(cmd):
res = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
return res
def inspectContainer(container):
raw = run(["docker", "inspect", "-s", container])
data = json.loads(raw)
return data
def getStats():
command = [
"docker",
"stats",
"--no-stream",
"--no-trunc",
"--format",
"{{ json . }}",
]
if not ONLY_RUNNING_CONTAINERS:
command.append("-a")
raw = run(command)
lines = raw.split(b"\n")
containers = []
for line in lines[0:-1]:
containers.append(json.loads(line))
return containers
def dump():
containers = []
try:
stats_containers = getStats()
except subprocess.CalledProcessError as e:
print(
json.dumps(
{
"version": VERSION,
"data": containers,
"error": e.returncode,
"errorString": e.output.decode("utf-8"),
}
)
)
return
for container in stats_containers:
try:
inspected_container = inspectContainer(container["Name"])
except subprocess.CalledProcessError:
continue
started_at = parser.parse(inspected_container[0]["State"]["StartedAt"])
if inspected_container[0]["State"]["Running"]:
finished_at = datetime.datetime.now(started_at.tzinfo)
else:
finished_at = parser.parse(inspected_container[0]["State"]["FinishedAt"])
uptime = finished_at - started_at
containers.append(
{
"container": container["Name"],
"pids": container["PIDs"],
"memory": {
"used": container["MemUsage"].split(" / ")[0],
"limit": container["MemUsage"].split(" / ")[1],
"perc": container["MemPerc"],
},
"cpu": container["CPUPerc"],
"size": {
"size_rw": inspected_container[0]["SizeRw"],
"size_root_fs": inspected_container[0]["SizeRootFs"],
},
"state": {
"status": inspected_container[0]["State"]["Status"],
"started_at": inspected_container[0]["State"]["StartedAt"],
"finished_at": inspected_container[0]["State"]["FinishedAt"],
"uptime": round(uptime.total_seconds()),
},
}
)
print(
json.dumps(
{
"version": VERSION,
"data": containers,
"error": "0",
"errorString": "",
}
)
)
if __name__ == "__main__":
dump()

View File

@@ -1,38 +0,0 @@
#!/usr/bin/env bash
VERSION=1
function dockerStatsFormat() {
cat <<EOF
{
"container": "{{.Name}}",
"pids": {{.PIDs}},
"memory": {
"used": "{{ index (split .MemUsage " / ") 0 }}",
"limit": "{{ index (split .MemUsage " / ") 1 }}",
"perc": "{{.MemPerc}}"
},
"cpu": "{{.CPUPerc}}"
}
EOF
}
function getStats() {
docker stats \
--no-stream \
--format "$(dockerStatsFormat)"
}
STATS=$(getStats 2>&1)
ERROR=$?
if [ $ERROR -ne 0 ];then
ERROR_STRING=${STATS}
unset STATS
fi
jq -nMc \
--slurpfile stats <(echo "${STATS:-}") \
--arg version "${VERSION:-1}" \
--arg error "${ERROR:-0}" \
--arg errorString "${ERROR_STRING:-}" \
'{"version": $version, "data": $stats, "error": $error, "errorString": $errorString }'
# vim: tabstop=2:shiftwidth=2:expandtab: