1
0
mirror of https://github.com/librenms/librenms-agent.git synced 2024-05-09 09:54:52 +00:00
Files
librenms-librenms-agent/snmp/docker-stats.sh
0xbad0c0d3 ca16f20185 docker stats script (#307)
* docker stats script

* Update docker-stats.sh

Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
2021-03-07 20:26:47 +01:00

39 lines
795 B
Bash

#!/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: