Skip to content

Instantly share code, notes, and snippets.

@SmetDenis
Created July 31, 2015 19:42
Show Gist options
  • Save SmetDenis/cd63bd0807e4b8dbdfc1 to your computer and use it in GitHub Desktop.
Save SmetDenis/cd63bd0807e4b8dbdfc1 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Script to send email notification if a server exceeds a specified load average.
#
# Selected load average limit. If above this number a notification message will be emailed.
NOTIFY="1"
EMAIL="admin@example.com"
TRUE="1"
# Create a temp file
TEMPFILE="$(mktemp)"
FTEXT='load average:'
LOAD1MIN="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f1 | sed 's/ //g')"
LOAD5MIN="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f2 | sed 's/ //g')"
LOAD15MIN="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f3 | sed 's/ //g')"
MEMU="$(free -tom | awk '/Total:/ {print "Total memory: "$2" MB\nUsed memory: "$3" MB\nFree memory: "$4" MB"}')"
SUBJECT="Alert $(hostname) high load average: $LOAD1MIN"
echo "Server 5 min load average $LOAD5MIN is above notification threshold $NOTIFY" >> $TEMPFILE
echo " " >> $TEMPFILE
echo "Hostname: $(hostname)" >> $TEMPFILE
echo "Local Date & Time : $(date)" >> $TEMPFILE
echo " " >> $TEMPFILE
echo "Server load for now: $LOAD1MIN" >> $TEMPFILE
echo "Server load for the last five minutes: $LOAD5MIN" >> $TEMPFILE
echo "Server load for the last fifteen minutes: $LOAD15MIN" >> $TEMPFILE
echo " " >> $TEMPFILE
echo "------------------------" >> $TEMPFILE
echo "Memory stats:" >> $TEMPFILE
echo "------------------------" >> $TEMPFILE
echo "$MEMU" >> $TEMPFILE
echo " " >> $TEMPFILE
RESULT=$(echo "$LOAD1MIN > $NOTIFY" | bc)
if [ "$RESULT" == "$TRUE" ]; then
/usr/sbin/sendmail "$EMAIL" -s "$SUBJECT" < $TEMPFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment