nagios 自带的监控load 的由于不同型号的机器需要区别设置参数,很不方便,使用使用百分比可以不用考虑机器的cpu核数
#!/bin/bash ########################################################## #[email protected] load_info # #[email protected] # ########################################################## #set -o nounset set -o errexit if [ $# -ne 4 ];then echo "sh `basename $0` -w[args] -c[args]" exit 2 fi while getopts :w:c: opt do case $opt in w) Warng_threshold=$OPTARG ;; c) Critical_threshold=$OPTARG ;; h) echo "Usage:sh $0 -w 80 -c 100" exit 3 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 3 ;; \?) echo "sh `basename $0` -w[args] -c[args]" exit 3 ;; esac done if [ -z "$Warng_threshold" ] || [ -z "$Critical_threshold" ];then # echo "sh `basename $0` -w[args] -c[args]" exit 2 fi int_War_threshold=$(echo $Warng_threshold|awk ‘{print $1*100}‘) int_Cri_threshold=$(echo $Critical_threshold|awk ‘{print $1*100}‘) load_OK=0 load_WARNING=1 load_CRITICAL=2 load_UNKNOWN=3 _load_1min=$(cat /proc/loadavg |awk ‘{print $1}‘) _load_5min=$(cat /proc/loadavg |awk ‘{print $2}‘) _load_15min=$(cat /proc/loadavg |awk ‘{print $3}‘) _cpu_core=$(cat /proc/cpuinfo |grep processor|wc -l) function get_percent() { local load_value=$1 #local temp_value=$(echo "scale=4;${load_value}/${_cpu_core}"|bc) #_value=$(echo "$temp_value*100"|bc|awk ‘{printf "%2.2f\n",$0}‘) local _value=$(echo "$load_value"|awk ‘{printf "%2.2f\n",$1/_cpu_core*100}‘ _cpu_core=${_cpu_core}) printf ${_value} } min1_load=$(get_percent ${_load_1min}) min5_load=$(get_percent ${_load_5min}) min15_load=$(get_percent ${_load_15min}) #echo "$min1_load,$min5_load,$min15_load" int_min1_load=$(echo $min1_load|awk ‘{print $1*100}‘) int_min5_load=$(echo $min5_load|awk ‘{print $1*100}‘) int_min15_load=$(echo $min15_load|awk ‘{print $1*100}‘) if [ $int_min1_load -lt $int_War_threshold ] && [ $int_min5_load -lt $int_War_threshold ] && [ $int_min15_load -lt $int_War_threshold ];then echo "OK" exit $load_OK elif [ $int_min1_load -gt $int_Cri_threshold ] || [ $int_min5_load -gt $int_Cri_threshold ] || [ $int_min15_load -gt $int_Cri_threshold ];then echo "CRITICAL: min1_load:${min1_load},min5_load:${min5_load},min15_load:${min15_load}" exit $load_CRITICAL else echo "WARNING: min1_load:${min1_load},min5_load:${min5_load},min15_load:${min15_load}" exit $load_WARNING fi
时间: 2024-10-13 21:29:35