This script references the official Nagios document https://exchange.nagios.org/and its code refinement and shift values, the original code is run under Ksh, the way to define the array is different from bash, in addition, in order to reflect the CPU's busy level, The average of the Vmstart command is captured and printed. In order to facilitate everyone to download, special will be included in my GitHub.
#!/bin/bash# cpu utilization statistics plugin for nagiosiostat= "/usr/bin/iostat "# nagios return codesstate_ok=0state_warning=1state_critical=2state_unknown=3# plugin parameters value if not define list_warning_threshold= "70,40,30" LIST_CRITICAL_ Threshold= "90,60,40" interval_sec=1num_report=3# plugin variable descriptionprogname=$ (basename $0) if [ ! -x $IOSTAT ]; then echo "UNKNOWN: iostat not found or is not executable by the nagios User. " exit $STATE _unknownfiprint_usage () { echo " echo " $PROGNAME $RELEASE - cpu utilization check script for nagios " echo&nbSP; "" echo "Usage: check_cpu_utili.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 threshold in % for crit_user,crit_system,crit_iowait CPU (default : 90,60,40) " 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 in %) " echo " " exit 0}# parse parameterswhile [ $# -gt 0 ]; do case "$ " 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 ;; &nBsp; -n | --number) shift NUM_REPORT=$1 ;; *) echo "Unknown argument: $1" print_usage exit $STATE _ unknown ;; esacshiftdone# List to Table for warning threshold (compatibility with tab_warning_threshold= (' echo $LIST _warning_ Threshold | sed&nbsP; ' S/,/ /g ") #declare -a TAB_WARNIG_THRESHOLDif [ " ${#TAB_WARNING_THRESHOLD [@]} " - ne "3" ]; then echo "Error : bad count parameter in" warning threshold " exit $STATE _warningelseuser_warning_threshold= ' echo ${tab_ Warning_threshold[0]} ' system_warning_threshold= ' echo ${tab_warning_threshold[1]} ' IOWAIT_WARNING_THRESHOLD = ' echo ${tab_warning_threshold[2]} ' fi# List to Table for critical Thresholdtab_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 _ Warningelseuser_critical_threshold= ' echo ${tab_critical_threshold[0]} ' system_critical_threshold= ' echo ${tab_critical_thrESHOLD[1]} ' iowait_critical_threshold= ' echo ${tab_critical_threshold[2]} ' Fiif [ ${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 _warningficpu_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_system= ' echo $CPU _report | cut -d "; -f 4 ' cpu_iowait= ' echo $CPU _report | cut -d '; -f 5 ' 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}% "# add for integer shell issuecpu_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 ' cpu_vmstat_r= ' vmstat 1 4 | sed -n ' 3,$ ' P | awk ' BEGINE{SUM=0} {SUM += $1} END {PRINT SUM/4} ' ' # returnif [ ${cpu_user_major} -ge $USER _critical_threshold ]; then echo "cpu statistics ok : ${nagios_data} the averange Value of r in vmstat :${cpu_vmstat_r} " exit $STATE _critical elif [ ${cpu_system_major} -ge $ system_critical_threshold ]; then echo "CPU statistics ok : ${nagios_data} the averange value of r in vmstat :${cpu_vmstat_r} " exit $STATE _ critical elif [ ${cpu_iowait_major} -ge $IOWAIT _critical_threshold ]; then echo "cpu statistics ok : ${nagios_data} the averange value of r in vmstat :${cpu_vmstat_r} " 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} " echo " Cpu statistics ok : ${nagios_data} the averange value of r in vmstat :${cpu_vmstat_r} " 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} " echo " cpu statistics ok : ${ Nagios_data} the averange value of r in vmstat :${cpu_vmstat_r} " 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}" echo "Cpu statistics ok : ${nagios_ Data} the averange value of r in vmstat :${cpu_vmstat_r} " exit $STATE _warningelse #echo "the averange value of r in vmstat :${cpu_vmstat_r}\c " echo "cpu statistics ok : ${nagios_data}; The averange value of r in vmstat:${cpu_vmstat_r} " exit $STATE _OKFI
Put the script in the server-side/usr/local/nagios/libexec/directory as the Nagios plugin, and the effect is as follows:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/EF/wKiom1WtseqjP5JwAACkN4khlog993.jpg "title=" QQ picture 20150721085935.png "alt=" Wkiom1wtseqjp5jwaackn4khlog993.jpg "/>
This article is from the "Fuqin Wine" blog, please make sure to keep this source http://yuhongchun.blog.51cto.com/1604432/1676639
CPU Utilization Plugin for Nagios v1.0