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.

  • Lewis

    i use a utility called hdsentinel to monitor my hd health..
    this returns multiple values. how can i put them in snmp tree ?

    root@Castor:/etc/snmp/scripts# ./hdsentinel.sh
    /dev/sda 29 100 890 WDC_WD40EFRX-68N32N0 WD-WCC7K7LPJACJ 3815448
    /dev/sdb 27 100 559 WDC_WD40EFRX-68N32N0 WD-WCC7K6DJDVSC 3815448
    /dev/sdc 26 100 537 WDC_WD40EFRX-68N32N0 WD-WCC7K4NV26SU 3815448
    /dev/sdd 27 100 22685 WDC_WD20EFRX-68EUZN0 WD-WCC4M0782134 1907729
    /dev/sde 28 100 24063 WDC_WD20EFRX-68EUZN0 WD-WCC4M0785522 1907729
    /dev/sdf ? ? 24063 Kingston_DataTraveler_3.0 PMAP1234PhIsOn 29567

    first column is the hard disk, second the temperature, third the health, etc…

    • Lewis

      hdsentinel utility can be found here..

      https://www.hdsentinel.com/hard_disk_sentinel_linux.php

      -solid parameter – solid output (drive, tempC, health%, power on hours, model, S/N, size)

    • Mitch

      Hi Lewis, you will have to divide your output to different scripts – example1: sh hdcentinel.sh |grep /dev/sdb |awk ‘{print$8}’ – to print second row, last column value 3815448. Once you’ve granulated results in different scripts you can add them to SNMP as per tutorial.
      Regards,
      Mitch

  • Cosme Corrêa

    How to use snmpset to pass data to a shell script.
    I am looking for examples without success.

  • Interesting. Thanks.

  • larr

    EXCELLENT write up on this… especially the ‘snmptranslate’ part… that’s very helpful.