Simple monitoring

I was searhing for a simple monitoring tool but most tools out there are very complex. We use 3rd party virtual private servers now so hardware monitoring is not our business. We're interested in monitoring two things:

  1. Free disk space
  2. CPU load
So I use two simple scripts that send out an e-mail alert when the diskspace is low or the CPU load it too high.

You can create the two scripts below and edit the MAILTO and TRIGGER variables to suit your needs /usr/local/bin/monitor-load.sh:

#!/bin/bash
TRIGGER=2.00
MAILTO=mail@somedomain.com


host=`hostname -f`
load=`cat /proc/loadavg | awk '{print $2}'`
response=`echo | awk -v T=$TRIGGER -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`
if [[ $response = "greater" ]]
then
        #capture top command output for the mail body
        body=`top -n 1 -b`

        echo "$body\n."|mail -s "High load on $host - [ $load ]" $MAILTO
fi
/usr/local/bin/monitor-disk-usage.sh:

#!/bin/bash
MAILTO=mail@somedomain.com
TRIGGER=80

HOST=`hostname -f`
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

if [ "$CURRENT" -gt "$TRIGGER" ] ; then

    mail -s "Disk Space Alert for $HOST" $MAILTO << EOF
The root partition of '$HOST' remaining free space is critically low. Used: $CURRENT%
EOF
fi

Make these two scripts executable:
chmod +x /usr/local/bin/monitor-load.sh 
chmod +x /usr/local/bin/monitor-disk-usage.sh
Now schedule these script in system cron jobs:
/etc/cron.d/monitor:
# Check server load and diskspace
* * * * * root /usr/local/bin/monitor-load.sh
0 7,16 * * * root /usr/local/bin/monitor-disk-usage.sh
This will check the server load every minute and disk space at 7am and 4pm. Finally it's wise to test if mails sent from console arrive. Change the e-mail address in this command and run it:
echo "."|mail -s "Test mail from console" mail@somedomain.com

Comments


  • Pieter van de Ven
    April 07, 2016 at 09:18

    Hai Merijn,

    The last cronjob will run every minute from 7:00 to 7:59 and from 16:00 to 16:59.


  • Merijn Schering
    April 07, 2016 at 09:40

    Thanks! I've adjusted it.


  • Unknown
    April 08, 2016 at 15:01

    Hi Merijn,

    nice scripts. We use dedicated hardware an monit for this purposes. I'll post a howto in the forum.


  • Web Hosting & Software Discount Offers 2019
    December 13, 2016 at 06:53

    This comment has been removed by the author.


  • Web Hosting & Software Discount Offers 2019
    December 13, 2016 at 06:55

    This comment has been removed by the author.

Back to Top
SourceForge.net Logo