现在的位置: 首页 > 综合 > 正文

HowToUseSNMPAgentExtension

2017年12月20日 ⁄ 综合 ⁄ 共 4029字 ⁄ 字号 评论关闭

 Introduction

SNMP can help us to grab useful hardware and software information from a given device.In some cases it would be useful to query more data than what is provided by the MIB files. A common solution may be to use an additional agent like NRPE or NSClient on
the node, but this needs one more deamon on each monitored server, which need to be installed and configured, and it opens one more port.

Fortunately, the SNMP deamon from the
Net-SNMP
package allows us to extend the functionalityby executing custom commands on the managed server to collect their exit code and output. According the the snmpd.conf man pagethere are several machanisms to extend the agent. They include:

  • running external commands (exec, extend, pass)
  • loading new code dynamically (embedded perl, dlmod)
  • communicating with other agents (proxy, SMUX, AgentX)

In this HowTo only exec and extend are handled. The exec statement should work on all Net-SNMP versions. Starting with Net-SNMP 5.0 extend was introduced, which providesmore features and is faster.The steps to configure are decribed below:

Variant A - exec


First, we need to tell the agent which script he has to keep available on request. It's quite simple.

  • Place the local script you want to use into a directory. Warning : choose this folder carefully in order to be sure that the script won't move, or won't be deleted !!
  • Edit your agent standard configuration file named snmpd.conf to add a line like this :
exec <Script_Name> <Script_Full_Path> <Script_Arguments>

Be aware to edit the standard snmpd.conf, and not the persistent one. For example with a compiled Net-SNMP 5.4, my standard file is/usr/local/share/snmpd/snmpd.conf, and the persistent one is/var/net-snmp/snmpd.conf

  • Then you need to restart your SNMP daemon in order to take change into account.

Name and result of the script will automatically be rooted under OID .1.3.6.1.4.1.2021.8, that is to say the "Net-SNMP Agent Extension Table" (calledextTable).To run the script, you just have to read OID
extTable.extIndex
Each script corresponding to an exec token has its own index, depending of the declaration order.

Then you can grab the results :

  • exit code of the script is OID extTable.extIndex.extResult (.1.3.6.1.4.1.2021.8.index.4.1)
  • output string of the script is OID extTable.extIndex.extOutput (.1.3.6.1.4.1.2021.8.index.5.1)

Results are automatically cached for 30 seconds by the agent.

Example 1


I've used this feature to monitor hard drive health :

  • the
    SmartMonTools
    package need to be installed on the system
  • I use the check_smart.pl local perl script to easy check hard drive health by runningS.M.A.R.T. self-tests using the
    smartctl command, so I've placed it into a/root/snmp_local_scripts folder
  • I add this exec token into my /usr/local/share/snmp/snmpd.conf :
exec CheckSmart /root/snmp_local_scripts/check_smart.pl -t -d /dev/hda
  • finally I could read the extTable of the agent :
# snmpwalk -v3 -u login -n "" -l authNoPriv -A password monIP .1.3.6.1.4.1.2021.8.1
UCD-SNMP-MIB::extIndex.1 = INTEGER: 1
UCD-SNMP-MIB::extNames.1 = STRING: CheckSmart
UCD-SNMP-MIB::extCommand.1 = STRING: /root/check_smart.pl
UCD-SNMP-MIB::extResult.1 = INTEGER: 0
UCD-SNMP-MIB::extOutput.1 = STRING: SMART overall-health self-assessment test result: PASSED
UCD-SNMP-MIB::extErrFix.1 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFixCmd.1 = STRING:

Variant B - extend


This description is based on the HowTo from
Michal Ludvig.
You can find more examples on his page. Here only the basic configurationis provided.

  1. On the remote server configure date extension in /etc/snmp/snmpd.conf. For every command you want to add, simply add a line at the end of the config file and reload snmpd. The syntax is:
extend <OIDName> <Command_Full_Path> <Arguments>
  1. From any client that has allowed SNMP access to the server query the datecheck with:
 ~$ snmpwalk -v2c -c public remote.server NET-SNMP-EXTEND-MIB::nsExtendOutputFull
 NET-SNMP-EXTEND-MIB::nsExtendOutputFull."datecheck" = STRING: Wed Oct 18 00:01:44 NZDT 2006

That's it. Easy way to run programs and scripts remotely, isn't it?

Example 2


In this example we add a command for checking the date on the remote node. Add a line like the following one to snmpd.conf:

extend datecheck /bin/date

For integration with Nagios use Michal's script
check_snmp_extend.sh
and save itto your nagios plugins path (ex.: /usr/local/nagios/libexec). Add a command and a service definition to Nagios/Centreon:

define command{
	command_name	check_snmp_extend
	command_line	$USER1$/check_snmp_extend.sh $HOSTADDRESS$ $ARG1$
}

define service{
	use			generic-service
	host_name		remote.server
	service_description	SomeService status
	check_command		check_snmp_extend!servicename
}

抱歉!评论已关闭.