如何使用 check_nt 命令而不是使用百分比值来检查 Nagios 中的确切空间量(MB 或 GB)?

如何使用 check_nt 命令而不是使用百分比值来检查 Nagios 中的确切空间量(MB 或 GB)?

如何使用 check_nt 命令而不是百分比值来检查 Nagios 中剩余的确切空间量(MB 或 GB)?

目前我正在使用这个命令:

 check_command           check_nt!USEDDISKSPACE!-l c -w 90 -c 95

但这是检查已用磁盘空间的百分比值,并且我想在某个驱动器上剩余确切的 GB 数量时收到通知(例如,当剩余 10GB 时发出警告,当仅剩 5GB 时发出严重警告)。

谢谢

答案1

check_nt!USEDDISKSPACE返回磁盘使用量的大小和百分比。但阈值是百分比。

如果您想要根据大小接收警报,您可以为check_nt命令编写一个包装器 shell 脚本,例如check_disk_by_size

#!/bin/bash

FREESPACE=`/usr/local/nagios/libexec/check_nt -H $2 -p 12489 -s pa$$word \
-v USEDDISKSPACE -l $4 | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [ $UNIT == "Gb" ]; then
    SIZE=`echo $SIZE \* 1024 | bc`
fi

if [ `echo "$SIZE >= $6" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space OK - $FREESPACE"
    exit 0
elif [ `echo "$SIZE < $6" | bc` -eq 1 -a `echo "$SIZE > $8" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space WARNING - $FREESPACE"
    exit 1
elif [ `echo "$SIZE <= $8" | bc` -eq 1 ]; then
    echo "$4:\_Drive_Space CRITICAL - $FREESPACE"
    exit 2
fi

测试:

$ check_disk_by_size.sh -H 192.168.6.31 -l c -w 10240 -c 5120
c:\_Drive_Space OK - free 13.01 Gb (36%)

$ check_disk_by_size.sh -H 192.168.6.31 -l c -w 14240 -c 5120
c:\_Drive_Space WARNING - free 13.01 Gb (36%)

$ check_disk_by_size.sh -H 192.168.6.31 -l c -w 16240 -c 15120
c:\_Drive_Space CRITICAL - free 13.01 Gb (36%)

您可以像这样将其添加到 Nagios:

"check_disk_by_size.sh".

# 'check_disk_by_size' command definition
define command{
       command_name    check_disk_by_size
       command_line    $USER1$/check_disk_by_size.sh -H $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
       }

define service{
        use                     generic-service
        host_name               fileserver1
        service_description     Drive Space fileserver1: L:
        check_command           check_disk_by_size!-l L -w 4096 -c 2048
        notifications_enabled   1
        }

答案2

我已经修改了代码以适应新的 BASH 语法和更强大的检查。

/bin/bash #!/bin/bash

usage() { echo "$0 check_nt -H 主机 [-p 端口] [-w 警告] [-c 严重] [-l 参数] [-t 超时]" 1>&2; 退出 1; }

while getopts ":H:p:l:w:c:" opt; 执行
    案例“${opt}”
        H)主机名=$OPTARG;;
        p)端口=$OPTARG;;
        l)DISC = $ OPTARG;;
        w) WARN_THRESHOLD=$OPTARG;;
        c)CRITICAL_THRESHOLD = $OPTARG;;
#t)TIME_OUT = $OPTARG;;
        \?) echo "无效选​​项:-$OPTARG" >&2; exit 1 ;;
        : ) echo "选项 -$OPTARG 需要一个参数。" >&2; exit 1;;
    埃萨克
完毕
移位 $((OPTIND-1))

如果 [[ -z "$HOST_NAME" ]] || [[ -z "$PORT" ]] || [[ -z "$DISC" ]] || [[ -z "$WARN_THRESHOLD" ]] || [[ -z "$CRITICAL_THRESHOLD" ]] ; 然后
    用法

FREESPACE =`/usr/local/nagios/libexec/check_nt -H $HOST_NAME -p $PORT \
-v USEDDISKSPACE -l $DISC | awk -F"- " '{打印 $4}' | awk -F "|" '{打印 $1}'`

SIZE=`echo $FREESPACE | awk'{打印$2}'`
UNIT=`echo $FREESPACE | awk'{打印$3}'`

如果 [[ $UNIT == "Gb" ]];那么
    SIZE=`echo $SIZE \* 1024 | bc`

如果 [[ `echo "$SIZE >= $WARN_THRESHOLD" | bc` -eq 1 ]];那么
    回显“$DISC:\_Drive_Space 确定 - $FREESPACE”
    出口 0
elif [[ `echo "$SIZE $CRITICAL_THRESHOLD" | bc` -eq 1 ]];然后
    echo "$DISC:\_Drive_Space 警告 - $FREESPACE"
    出口 1
elif [[`echo“$SIZE

答案3

我修改了 Gnought 的脚本,它是对 quanta 脚本的进一步修改,以消除一些代码错误,并修改结果以更接近 check_nt 的原始输出,这在分析随时间变化的趋势时可以提供更好的数据以及重新添加的性能数据。

#!/bin/bash
# Date: 2015-06-30
# Purpose: A wrapper script for check_nt to set threshold for exact space
#          free rather than just percentage.  Useful on VERY large drives.
# Example: check_disk_by_size.sh -H 192.168.0.1 -l c -w 10240 -c 5120

usage() { echo "$0 -H host [-s password] [-p port] [-w warning] [-c critical] [-l params]" 1>&2; exit 1; }

while getopts ":H:s:p:l:w:c:" opt; do
    case "${opt}" in
        H ) HOST_NAME=$OPTARG;;
        s ) PASSW0RD=$OPTARG;;
        p ) PORT=$OPTARG;;
        l ) DISC=$OPTARG;;
        w ) WARN_THRESHOLD=$OPTARG;;
        c ) CRITICAL_THRESHOLD=$OPTARG;;
        \?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
        : ) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
    esac
done
shift $((OPTIND-1))

if [[ -z "${HOST_NAME}" ]] || [[ -z "${PASSW0RD}" ]] || [[ -z "${PORT}" ]] || [[ -z "${DISC}" ]] || [[ -z "${WARN_THRESHOLD}" ]] || [[ -z "${CRITICAL_THRESHOLD}" ]] ; then
    usage
fi

CHECKRESULT=`/usr/local/nagios/libexec/check_nt -H ${HOST_NAME} -p ${PORT} -s ${PASSW0RD} -v USEDDISKSPACE -l ${DISC}`
PERFDATA=`echo ${CHECKRESULT} | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $2 }'`
FREESPACE=`echo ${CHECKRESULT} | awk -F"- " '{ print $4 }' | awk -F "|" '{ print $1 }'`
USEDSPACE=`echo ${CHECKRESULT} | awk -F"- " '{ print $3 }'`
TOTALSPACE=`echo ${CHECKRESULT} | awk -F"- " '{ print $2 }'`

if [[ -z ${FREESPACE} ]]; then
    ## Command failed or server offline
    echo "ERROR ERROR: Command failed"
    exit 1
fi

SIZE=`echo $FREESPACE | awk '{ print $2 }'`
UNIT=`echo $FREESPACE | awk '{ print $3 }'`

if [[ ${UNIT} == "Gb" ]]; then
    SIZE=`echo ${SIZE} \* 1024 | bc`
fi

if [[ `echo "${SIZE} >= ${WARN_THRESHOLD}" | bc` -eq 1 ]]; then
    echo "${DISC}:\ OK - ${TOTALSPACE} - ${USEDSPACE} - ${FREESPACE} | ${PERFDATA}"
    exit 0
elif [[ `echo "${SIZE} < ${WARN_THRESHOLD}" | bc` -eq 1 && `echo "${SIZE} > ${CRITICAL_THRESHOLD}" | bc` -eq 1 ]]; then
    echo "${DISC}:\ WARNING - ${TOTALSPACE} - ${USEDSPACE} - ${FREESPACE} | ${PERFDATA}"
    exit 1
elif [[ `echo "${SIZE} <= ${CRITICAL_THRESHOLD}" | bc` -eq 1 ]]; then
    echo "${DISC}:\ CRITICAL - ${TOTALSPACE} - ${USEDSPACE} - ${FREESPACE} | ${PERFDATA}"
    exit 2
fi

相关内容