Home Assistant
From My Wiki
Send VPS statistics to HA
Update token ($apitoken), address ($apiaddr) and disk device ($disk)
#!/bin/bash apitoken="xxxxxx" apiaddr="https://ha.myhome:8123/api/states/" apiheaders=" -H \"Authorization: Bearer $apitoken\" -H \"Content-Type: application/json\" " apihost=`hostname|sed "s/\./_/g"` apiurl="sensor.$apihost" disk="/dev/vda" srvuptime=`uptime -p` srvmem=`free -m |grep "Mem"|awk '{print ($4+$7)"M"}'` srvprocs=`ps -ef|wc -l` diskuse=`df -h $disk |grep "/$"|awk '{print $5}'` diskio=`df -ih $disk |grep "/$"|awk '{print $5}'` netcons=`netstat -ant|grep ESTA|wc -l` mailqu=`/usr/sbin/exim -bpc` srvtmp=`cat /proc/loadavg` read -r -a srvla <<< "$srvtmp" apidata=" -d '{\"state\":\"$srvuptime\", \"attributes\": {\"friendly_name\":\"$apihost\",\"uptime\":\"$srvuptime\",\"freemem\":\"$srvmem\",\"load\":${srvla[0]},\"procs\":$srvprocs,\"disk_use_$disk\":\"$diskuse\",\"disk_io_use_$disk\":\"$diskio\",\"connections\":$netcons,\"mail_queue\":$mailqu}}'" bash -c "curl $apiaddr$apiurl $apiheaders -X POST $apidata -k"