Extending the Net-SNMP agent

Extending the Net-SNMP agent

Extending the Net-SNMP agent 150 150 Roderick Derks

Extending the Net-SNMP agent

A great functionnality of Net-SNMP is that you can “extend” it.

Let’s run the /tmp/foo.sh script :

$ /tmp/foo.sh -arg1

Now put this in snmpd.conf :

exec foo /bin/sh /tmp/foo.sh -arg1

The result of your script will be accessible under the ucdavis.extTable.extEntry tree :

  • output of the script : ucdavis.extTable.extEntry.extOutput
  • exit status : ucdavis.extTable.extEntry.extResult
  • command : ucdavis.extTable.extEntry.extCommand

You can check the result with this SNMP query :

$ snmpwalk -v 1 -c public localhost .
UCD-SNMP-MIB::extIndex.1 = INTEGER: 1
UCD-SNMP-MIB::extNames.1 = STRING: foo
UCD-SNMP-MIB::extCommand.1 = STRING: /bin/sh /tmp/foo.sh -arg1
UCD-SNMP-MIB::extResult.1 = INTEGER: 0
UCD-SNMP-MIB::extOutput.1 = STRING: 123
UCD-SNMP-MIB::extErrFix.1 = INTEGER: 0
UCD-SNMP-MIB::extErrFixCmd.1 = STRING:

extOutput translates to .
As “foo” is our first exec directive, add “.1” at the end of the OID.

In Cacti, use the “SNMP – Generic OID Template” like this :

Voila ! Result of the /tmp/foo.sh script is now graphed in Cacti.

Now let’s run this second script, which returns more than one result :

$ /tmp/bar.sh

It returns two values, one per line (this is important).

Another way to call scripts from snmpd.conf is by specifying an OID, like this :

exec . /bin/sh /tmp/bar.sh

Run this query :

$ snmpwalk -v 1 -c public localhost .
UCD-SNMP-MIB::ucdavis.555.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.555.2.1 = STRING: "/bin/sh"
UCD-SNMP-MIB::ucdavis.555.3.1 = STRING: "/tmp/bar.sh"
UCD-SNMP-MIB::ucdavis.555.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.555.101.1 = STRING: "456"
UCD-SNMP-MIB::ucdavis.555.101.2 = STRING: "789"
UCD-SNMP-MIB::ucdavis.555.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.555.103.1 = ""

First line returned by the script will be available at ., and so on.

You can then use the “SNMP – Generic OID Template” in Cacti (one Data Source per OID).

Let’s say you want to count the number of entries in a log file.
Add this to snmpd.conf :

logmatch cactistats /home/cactiuser/cacti/log/cacti.log 120 SYSTEM STATS
  • the global count of matches will be available under the . OID
  • the “Regex match counter” (which is reset with each file rotation) will be available under the . OID

To list all the available variables, use this query :

$ snmpwalk -v 1 -c public localhost logMatch
UCD-SNMP-MIB::logMatchMaxEntries.0 = INTEGER: 50
UCD-SNMP-MIB::logMatchIndex.1 = INTEGER: 1
UCD-SNMP-MIB::logMatchName.1 = STRING: cactistats
UCD-SNMP-MIB::logMatchFilename.1 = STRING: /home/cactiuser/cacti/log/cacti.log
UCD-SNMP-MIB::logMatchGlobalCounter.1 = Counter32: 301634
UCD-SNMP-MIB::logMatchGlobalCount.1 = INTEGER: 301634
UCD-SNMP-MIB::logMatchCurrentCounter.1 = Counter32: 6692
UCD-SNMP-MIB::logMatchCurrentCount.1 = INTEGER: 6692
UCD-SNMP-MIB::logMatchCounter.1 = Counter32: 1
UCD-SNMP-MIB::logMatchCount.1 = INTEGER: 0
UCD-SNMP-MIB::logMatchCycle.1 = INTEGER: 120
UCD-SNMP-MIB::logMatchErrorFlag.1 = INTEGER: 0
UCD-SNMP-MIB::logMatchRegExCompilation.1 = STRING: Success

We’ll then use another interesting directive, the “proxy” one.
Let’s take for example the Squid proxy : when enabled, its SNMP agent listen to UDP 3401 port.
If you want to have system graphs and Squid graphs without declaring 2 devices in Cacti, add this in snmpd.conf :

proxy -v 1 -c public localhost:3401 .

The Squid SNMP tree will be available under the . branch.

Let’s query this host :

$ snmpwalk -v 1 -c public sysdescr
SNMPv2-MIB::sysDescr.0 = STRING: Linux srv1.foo.com #1 Fri Oct 1 12:53:41 CEST 2004 i686

And here’s the Squid part (this specific OID returns the Squid version) :

$ snmpwalk -v 1 -c public .
SNMPv2-SMI::enterprises.3495. = STRING: "2.5.STABLE6"

You’ll find how to enable the Squid SNMP agent here.

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your Name (required)

Your Email (required)


Your Message

Your Name (required)

Your Email (required)


Your Message