A script to check cacti

A simple script to check whether cacti works well.

#!/bin/bash

if [ $# -lt 2 ]; then
cat <<EOF
  Check cacti log
  usage:  check_cacti.sh <CACTI_HOME>
  examples:      
          check_cacti.sh /export/home/cacti 30   #check last 30 lines in cacti log
EOF
  exit 1
fi

# arguments
CACTI_HOME="$1"
lines_to_check="$2"

# configuration
MAIL_TO="alexzeng@wordpress.com"
CACTI_LOG="$CACTI_HOME/log/cacti.log"
CHECK_LOG="$CACTI_HOME/log/check_cacti.log"

echo "`date` : Start" > $CHECK_LOG
echo ""  >> $CHECK_LOG

#check if the logfile get updated
current=`date +%s`
last_modified=`stat -c "%Y" $CACTI_LOG`
if [ $(($current-$last_modified)) -gt 3600 ]; then 
  title="CACTI log didn't get updated in last hour"; 
  ls -lt $CACTI_LOG >> $CHECK_LOG
fi

#check warning or error msg in log, only if cacti log is updated
if [ "alex$title" = "alex" ] ; then
  warining=`tail -$lines_to_check $CACTI_LOG | grep -i -e warning -e error`
  if [ "alex$warining" = "alex" ] ; then
    echo "No warning or error msg" >> $CHECK_LOG
  else
    title="found warning/error in $CACTI_LOG"
    tail -$lines_to_check $CACTI_LOG | grep -i -e warning -e error >> $CHECK_LOG
  fi
fi

echo ""  >> $CHECK_LOG
echo "`date` : End" >> $CHECK_LOG

#send mail
if [ "alex$title" != "alex" ] ; then
  title="$title at `hostname`"
  mailx -s "$title" $MAIL_TO < $CHECK_LOG
fi

Add a cron job to check cacti every hour.

#check cacti
0 * * * *   /export/home/cacti/cli/check_cacti.sh /export/home/cacti 30 
Advertisements

About Alex Zeng
I would be very happy if this blog can help you. I appreciate every honest comments. Please forgive me if I'm too busy to reply your comments in time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: