12.15
Over the years of owning my own dedicated servers, I often find myself logging into the server just to check the load, the logs, and if there was any downtime. And eventually, I ended up creating a network of servers talking and checking each other, and sending the results (if necessary) to me and logging the rest. It’s always important to keep track of the health of your server, in both uptime, storage space, and load average, here are some great scripts I’ve created and have worked perfectly.
Each of these bash scripts should be copied into a file and run using a cron job every few minutes for optimal use. You’ll want to have these scripts setup on each of your servers, and have them monitor each other for best results. You can set it to send the notification to anywhere you want. Personally, I send it as a text message to my phone, and carbon copy it to my email. This way, I’m always connected with my servers, and I know what’s going on when I need to know, and not after there’s already a problem. If you’re getting too many notifications, try changing the check count(s) or load average limits.
This first script will monitor the uptime of another server:
Note: it will automatically create a log file called pinger.log and store the results of each check there.
#!/bin/bash # Put your server IP(s) or Hostname(s) here, separate by spaces. HOSTS="google.com yahoo.com" # How many times should we check for up status? COUNT=3 #What should the subject of the message be? SUBJECT="[SERVER] Alert" # Where should we send the alert? EMAILID="youremail@provider.com" # /End of user input WGET="/usr/bin/wget" if [ ! -s pinger.log ];then touch pinger.log chmod 777 pinger.log fi for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 0 ]; then # 100% failed echo " Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID echo "Host : $myHost is down (ping failed) at $(date)" >> pinger.log else echo "Host : $myHost is pingable at $(date)" >> pinger.log fi $WGET -q --tries=$COUNT --timeout=$COUNT $myHost -O /tmp/ping.$myHost &> /dev/null if [ ! -s /tmp/ping.$myHost ];then echo " Host : $myHost (apache) is down at $(date)" | mail -s "$SUBJECT" $EMAILID echo "Host : $myHost (apache) is down at $(date)" >> pinger.log else echo "Host : $myHost (apache) is up at $(date)" >> pinger.log fi done
This next script will monitor the load average of the server:
#!/bin/bash # Where should we send the alert? EMAILID="youremail@provider.com" # How high can the load be before the alert is triggered? LOAD="5" # /End of user input loadavg=`uptime | awk '{print $10}'` thisloadavg=`echo $loadavg|awk -F \. '{print $1}'` if [ "$thisloadavg" -ge "$LOAD" ]; then echo 'Load Average at:' `date` 'is' `uptime` | mail -s "Load Average Alert" $EMAILID fi
It should also be noted that by far the best uptime monitoring service I have used (and still use) is Pingdom. What great about these scripts, is that you’ll be able to monitor your server on the go. I’ve found that the load alert will often help me get control of the server before something bad happens, because you’ll be notified of a climbing load average which gives you plenty time to react.
Feel free to copy, redistribute and improve on these.














No Comment.
Add Your Comment