HowTo – Cacti Status Monitoring
Using on a script from Alex Zeng as the basis, we’ve created a script to check the status of a Cacti system and send out alerts in case of issues. The original script checked the Cacti log file only, while this enhanced version also checks on the poller runtime and 15min system load data. Feel free to use and enhance it accordingly. Save it as check_cacti.sh within the cli directory of your Cacti installation
#!/bin/bash ##### # Check Cacti Status and report any Errors/Warning # # Version: # 1.10 # # Changelog: # 1.10 - Added System Load and Cacti Runtime # 1.00 - Initial Release ##### 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" MAX_CACTI_RUNTIME=60 MAX_SYSTEM_LOAD=9 # Get Path to script. SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # configuration MAIL_TO="support@example.com" CACTI_LOG="$CACTI_HOME/log/cacti.log" CHECK_LOG="$CACTI_HOME/log/check_cacti.log" echo "`date` : Start" > $CHECK_LOG echo "" >> $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 [ "ISEMPTY$title" = "ISEMPTY" ] ; then warning=`tail -$lines_to_check $CACTI_LOG | grep -i -e warning -e error | grep -v -f $SCRIPTDIR/check_cacti.filter` if [ "ISEMPTY$warning" = "ISEMPTY" ] ; then echo "No warning or error msg" >> $CHECK_LOG else echo "" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG echo " Cacti Log Information" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG echo " Filters:" >> $CHECK_LOG cat $SCRIPTDIR/check_cacti.filter >> $CHECK_LOG echo "" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG title="found warning/error in $CACTI_LOG" tail -$lines_to_check $CACTI_LOG | grep -i -e warning -e error | grep -v -f $SCRIPTDIR/check_cacti.filter >> $CHECK_LOG fi fi ### Cacti Runtime Check lastruntime=`grep "SYSTEM STATS" $CACTI_LOG | tail -f -n 1 | awk '{print $7}' | awk -F: '{print $2}'` historyruntime=`grep "SYSTEM STATS" $CACTI_LOG | tail -f -n 5` if [ $((${lastruntime%.*})) -gt $(($MAX_CACTI_RUNTIME-5)) ]; then echo "" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG echo " Cacti RUNTIME Information" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG title="Cacti runtime is high"; echo "$historyruntime" >> $CHECK_LOG fi ### System load Check load=`uptime` maxload=`uptime | awk -F, '{ print $4 $5 $6 }' | awk '{ print $5 }'` if [ $((${maxload%.*})) -gt $((MAX_SYSTEM_LOAD)) ]; then echo "" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG echo " System Load Information" >> $CHECK_LOG echo "==================================" >> $CHECK_LOG title="System load is high"; echo "$load" >> $CHECK_LOG fi echo "" >> $CHECK_LOG echo "" >> $CHECK_LOG echo "`date` : End" >> $CHECK_LOG #send mail if [ "ISEMPTY$title" != "ISEMPTY" ] ; then title="$title at `hostname`" mailx -s "$title" $MAIL_TO < $CHECK_LOG fi
The following file check_cacti.filter needs to be placed in the same directory as the above script. It enables you to filter the Cacti warning and error messages.
SNMP timeout detected SNMP Get Timeout Ping timed out Empty result
Now you can schedule the script to run on an hourly basis:
0 * * * * cacti /var/www/html/cacti/cli/check_cacti.sh /var/www/html/cacti 30