system_stats.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. # https://gist.github.com/izmailoff/8923008
  3. # Debugging and error handling
  4. # Stop this script on any error. Unless you want to get data by any means
  5. # this is a good option.
  6. set -e
  7. # Debugging options:
  8. #set -x
  9. #set -v
  10. # Memory
  11. memTotal=$(egrep '^MemTotal:' /proc/meminfo | awk '{print $2}')
  12. memFree=$(egrep '^MemFree:' /proc/meminfo | awk '{print $2}')
  13. memCached=$(egrep '^Cached:' /proc/meminfo | awk '{print $2}')
  14. memAvailable=$(expr "$memFree" + "$memCached")
  15. memUsed=$(($memTotal - $memFree))
  16. swapTotal=$(egrep '^SwapTotal:' /proc/meminfo | awk '{print $2}')
  17. swapFree=$(egrep '^SwapFree:' /proc/meminfo | awk '{print $2}')
  18. swapUsed=$(($swapTotal - $swapFree))
  19. # CPU
  20. #cpuUtilization=$((100 - $(vmstat 2 2 | tail -1 | awk '{print $15}' | sed 's/%//')))
  21. cpuPercent=$(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1); }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat) | sed s/,/./g)
  22. load=$(awk '{print "load_1=" $1 ",load_5=" $2 ",load_15=" $3}' < /proc/loadavg)
  23. procs=$(awk '{print $4}' < /proc/loadavg | awk -F '/' '{print "procs_current=" $1 ",procs_scheduled=" $2}')
  24. # Disk
  25. #disksJson=$(for d in $(df -P -x tmpfs -x devtmpfs -x ecryptfs -x nfs -x cifs -x squashfs -T | tail -n+2 | awk '{print "{" "\"total\":" $3 ", \"used\":" $4 ", \"mountPoint\":" "\""$7"\"" "},"}'); do echo $d; done | sed '$s/.$//')
  26. #rootUsed=$(df -P -x tmpfs -x devtmpfs -x ecryptfs -x nfs -x cifs -x squashfs -T | egrep ' /$' | awk '{print $6 }' | sed s/%//g)
  27. disks=$(for d in $(df -P -x tmpfs -x devtmpfs -x ecryptfs -x nfs -x cifs -x squashfs -T | tail -n+2 | awk '{print ",mount=" $7 ",fs=" $1 ",type=" $2 ":used=" $4 ",available=" $5 ",capacity=" $6 }'); do echo "disks,host=$HOSTNAME$d"; done | sed 's/%//g' | sed 's/:/ /g')
  28. piTemp="" # extra check for raspberry
  29. if [ $(whoami) == "pi" ]; then
  30. piTemp="temp,host=$HOSTNAME c=$(vcgencmd measure_temp | grep -o '[0-9.]\+')"
  31. fi
  32. mem="memory,host=$HOSTNAME total=$memTotal,used=$memUsed,cache=$memCached,available=$memAvailable,free=$memFree,swap_total=$swapTotal,swap_free=$swapFree,swap_used=$swapUsed"
  33. cpu="cpu,host=$HOSTNAME used=$cpuPercent,$load,$procs"
  34. IQL="$mem
  35. $cpu
  36. $disks
  37. $piTemp"
  38. #echo "$IQL"
  39. curl -X POST --data-binary "$IQL" "https://xrg.io/influx.php?db=machines"