新Nagios BASH插件输出错误:“此OID中当前存在这样的实例:应为整数表达式”

新Nagios BASH插件输出错误:“此OID中当前存在这样的实例:应为整数表达式”,bash,nagios,icinga,icinga2,Bash,Nagios,Icinga,Icinga2,我正在尝试编写我的第一个Nagios插件来检查WLAN控制器AP上的状态。目标是制作一种通用插件,但我遇到了一个错误: .1.3.6.1.4.1.14179.2.2.1.1.3.0.: Unknown Object Identifier () /usr/lib/nagios/plugins/check_wlc_ap_state.sh: line 50: [: Such Instance currently exists at this OID: integer expression expec

我正在尝试编写我的第一个Nagios插件来检查WLAN控制器AP上的状态。目标是制作一种通用插件,但我遇到了一个错误:

.1.3.6.1.4.1.14179.2.2.1.1.3.0.: Unknown Object Identifier ()

/usr/lib/nagios/plugins/check_wlc_ap_state.sh: line 50: [: Such Instance currently exists at this OID: integer expression expected
/usr/lib/nagios/plugins/check_wlc_ap_state.sh: line 53: [: Such Instance currently exists at this OID: integer expression expected
/usr/lib/nagios/plugins/check_wlc_ap_state.sh: line 56: [: Such Instance currently exists at this OID: integer expression expected
UNKOWN-  = Such Instance currently exists at this OID
这是我的密码:

#!/bin/bash

while getopts "H:C:O:N:I:w:c:h" option; do
        case "$option" in
                H )     host_address=$OPTARG;;
                C )     host_community=$OPTARG;;
                O )     ap_op_status_oid=$OPTARG;;
                N )     ap_hostname_oid=$OPTARG;;
                w )     warn_thresh=$OPTARG;;
                c )     crit_thresh=$OPTARG;;
                h )     show_help="yes";;

        esac
done

# Help Menu
help_menu="Plugin to check AP operational status.
Example:
Check AP Status on Cisco CS5508
./check_wlc_ap_state.sh -H [Controller IP] -C [Controller Community] -O .1.3.6.1.4.1.14179.2.2.1.1.6.0 -N .1.3.6.1.4.1.14179.2.2.1.1.3.0 -w 2 -c 3

Required Arguments:
-H      WLAN Controller Address
-C      WLAN Controller RO Community String
-O      OID to AP Operation Status
-N      OID to AP Hostname
-c      Critical Threshold
-w      Warning Threshold

Optional Arguments:
-h      Display help 
"

if [ "$show_help" = "yes" -o $# -lt 1 ]; then
  echo "$help_menu"
  exit 0
fi

# Change the .1. to iso. and get the length + 1 to get rid of the trailing .
ap_op_status_oid=${ap_op_status_oid:2}
ap_op_status_oid="iso$ap_op_status_oid"
ap_op_status_oid_length=${#ap_op_status_oid}
ap_op_status_oid_length="$ap_op_status_oid_length+1"

#Get info
while read -r oid_index equal integer ap_stat;
do
        ap_index="${oid_index:$ap_op_status_oid_length}"
        ap_hostname=$(snmpget -c $host_community $host_address -v 1 $ap_hostname_oid.$ap_index | awk -F '"' '{print$2}')
        if [ "$ap_stat" -lt "$warn_thresh" ]; then
                echo -n "OK- $ap_hostname = $ap_stat | "
                exit 0;
        elif [ "$ap_stat" -eq "$warn_thresh" ]; then
                echo -n "WARNING- $ap_hostname = $ap_stat | "
                exit 1;
        elif [ "$ap_stat" -ge "$crit_thresh" ]; then
                echo -n "CRITICAL- $ap_hostname = $ap_stat | "
                exit 2;
        else
                echo -n "UNKOWN- $ap_hostname = $ap_stat | "
                exit 3;
        fi

done < <(snmpwalk -c $host_community -v 2c $host_address $ap_op_status_oid)
编辑:这是集合-x

:40+ap_op_status_oid=.3.6.1.4.1.14179.2.2.1.1.6.0
:41+ap_op_status_oid=iso.3.6.1.4.1.14179.2.2.1.1.6.0
:42+ap_op_status_oid_length=31
:43+ap_op_status_oid_length=32
:46+read -r oid_index equal integer ap_stat
::69+snmpwalk -c public -v 2c 10.77.208.12 iso.3.6.1.4.1.14179.2.2.1.1.6.0
:48+oid_index=iso.3.6.1.4.1.14179.2.2.1.1.6.0.129.196.3.1.112
:49+equal==
:50+integer=INTEGER:
:51+ap_stat=1
:52+ap_index=129.196.3.1.112
::53+awk -F '"' '{print$2}'
::53+snmpget -c public 10.77.208.12 -v 1 .1.3.6.1.4.1.14179.2.2.1.1.3.0.129.196.3.1.112
:53+ap_hostname=AP-01
:55+'[' 1 -lt 2 ']'
:56+echo -n 'OK- AP-01 = 1 | '
OK- AP-01 = 1 | :57+exit 0
ap_stat包含非数字内容。您的代码正在将其传递给一个测试操作符,该操作符要求将其解析为数字


使用set-x跟踪脚本的执行;这将使这种情况更容易诊断。

您有一个大字符串要传递给数值测试。那不行。顺便说一句,你有一堆引用错误。运行代码并修复找到的内容。顺便问一下,您是否希望ap_op_status_oid_length=$ap_op_status_oid_length+1是一个数学运算?事实并非如此。也许你想要ap_op_status_oid_length=$ap_op_status_oid_length+1,或者++ap_op_status_oid_length——后者是Bashim,前者是POSIX兼容的。另外,在堆栈溢出问题中编写代码时,尝试生成一个最小值,这意味着它不包含像help这样的无关组件,完整且可验证意味着其他人可以运行它来查看您的问题和/或确定他们的修复程序是否有效-这意味着,例如,可能将snmpget或snmpwalk硬编码为具有硬编码输出的函数,或者如果您可以这样做但仍然显示问题,则直接删除它们。此外,echo-n实际上取决于实现定义的行为。建议在新代码中改用printf:printf“%s”这里的字符串比echo-n这里的字符串更可靠、更可移植。请参阅上述链接的“应用程序用法和基本原理”部分,了解echo的可移植性受到限制的原因,除非是在非常有限的参数域内。ap_stat仅返回1显然不是在出现此错误的情况下。同样,通过运行PS4=':$LINENO+'bash-xyourscript来收集set-x日志。@red_eagle,…我认为snmpwalk返回了一个错误。您正在将该错误的第一个字读取到oid_索引中,第二个字读取到equal中,第三个字读取到integer中,其余的字最终读取到ap_stat中。@red_eagle,…在使用set-x时记录由read填充的变量的内容,我经常在我的循环中添加一行,看起来像::oid_index=$oid_index equal=$equal integer=$integer ap_stat=$ap_stat-因为:是true的同义词,在不处于调试模式时它什么都不做,但是当您使用set-x运行时,它会显示您的相关值,set-x输出没有显示实际发出错误的实例。这是相当重要的。
:40+ap_op_status_oid=.3.6.1.4.1.14179.2.2.1.1.6.0
:41+ap_op_status_oid=iso.3.6.1.4.1.14179.2.2.1.1.6.0
:42+ap_op_status_oid_length=31
:43+ap_op_status_oid_length=32
:46+read -r oid_index equal integer ap_stat
::69+snmpwalk -c public -v 2c 10.77.208.12 iso.3.6.1.4.1.14179.2.2.1.1.6.0
:48+oid_index=iso.3.6.1.4.1.14179.2.2.1.1.6.0.129.196.3.1.112
:49+equal==
:50+integer=INTEGER:
:51+ap_stat=1
:52+ap_index=129.196.3.1.112
::53+awk -F '"' '{print$2}'
::53+snmpget -c public 10.77.208.12 -v 1 .1.3.6.1.4.1.14179.2.2.1.1.3.0.129.196.3.1.112
:53+ap_hostname=AP-01
:55+'[' 1 -lt 2 ']'
:56+echo -n 'OK- AP-01 = 1 | '
OK- AP-01 = 1 | :57+exit 0