Extending the Net-SNMP agent
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 :
Code: |
$ /tmp/foo.sh -arg1 |
Now put this in snmpd.conf :
Code: |
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 :
Code: |
$ snmpwalk -v 1 -c public localhost .1.3.6.1.4.1.2021.8.1 |
extOutput translates to .1.3.6.1.4.1.2021.8.1.101
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 :
Code: |
$ /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 :
Code: |
exec .1.3.6.1.4.1.2021.555 /bin/sh /tmp/bar.sh |
Run this query :
Code: |
$ snmpwalk -v 1 -c public localhost .1.3.6.1.4.1.2021.555 |
First line returned by the script will be available at .1.3.6.1.4.1.2021.555.2, 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 :
Code: |
logmatch cactistats /home/cactiuser/cacti/log/cacti.log 120 SYSTEM STATS |
- the global count of matches will be available under the .1.3.6.1.4.1.2021.16.2.1.5.1 OID
- the “Regex match counter” (which is reset with each file rotation) will be available under the .1.3.6.1.4.1.2021.16.2.1.7.1 OID
To list all the available variables, use this query :
Code: |
$ snmpwalk -v 1 -c public localhost logMatch |
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 :
Code: |
proxy -v 1 -c public localhost:3401 .1.3.6.1.4.1.3495.1 |
The Squid SNMP tree will be available under the .1.3.6.1.4.1.3495.1 branch.
Let’s query this host :
Code: |
$ snmpwalk -v 1 -c public 10.151.33.3 sysdescr |
And here’s the Squid part (this specific OID returns the Squid version) :
Code: |
$ snmpwalk -v 1 -c public 10.151.33.3 .1.3.6.1.4.1.3495.1.2.3.0 |
You’ll find how to enable the Squid SNMP agent here.