Plugin:check cpu stats


 * 1) CPU Utilization Statistics plugin for Nagios
 * 2) Written by	: Steve BosekA
 * 3) Patched by  	: Bas van der Doorn
 * 4) Release	: 2.3
 * 5) Creation date	: 8 September 2007
 * 6) Revision date : 11 December 2008
 * 7) Package       : DTB Nagios Plugin
 * 8) Description   : Nagios plugin (script) to check cpu utilization statistics.
 * 9) 		This script has been designed and written on Unix plateform (Linux, Aix, Solaris),
 * 10) 		requiring iostat as external program. The locations of these can easily
 * 11) 		be changed by editing the variables $IOSTAT at the top of the script.
 * 12) 		The script is used to query 6 of the key cpu statistics (user,system,iowait,steal,nice,idle)
 * 13) 		at the same time.
 * 14) USAGE		: ./check_cpu_stats.sh [-w ] [-c ] ( [ -i ] [ -n ])
 * 15) Exemple: ./check_cpu_stats.sh
 * 16)          ./check_cpu_stats.sh -w 70,40,30 -c 90,60,40
 * 17)          ./check_cpu_stats.sh -w 70,40,30 -c 90,60,40 -i 3 -n 5
 * 18) TODO:  	- Support for HP-UX
 * 19) 	 	     - Beta Version : Threshold for user and system output with format -w user,system,iowait -c user,system,iowait
 * 20) 	 	     Example: ./check_cpu_stats.sh -w 70,40,30 -c 90,40,30
 * 21)         - Check ${TAB_WARNING_THRESHOLD[@]} and ${TAB_CRITICAL_THRESHOLD[@]}
 * 22)         - Check Warning CPU threshold lower as Critical CPU Threshold
 * 23) HISTORY :
 * 24)     Release	|     Date	|    Authors	 	|	Description
 * 25)  2.0		| 16.02.08	| Steve Bosek		| Solaris support and new parameters
 * 26)               | 		|               	| New Parameters : - iostat seconds intervals
 * 27)               |               |               	|         	   - iostat report number
 * 28)  2.1 		| 08.06.08 	| Steve Bosek 		| Bug perfdata and convert comma in point for Linux result
 * 29)  2.1.1 	| 05.12.08 	| Steve Bosek   	| Fixed improperly terminated string that was left open at line 130
 * 30)  2.1.2	| 06.12.08 	| Bas van der Doorn 	| Fixed linux steal reported as idle, comparisons
 * 31)  2.2		| 06.12.08	| Bas van der Doorn	| Capable systems will output nice and steal data
 * 32)  2.2.1	| 06.12.08	| Steve Bosek		| Add for uniform Unix output nice and steal data on all perfdata
 * 33)  2.3   | 11.12.08  | Steve Bosek   |  Add Threshold for user and system output with format -w user,system,iowait -c user,system,iowait
 * 34)                                       Add Default parameters value for threshold if not define
 * 35)                                       Add check for ${TAB_WARNING_THRESHOLD[@]} and ${TAB_CRTICAL_THRESHOLD[@]}
 * 36)                                       Add verify for Critical CPU Threshold lower as Warning CPU threshold
 * 37) 2.3.1 | 16.12.08 | Steve Bosek   |    Potability AIX,SOLARIS,LINUX for table initialisation (TAB_WARNING_THRESHOLD and TAB_CRITICAL_THRESHOLD)
 * 1)  2.1 		| 08.06.08 	| Steve Bosek 		| Bug perfdata and convert comma in point for Linux result
 * 2)  2.1.1 	| 05.12.08 	| Steve Bosek   	| Fixed improperly terminated string that was left open at line 130
 * 3)  2.1.2	| 06.12.08 	| Bas van der Doorn 	| Fixed linux steal reported as idle, comparisons
 * 4)  2.2		| 06.12.08	| Bas van der Doorn	| Capable systems will output nice and steal data
 * 5)  2.2.1	| 06.12.08	| Steve Bosek		| Add for uniform Unix output nice and steal data on all perfdata
 * 6)  2.3   | 11.12.08  | Steve Bosek   |  Add Threshold for user and system output with format -w user,system,iowait -c user,system,iowait
 * 7)                                       Add Default parameters value for threshold if not define
 * 8)                                       Add check for ${TAB_WARNING_THRESHOLD[@]} and ${TAB_CRTICAL_THRESHOLD[@]}
 * 9)                                       Add verify for Critical CPU Threshold lower as Warning CPU threshold
 * 10) 2.3.1 | 16.12.08 | Steve Bosek   |    Potability AIX,SOLARIS,LINUX for table initialisation (TAB_WARNING_THRESHOLD and TAB_CRITICAL_THRESHOLD)


 * 1) Paths to commands used in this script.  These may have to be modified to match your system setup.

IOSTAT=/usr/bin/iostat

STATE_OK=0 STATE_WARNING=1 STATE_CRITICAL=2 STATE_UNKNOWN=3
 * 1) Nagios return codes

LIST_WARNING_THRESHOLD=${LIST_WARNING_THRESHOLD:="70,40,30"} LIST_CRITICAL_THRESHOLD=${LIST_CRITICAL_THRESHOLD:="90,60,40"} INTERVAL_SEC=${INTERVAL_SEC:="1"} NUM_REPORT=${NUM_REPORT:="3"} PROGNAME=$(basename $0) RELEASE="Revision 2.2.1" AUTHOR="(c) 2008 Steve Bosek (steve.bosek@gmail.com)"
 * 1) Plugin parameters value if not define
 * 1) Plugin variable description

if [ ! -x $IOSTAT ]; then echo "UNKNOWN: iostat not found or is not executable by the nagios user." exit $STATE_UNKNOWN fi

print_release { echo "$RELEASE $AUTHOR" }
 * 1) Functions plugin usage

print_usage { echo "" echo "$PROGNAME $RELEASE - CPU Utilization check script for Nagios" echo "" echo "Usage: check_cpu_stats.sh -w -c (-i -n)" echo "" echo " -w  Warning threshold in % for warn_user,warn_system,warn_iowait CPU (default : 70,40,30)" echo " Exit with WARNING status if cpu exceeds warn_n" echo " -c  Critical threshol        echo "  Exit with CRITICAL status if cpu exceeds crit_n"        echo "  -i  Interval in seconds for iostat (default : 1)"        echo "  -n  Number report for iostat (default : 3)"        echo "  -h  Show this page"        echo ""    echo "Usage: $PROGNAME"    echo "Usage: $PROGNAME --help"    echo ""    exit 0 }

print_help { print_usage echo "" echo "This plugin will check cpu utilization (user,system,CPU_Iowait,idle in %)" echo "" exit 0 }

while [ $# -gt 0 ]; do   case "$1" in        -h | --help)            print_help            exit $STATE_OK            ;;        -v | --version) print_release exit $STATE_OK ;;       -w | --warning)                shift                LIST_WARNING_THRESHOLD=$1                ;;        -c | --critical) shift LIST_CRITICAL_THRESHOLD=$1 ;;       -i | --interval)               shift               INTERVAL_SEC=$1                ;;        -n | --number) shift NUM_REPORT=$1 ;;               *)  echo "Unknown argument: $1"            print_usage            exit $STATE_UNKNOWN            ;;        esac shift done
 * 1) Parse parameters

set +A TAB_WARNING_THRESHOLD `echo $LIST_WARNING_THRESHOLD | sed 's/,/ /g'` if [ "${#TAB_WARNING_THRESHOLD[@]}" -ne "3" ]; then echo "ERROR : Bad count parameter in Warning Threshold" exit $STATE_WARNING else USER_WARNING_THRESHOLD=`echo ${TAB_WARNING_THRESHOLD[0]}` SYSTEM_WARNING_THRESHOLD=`echo ${TAB_WARNING_THRESHOLD[1]}` IOWAIT_WARNING_THRESHOLD=`echo ${TAB_WARNING_THRESHOLD[2]}` fi
 * 1) List to Table for warning threshold (compatibility with

set +A TAB_CRITICAL_THRESHOLD `echo $LIST_CRITICAL_THRESHOLD | sed 's/,/ /g'` if [ "${#TAB_CRITICAL_THRESHOLD[@]}" -ne "3" ]; then echo "ERROR : Bad count parameter in CRITICAL Threshold" exit $STATE_WARNING else USER_CRITICAL_THRESHOLD=`echo ${TAB_WARNING_THRESHOLD[0]}` SYSTEM_CRITICAL_THRESHOLD=`echo ${TAB_CRITICAL_THRESHOLD[1]}` IOWAIT_CRITICAL_THRESHOLD=`echo ${TAB_CRITICAL_THRESHOLD[2]}` fi
 * 1) List to Table for critical threshold

if [ ${TAB_WARNING_THRESHOLD[0]} -ge ${TAB_CRITICAL_THRESHOLD[0]} -o ${TAB_WARNING_THRESHOLD[1]} -ge ${TAB_CRITICAL_THRESHOLD[1]} -o ${TAB_WARNING_THRESHOLD[2]} -ge ${TAB_CRITICAL_THRESHOLD[2]} ]; then echo "ERROR : Critical CPU Threshold lower as Warning CPU Threshold " exit $STATE_WARNING fi

case `uname` in	Linux ) CPU_REPORT=`iostat -c $INTERVAL_SEC $NUM_REPORT | sed -e 's/,/./g' | tr -s ' ' ';' | sed '/^$/d' | tail -1`			CPU_REPORT_SECTIONS=`echo ${CPU_REPORT} | grep ';' -o | wc -l`			CPU_USER=`echo $CPU_REPORT | cut -d ";" -f 2`			CPU_NICE=`echo $CPU_REPORT | cut -d ";" -f 3`			CPU_SYSTEM=`echo $CPU_REPORT | cut -d ";" -f 4`			CPU_IOWAIT=`echo $CPU_REPORT | cut -d ";" -f 5`		if [ ${CPU_REPORT_SECTIONS} -ge 6 ]; then			CPU_STEAL=`echo $CPU_REPORT | cut -d ";" -f 6`			CPU_IDLE=`echo $CPU_REPORT | cut -d ";" -f 7`			NAGIOS_DATA="user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% nice=${CPU_NICE}% steal=${CPU_STEAL}% | CpuUser=${CPU_USER};CpuSystem=${CPU_SYSTEM};CpuIowait=${CPU_IOWAIT};CpuIdle=${CPU_IDLE};CpuNice=${CPU_NICE};CpuSteal=${CPU_STEAL};$LIST_WARNING_THRESHOLD;$LIST_CRITICAL_THRESHOLD"		else			CPU_IDLE=`echo $CPU_REPORT | cut -d ";" -f 6` NAGIOS_DATA="user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% nice=${CPU_NICE}% steal=0.00% | CpuUser=${CPU_USER};CpuSystem=${CPU_SYSTEM};CpuIowait=${CPU_IOWAIT};CpuIdle=${CPU_IDLE};CpuNice=${CPU_NICE};CpuSteal=00.0;$LIST_WARNING_THRESHOLD;$LIST_CRITICAL_THRESHOLD" fi ;; 	AIX ) CPU_REPORT=`iostat -t $INTERVAL_SEC $NUM_REPORT | sed -e 's/,/./g'|tr -s ' ' ';' | tail -1`			CPU_USER=`echo $CPU_REPORT | cut -d ";" -f 4`			CPU_SYSTEM=`echo $CPU_REPORT | cut -d ";" -f 5`			CPU_IOWAIT=`echo $CPU_REPORT | cut -d ";" -f 7`			CPU_IDLE=`echo $CPU_REPORT | cut -d ";" -f 6`			NAGIOS_DATA="user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% nice=0.00% steal=0.00% | CpuUser=${CPU_USER};CpuSystem=${CPU_SYSTEM};CpuIowait=${CPU_IOWAIT};CpuIdle=${CPU_IDLE};CpuNice=00.0;CpuSteal=00.0;$LIST_WARNING_THRESHOLD;$LIST_CRITICAL_THRESHOLD"           ;;  	HP-UX ) CPU_REPORT=`sar -u $INTERVAL_SEC $NUM_REPORT | tail -1` CPU_USER=`echo $CPU_REPORT | awk '{ print $2 }'` CPU_SYSTEM=`echo $CPU_REPORT | awk '{ print $3 }'` CPU_IOWAIT=`echo $CPU_REPORT | awk '{ print $4 }'` CPU_IDLE=`echo $CPU_REPORT | awk '{ print $5 }'` NAGIOS_DATA="user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% nice=0.00% steal=0.00% | CpuUser=${CPU_USER};CpuSystem=${CPU_SYSTEM};CpuIowait=${CPU_IOWAIT};CpuIdle=${CPU_IDLE};CpuNice=00.0;CpuSteal=00.0;$LIST_WARNING_THRESHOLD;$LIST_CRITICAL_THRESHOLD" ;; 	SunOS ) CPU_REPORT=`iostat -c $INTERVAL_SEC $NUM_REPORT | tail -1`      			CPU_USER=`echo $CPU_REPORT | awk '{ print $1 }'`			      CPU_SYSTEM=`echo $CPU_REPORT | awk '{ print $2 }'`			      CPU_IOWAIT=`echo $CPU_REPORT | awk '{ print $3 }'`			      CPU_IDLE=`echo $CPU_REPORT | awk '{ print $4 }'`			      NAGIOS_DATA="user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% nice=0.00% steal=0.00% | CpuUser=${CPU_USER};CpuSystem=${CPU_SYSTEM};CpuIowait=${CPU_IOWAIT};CpuIdle=${CPU_IDLE};CpuNice=00.0;CpuSteal=00.0;$LIST_WARNING_THRESHOLD;$LIST_CRITICAL_THRESHOLD"            ;;	*) 		echo "UNKNOWN: `uname` not yet supported by this plugin. Coming soon !" exit $STATE_UNKNOWN ;;	esac
 * 1) CPU Utilization Statistics Unix Plateform ( Linux,AIX,Solaris are supported )

CPU_USER_MAJOR=`echo $CPU_USER| cut -d "." -f 1` CPU_SYSTEM_MAJOR=`echo $CPU_SYSTEM | cut -d "." -f 1` CPU_IOWAIT_MAJOR=`echo $CPU_IOWAIT | cut -d "." -f 1` CPU_IDLE_MAJOR=`echo $CPU_IDLE | cut -d "." -f 1`
 * 1) Add for integer shell issue

if [ ${CPU_USER_MAJOR} -ge $USER_CRITICAL_THRESHOLD ]; then echo "CPU STATISTICS CRITICAL : ${NAGIOS_DATA}" exit $STATE_CRITICAL elif [ ${CPU_SYSTEM_MAJOR} -ge $SYSTEM_CRITICAL_THRESHOLD ]; then echo "CPU STATISTICS CRITICAL : ${NAGIOS_DATA}" exit $STATE_CRITICAL elif [ ${CPU_IOWAIT_MAJOR} -ge $IOWAIT_CRITICAL_THRESHOLD ]; then echo "CPU STATISTICS CRITICAL : ${NAGIOS_DATA}" exit $STATE_CRITICAL elif [ ${CPU_USER_MAJOR} -ge $USER_WARNING_THRESHOLD ] && [ ${CPU_USER_MAJOR} -lt $USER_CRITICAL_THRESHOLD ]; then echo "CPU STATISTICS WARNING : ${NAGIOS_DATA}" exit $STATE_WARNING elif [ ${CPU_SYSTEM_MAJOR} -ge $SYSTEM_WARNING_THRESHOLD ] && [ ${CPU_SYSTEM_MAJOR} -lt $SYSTEM_CRITICAL_THRESHOLD ]; then echo "CPU STATISTICS WARNING : ${NAGIOS_DATA}" exit $STATE_WARNING elif [ ${CPU_IOWAIT_MAJOR} -ge $IOWAIT_WARNING_THRESHOLD ] && [ ${CPU_IOWAIT_MAJOR} -lt $IOWAIT_CRITICAL_THRESHOLD ]; then echo "CPU STATISTICS WARNING : ${NAGIOS_DATA}" exit $STATE_WARNING else echo "CPU STATISTICS OK : ${NAGIOS_DATA}" exit $STATE_OK fi
 * 1) Return