Share, , Google Plus, Pinterest,

Print

Posted in:

Extend SNMP – Run bash scripts via SNMP

You probably use some kind of monitoring tools to keep an eye out on your machines and you also probably implemented this monitoring via SNMP to avoid installing additional agents on your machines – so did i. And now you want to implement additional customized monitoring checks and you found out you need to extend SNMP on your client machine.

Well you’ve come to the right place. Follow the guide below to extend SNMP and allow your monitoring software to run bash scripts on your machines and read output these scripts produce. It turns out to be very easy to do so, you just need to extend snmp.

Extend SNMP
Extend SNMP

Here’s how to Extend SNMP

1. Bash Scripts

Preparing to extend snmp – prepare the scripts you would like to run, move it to desired folder and make sure they are executable by “root” user. I think it is hygienical to create a new folder called “scripts” in “/etc/snmp” and put these scripts there. If you disagree, you can put it anywhere else on the system too, as long as “root” user will be able to access them.

2. Edit snmpd.conf

To extend snmp we need to edit “/etc/snmp/snmpd.conf” configuration file and add the extend configuration parameters that point to bash scripts. For this example purpose i will extend SNMP by two bash scripts, you can add more or less of course.

# Extend SNMP
extend script1 /etc/snmp/scripts/script1.sh
extend script2 /etc/snmp/scripts/script2.sh

3. Restart SNMPD Service

To apply configuration changes we need to restart “snmpd” service by running the following command:

[mitch@node01 ~]$ sudo service snmpd restart
 Stopping snmpd: [ OK ]
 Starting snmpd: [ OK ]

4. Test it out

After we’ve restarted SNMPD service we can proceed to run tests by manually triggering the scripts via “snmpget” command, where:

  • “v2c” tells it to use Simple Network Management Protocol Version 2
  • “geekpeek” is the configured community name (default is “public”) you can see in snmpd.conf file
  • “localhost” is the host we are connecting to
  • “script1” is the human readable name of the extended SNMP
  • “STRING: X” tells us the output of the bash script which is X
[mitch@node01 ~]$ snmpget -v2c -c geekpeek localhost 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."script1".1'
NET-SNMP-EXTEND-MIB::nsExtendOutLine."script1".1 = STRING: 1
[mitch@node01 ~]$ snmpget -v2c -c geekpeek localhost 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."script2".1'
NET-SNMP-EXTEND-MIB::nsExtendOutLine."script2".1 = STRING: 2

As we can see the “script1” output is “1” and the “script2” output is “2”, which in my case is ok and i confirmed normal operation of SNMP.

5. Get the OID

There’s one more thing missing, to make this useful. To wrap things up we need to get the OID of these extended scripts and we can use this OID to make monitoring checks. To get the OIDs we need to run the “snmptranslate” command as you see below:

[mitch@node01 ~]$ snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."script1".1'
.1.3.6.1.4.1.8072.1.3.2.3.1.1.11.109.111.110.95.115.101.110.100.101.114.115
[mitch@node01 ~]$ snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."script2".1'
.1.3.6.1.4.1.8072.1.3.2.3.1.1.14.100.99.115.95.112.111.115.110.95.100.101.108.97.121

Voila and we got the OIDs we can call via SNMP. What we get is the output of the scripts configured in snmpd.conf file. Told you it’s really easy to extend snmp.

Easy peasy! ..till next time, geek ON!

  • I think there may be a typo in your Step 5 where you translate. Part of the command says “nsExtendOutput1Line”, but I think it should say “nsExtendOutLine” just like in step 4. I went through all of your steps and it doesn’t work properly if you do “nsExtendOutput1Line”

    Am I mistaken on that?

    Thank you very much for writing this, it was incredibly helpful. You guys rock.

  • Jaime Hidalgo

    Very interesting. Thanks.