Linux下snmpd.conf说明–扩展B:可执行脚本

snmpd.conf通过exec支持直接对于执行指令或是执行脚本的返回结果的SNMP查询。

但是有两种情况:

1)只可以SNMP查询出单行(首行))返回结果。

2)可以SNMP查询出多行(全部)返回结果。

下面先说第一种情况:只能查单行(首行)返回结果

配置格式:

#exec NAME PROGRAM [ARGS ...]

#

#NAME: A generic name. The name must be unique for each exec statement.

#PROGRAM: The program to run. Include the path!

#ARGS: optional arguments to be passed to the program

可以通过外部命令捕获系统信息,并保存在mib中。

第一种格式中,该信息保存在enterprises分支中的extEntry表中具体的OID为:.1.3.6.1.4.1.2021.8.1

先介绍一下snmpd.conf中自带的示例:

exec echotest /bin/echo hello world

exec shelltest /bin/sh /tmp/shtest

#cat /tmp/shtest

##!/bin/sh

#echo hello world

#echo hi there

#exit 35

可以看出,两行exec中,第一行是单行输出,第二行执行的脚本是多行(两行)输出,下面是示例给出的结果:

#% snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.8

#enterprises.ucdavis.extTable.extEntry.extIndex.1 = 1

#enterprises.ucdavis.extTable.extEntry.extIndex.2 = 2

#enterprises.ucdavis.extTable.extEntry.extNames.1 = "echotest"

#enterprises.ucdavis.extTable.extEntry.extNames.2 = "shelltest"

#enterprises.ucdavis.extTable.extEntry.extCommand.1 = "/bin/echo hello world"

#enterprises.ucdavis.extTable.extEntry.extCommand.2 = "/bin/sh /tmp/shtest"

#enterprises.ucdavis.extTable.extEntry.extResult.1 = 0

#enterprises.ucdavis.extTable.extEntry.extResult.2 = 35

#enterprises.ucdavis.extTable.extEntry.extOutput.1 = "hello world."

#enterprises.ucdavis.extTable.extEntry.extOutput.2 = "hello world."

#enterprises.ucdavis.extTable.extEntry.extErrFix.1 = 0

#enterprises.ucdavis.extTable.extEntry.extErrFix.2 = 0

从示例给出的结果可以证实:

此种方法,只能查询出所执行脚本或指令所得到的第一行返回结果。

在snmpd.conf加入:

exec uptime /usr/bin/uptime   //只有一行返回结果

exec who /usr/bin/who //可以返回多结果的脚本

查询结果:

UCD-SNMP-MIB::extIndex.1 = INTEGER: 1

UCD-SNMP-MIB::extIndex.2 = INTEGER: 2

UCD-SNMP-MIB::extNames.1 = STRING: uptime

UCD-SNMP-MIB::extNames.2 = STRING: who

UCD-SNMP-MIB::extCommand.1 = STRING: /usr/bin/uptime

UCD-SNMP-MIB::extCommand.2 = STRING: /usr/bin/who

UCD-SNMP-MIB::extResult.1 = INTEGER: 0

UCD-SNMP-MIB::extResult.2 = INTEGER: 0

UCD-SNMP-MIB::extOutput.1 = STRING: 18:18:56 up 2:33, 4 users, load average: 0.00, 0.00, 0.00

UCD-SNMP-MIB::extOutput.2 = STRING: root tty1 2012-09-20 15:46

UCD-SNMP-MIB::extErrFix.1 = INTEGER: noError(0)

UCD-SNMP-MIB::extErrFix.2 = INTEGER: noError(0)

UCD-SNMP-MIB::extErrFixCmd.1 = STRING:

UCD-SNMP-MIB::extErrFixCmd.2 = STRING:

可以执行,并且extOutput确实也只是输出了第一行返回结果。

下面先说第二种情况:可以SNMP查询出多行(全部)返回结果此格式,将该信息保存在指定的MIB中,这个MIB由自己指定空闲的MIB OID,也可以覆盖已经定义的MIB OID,MIB后面要跟label,作为一个标识符在配置文件中添加。

还是先看一下snmpd.conf自带的示例:

###############################################################################

#Extensible sections.

#exec .1.3.6.1.4.1.2021.50 shelltest /bin/sh /tmp/shtest

#cat /tmp/shtest

##!/bin/sh

#echo hello world

#echo hi there

#exit 35

与第一种情况不同的是:exec后,指定了OID,返回的结果如下:

#% snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.50

#enterprises.ucdavis.50.1.1 = 1

#enterprises.ucdavis.50.2.1 = "shelltest"

#enterprises.ucdavis.50.3.1 = "/bin/sh /tmp/shtest"

#enterprises.ucdavis.50.100.1 = 35

#enterprises.ucdavis.50.101.1 = "hello world."

#enterprises.ucdavis.50.101.2 = "hi there."

#enterprises.ucdavis.50.102.1 = 0

可以看出这种方式是可以查询到多行(全部)的返回结果。另外,同是也可以直接使用系统命令。如示例:

#exec .1.3.6.1.4.1.2021.51 ps /bin/ps

#exec .1.3.6.1.4.1.2021.52 top /usr/local/bin/top

#exec .1.3.6.1.4.1.2021.53 mailq /usr/bin/mailq

实际操作一下第二种情况:

exec .1.3.6.1.4.1.2021.51 ps /bin/ps

进行snmpwalk查询:

#snmpwalk -v2c -cpublic 192.168.1.21 .1.3.6.1.4.1.2021.51

UCD-SNMP-MIB::ucdavis.51 = No Such Object available on this agent at this OID

可以发现是找不到.1.3.6.1.4.1.2021.51这个OID的。

这是什么原因呢?

如果出现这种情况,请先查看一下snmpd的版本。

#snmpd -v

NET-SNMP version: 5.5

上面的配置方式是旧版的配置,新版本已经不用此格式了。新的格式是:

extend .1.3.6.1.4.1.2021.51ps

/bin/ps

重启动snmpd后,现进行snmpwalk查询:

#snmpwalk -v2c -cpublic 192.168.1.21 .1.3.6.1.4.1.2021.51 |more

UCD-SNMP-MIB::ucdavis.51.1.0 = INTEGER: 1

UCD-SNMP-MIB::ucdavis.51.2.1.2.2.112.115 = STRING: "/bin/ps"

UCD-SNMP-MIB::ucdavis.51.2.1.3.2.112.115 = ""

UCD-SNMP-MIB::ucdavis.51.2.1.4.2.112.115 = ""

UCD-SNMP-MIB::ucdavis.51.2.1.5.2.112.115 = INTEGER: 5

UCD-SNMP-MIB::ucdavis.51.2.1.6.2.112.115 = INTEGER: 1

UCD-SNMP-MIB::ucdavis.51.2.1.7.2.112.115 = INTEGER: 1

UCD-SNMP-MIB::ucdavis.51.2.1.20.2.112.115 = INTEGER: 4

UCD-SNMP-MIB::ucdavis.51.2.1.21.2.112.115 = INTEGER: 1

UCD-SNMP-MIB::ucdavis.51.3.1.1.2.112.115 = STRING: " PID TTY TIME CMD"

UCD-SNMP-MIB::ucdavis.51.3.1.2.2.112.115 = STRING: " PID TTY TIME CMD

.............

可以发现已经可查询出结果了。