I am monitoring hundreds of servers both dedicated and virtual using the following script:
#!/bin/bash
PATH=/usr/lib64/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
threshold=90
serverip=$($(which ifconfig) | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -1)
memused=$(free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' | awk '{print $3}' | cut -d"." -f1)
if [ "$memused" -gt "$threshold" ]
then
CTIME=$(date +%Y-%m-%d-%H%M%S)
ps aux > /root/.example/logs/lowmem-"${CTIME}"-ps.log
top -n 1 -o %MEM -c > /root/.example/logs/lowmem-"${CTIME}"-top.log
free -m > /root/.example/logs/lowmem-"${CTIME}"-free.log
mysqladmin proc -v status > /root/.example/logs/lowmem-"${CTIME}"-mysqlproc.log
bash /example/general/slack.sh "#server-alerts" ":warning: $(hostname) - ${serverip} - Memory Usage has reached 90% - Check logs /root/.example/logs/lowmem-${CTIME} \n \`\`\`$(head -1 /root/.example/logs/lowmem-"${CTIME}"-free.log) \n $(head -2 /root/.example/logs/lowmem-"${CTIME}"-free.log | tail -1) \n $(tail -1 /root/.example/logs/lowmem-"${CTIME}"-free.log)\`\`\`"
crontab -l | grep -v '/example/mon_mem.sh' | crontab -
sleep 900
crontab -l | { cat; echo "* * * * * bash /example/mon_mem.sh"; } | crontab -
fi
While it works in most cases, we are randomly getting false positives, its completely random servers and its not consistent with each server so one server might trigger but then not trigger ever again(falsely)
Example of a false positive:
total used free shared buff/cache available
Mem: 2048 345 1580 27 122 1674
Swap: 2048 0 2048
An alert came in from this server but you can see only 345 MB was in use.