Skip to content

Instantly share code, notes, and snippets.

@wilkart
Last active December 16, 2016 08:33
Show Gist options
  • Save wilkart/9f785d9a5f71f96b581b4563fdab0c06 to your computer and use it in GitHub Desktop.
Save wilkart/9f785d9a5f71f96b581b4563fdab0c06 to your computer and use it in GitHub Desktop.
#!/bin/bash
##!
##! StatusCake Server Monitoring for OpenBSD
##! based on https://www.statuscake.com/App/Agent/agent.sh
##!
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/etc/cakeagent
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
function checkCpu {
local CPU_IDLE=`top -b | grep "CPU states" | awk -F, '{print $5}' | awk '{print $1}' | sed 's/%//g'`
local CPU_USAGE=`echo "100 - ${CPU_IDLE}" | bc`
CPU=${CPU_USAGE}
}
function checkLoad {
LOAD=`sysctl vm.loadavg | awk -F= '{print $2}' | awk '{print $1}'`
LOAD=`echo "100 * ${LOAD}" | bc`
}
function checkNetwork {
local checkinterval=10
local NET_STAT=`netstat -ibqn -w ${checkinterval} -c 2 | tail -n 1`
RX=`echo ${NET_STAT} | awk '{print $3}'`
TX=`echo ${NET_STAT} | awk '{print $4}'`
RX=$(($RX/($checkinterval*1024)))
TX=$(($TX/($checkinterval*1024)))
}
function checkMemory {
freeMem=`top -b | grep "^Memory" | awk -F: '{ print $4 }' | awk '{ print $1 }' | sed 's/M//g'`
usedMem=`top -b | grep "^Memory" | awk -F: '{ print $3 }' | awk -F/ '{ print $2 }' | awk '{ print $1 }' | sed 's/M//g'`
MemTotal=$(($usedMem+$freeMem))
}
function checkDrives {
drives=`df -nlPk | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ printf $3 "|" $2 "|" $1 ":"}'`
hdd=`df -nlPk | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ SUM += $3} END { print SUM }'`
thdd=`df -nlPk | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ SUM += $2} END { print SUM }'`
}
function checkProcess {
process=`ps -auxc | awk '{ gsub("%","%%",$0); printf $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 "|" $11 ":::"}'`
}
## Todo
function checkTodo {
ping=
uptime=0
}
## Collect metrics
checkCpu
checkMemory
checkDrives
checkProcess
checkNetwork
checkTodo
## 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\",\"uptime\":\"$uptime\",\"hdd\":\"$hdd\",\"thdd\":\"$thdd\"}"
## Send to StatusCake, with a timeout of 30s
wget -v -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