Skip to content

Instantly share code, notes, and snippets.

@walm
Last active August 26, 2024 04:14
Show Gist options
  • Save walm/e084e5184bc14da9ddbe to your computer and use it in GitHub Desktop.
Save walm/e084e5184bc14da9ddbe to your computer and use it in GitHub Desktop.
Simple Linux Server stats as JSON
#!/bin/sh
echo -n '{'
# memory as "mem": { "current": 800, "total": 1024, "load", 82 } where amount is in MB and load in %
free -m | awk 'NR==2{printf "\"mem\": { \"current\":%d, \"total\":%d, \"load\": %.2f }", $3,$2,$3*100/$2 }'
echo -n ','
# diska as "disk": { "current": 6, "total": 40, "used": 19 } where amount is in GB and used in %
df -h | awk '$NF=="/"{printf "\"disk\": { \"current\":%d, \"total\":%d, \"used\": %d }", $3,$2,$5}'
echo -n ','
# cpu as "cpu": { "load": 40 } where load is in %
top -bn1 | grep load | awk '{printf "\"cpu\": { \"load\": %.2f }", $(NF-2)}'
echo -n '}'
{
"cpu" : {
"load" : 0
},
"disk" : {
"used" : 19,
"current" : 6,
"total" : 40
},
"mem" : {
"current" : 1814,
"load" : 90.61,
"total" : 2002
}
}
@mdimai666
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment