Skip to content

Instantly share code, notes, and snippets.

@chluehr
Created June 29, 2018 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chluehr/9d33bdf45cf8f50db1656f3a564d285f to your computer and use it in GitHub Desktop.
Save chluehr/9d33bdf45cf8f50db1656f3a564d285f to your computer and use it in GitHub Desktop.
Updated statuscake agent script to better deal with cpu usage and disk space reporting
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/cakeagent
# Make sure we have some auth information
if [ -f /etc/cakeagent/auth.log ]
then
Authentication=($(cat /etc/cakeagent/auth.log))
else
echo "Something has gone wrong and we can not find your auth credentials. Please reinstall"
exit 1
fi
CPU=($(cat /proc/stat | grep -E '^cpu\s'))
TOTAL0=$((${CPU[1]}+${CPU[2]}+${CPU[3]}+${CPU[4]}+${CPU[5]}+${CPU[6]}+${CPU[7]}+${CPU[8]}))
IDLE0=$((${CPU[4]}+${CPU[5]}))
sleep 10
# Get the CPU Speed
CPU=($(cat /proc/stat | grep -E '^cpu\s'))
TOTAL1=$((${CPU[1]}+${CPU[2]}+${CPU[3]}+${CPU[4]}+${CPU[5]}+${CPU[6]}+${CPU[7]}+${CPU[8]}))
IDLE1=$((${CPU[4]}+${CPU[5]}))
IDLE=$((${IDLE1}-${IDLE0}))
TOTAL=$((${TOTAL1}-${TOTAL0}))
USAGE=$((1000*(${TOTAL}-${IDLE})/${TOTAL}))
USAGE_UNITS=$((${USAGE}/10))
USAGE_DECIMAL=$((${USAGE}%10))
# average CPU usage over the last 5 checks:
echo "$USAGE_UNITS.$USAGE_DECIMAL" >> /etc/cakeagent/cpu.history
tail -5 /etc/cakeagent/cpu.history > /etc/cakeagent/cpu.history.new
mv /etc/cakeagent/cpu.history.new /etc/cakeagent/cpu.history
CPU_USAGE=($(cat /etc/cakeagent/cpu.history | egrep -v "^$" | awk '{s+=$1}END{print s/NR}'))
checkinterval=1
info="/sys/class/net/"
cd ${info}
defaultnet="$(ip route | grep '^default' | awk '{print $5;}')"
for interface in ${defaultnet}
do
rx1=`cat $info$interface/statistics/rx_bytes`
tx1=`cat $info$interface/statistics/tx_bytes`
`sleep $((checkinterval))s`
rx2=`cat $info$interface/statistics/rx_bytes`
tx2=`cat $info$interface/statistics/tx_bytes`
RX=$((($rx2-$rx1)/($checkinterval*1024)))
TX=$((($tx2-$tx1)/($checkinterval*1024)))
done
# Memory Information
freeMem=`cat /proc/meminfo | grep MemAvailable | sed -r "s/MemAvailable:.* ([0-9]+) kB/\1/"`
if [ -z "$freeMem" ]; then
mem_free=`grep MemFree /proc/meminfo | awk '{print $2}'`
mem_buff=`grep Buffers /proc/meminfo | awk '{print $2}'`
mem_cach=`grep Cached /proc/meminfo | awk 'NR==1{print $2}'`
freeMem=`expr $mem_free + $mem_buff + $mem_cach`
fi
# Count 80% of the ARC (ZFS) cache as available - 100% can be released, but it's probably a bad idea for performance reasons
if [ -f /proc/spl/kstat/zfs/arcstats ]; then
ArcCacheSize=($(grep ^size /proc/spl/kstat/zfs/arcstats | awk '{print $3}'))
if [ -n "$ArcCacheSize" ]; then
ArcCacheSize=$(echo $ArcCacheSize | LC_NUMERIC=en_US.UTF-8 awk '{printf "%0.0f", $1 / 1000 }')
# 80% of the ARC cache that we want to count as available
SafeArcCacheSize=$(echo $ArcCacheSize | LC_NUMERIC=en_US.UTF-8 awk '{ printf "%0.0f", $1 * 0.8 }')
freeMemWithArcCache=$(echo "$freeMem $SafeArcCacheSize" | awk '{printf "%0.0f", $1 + $2}')
freeMem=$freeMemWithArcCache
fi
fi
# average free memory over the last 5 checks:
echo "$freeMem" >> /etc/cakeagent/mem.history
tail -5 /etc/cakeagent/mem.history > /etc/cakeagent/mem.history.new
mv /etc/cakeagent/mem.history.new /etc/cakeagent/mem.history
freeMem=($(cat /etc/cakeagent/mem.history | egrep -v "^$" | awk '{s+=$1}END{print int(s/NR)}'))
MemTotal=`cat /proc/meminfo | grep MemTotal | sed -r "s/MemTotal:.* ([0-9]+) kB/\1/"`
cpuUse=`top -b -n1 | grep Cpu | sed -re "s/.*:[ ]+([0-9.]*)%us,.*/\1/"`
# Uptime Information
uptime=$(</proc/uptime)
uptime=${uptime%%.*}
seconds=$(( uptime%60 ))
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))
# Hard Drive Information
#hdd=`df -x tmpfs -x cdrom --total | grep total | awk '{print $3}'`
#thdd=`df -x tmpfs -x cdrom --total | grep total | awk '{print $2}'`
hdd=`df -x tmpfs -x cdrom --total | grep -v '/dev/loop' | grep -v 'Used' | awk '{print $5 " " $3 " " $2 "\n"}' |sed -r -e "s/%//" | sort -n -r | head -1 | cut -d " " -f 2`
thdd=`df -x tmpfs -x cdrom --total | grep -v '/dev/loop' | grep -v 'Used' | awk '{print $5 " " $3 " " $2 "\n"}' |sed -r -e "s/%//" | sort -n -r | head -1 | cut -d " " -f 3`
drives=`df -H -P -B G | grep -vE '^Filesystem|tmpfs|cdrom|loop' | awk '{ printf $3 "|" $2 "|" $1 ":"}'`
# Running processes
process=`ps aux | awk '{gsub("%", "%%", $0);printf $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 "|" $11 ":::"}'`
# The data to send
data_to_send="user=${Authentication[0]}&secret=${Authentication[1]}&payload={\"rx\":\"$RX\",\"tx\":\"$TX\",\"process\":\"$process\",\"drives\":\"$drives\",\"ping\":\"$ping\",\"freeMem\":\"$freeMem\",\"MemTotal\":\"$MemTotal\",\"cpuUse\":\"$CPU_USAGE\",\"uptime\":\"$uptime\",\"hdd\":\"$hdd\",\"thdd\":\"$thdd\"}"
# Send to StatusCake, with a timeout of 30s
wget -q -o /dev/null -O /etc/cakeagent/cakelog.log -T 30 --post-data "$data_to_send" --no-check-certificate https://agent.statuscake.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment