SNMP execute remote script

In NET-SNMP it is easy to create options in the snmpd.conf file which will execute scripts.

This way it is easy to execute scripts from a remote server without the need to log in, or have another daemon running.

On the host, where the script should be executed, runs the snmp daemon that comes with the NET-SNMP package. In de file snmpd.conf we add the following line:

exec filedate /usr/local/bin/check_file-date.sh
exec filesize /usr/local/bin/check_file-size.sh

On this host we also create these scripts:

check_file-date.sh:
#!/bin/bash
ls -l /etc/hosts | awk '{print $6,$7,$8}'
exit 0

and

/usr/local/bin/check_file-size.sh
 #!/bin/bash
ls -l /etc/hosts | awk '{print $5}'
exit 0

The oid which can be used to execute the scripts is: .1.3.6.1.4.1.2021.8

The next command can be executed on any remote host (which has the rights):

snmpwalk -v1 -c public 192.168.1.1 .1.3.6.1.4.1.2021.8

The output would be something like this:

UCD-SNMP-MIB::extIndex.1 = INTEGER: 1
UCD-SNMP-MIB::extIndex.2 = INTEGER: 2
UCD-SNMP-MIB::extNames.1 = STRING: filedate
UCD-SNMP-MIB::extNames.2 = STRING: filesize
UCD-SNMP-MIB::extCommand.1 = STRING: /usr/local/bin/check_file-date.sh 
UCD-SNMP-MIB::extCommand.2 = STRING: /usr/local/bin/check_file-size.sh 
UCD-SNMP-MIB::extResult.1 = INTEGER: 0
UCD-SNMP-MIB::extResult.2 = INTEGER: 0
UCD-SNMP-MIB::extOutput.1 = STRING: nov 12 18:52
UCD-SNMP-MIB::extOutput.2 = STRING: 27851
UCD-SNMP-MIB::extErrFix.1 = INTEGER: 0
UCD-SNMP-MIB::extErrFix.2 = INTEGER: 0
UCD-SNMP-MIB::extErrFixCmd.1 = STRING: 
UCD-SNMP-MIB::extErrFixCmd.2 = STRING:

Now you see there is an extra oid available which will just give the outcome of the script.

snmpwalk -v1 -c public 192.168.1.1 UCD-SNMP-MIB::extOutput.1
UCD-SNMP-MIB::extOutput.2 = STRING: 27851

In Nagios, you could use the check_snmp plugin:

check_snmp -H 192.168.1.1 -o UCD-SNMP-MIB::extOutput.2 -s 27851
SNMP OK - 27851 | UCD-SNMP-MIB::extOutput.2=27851