Creating Your Own SNMP Script


The SNMP software package allows you to create your own resource monitoring script, which is run (usually not more than every 30 seconds) to check the health of system resources and report errors into an SNMP MIB that can be monitored by the Mon program running on a central monitoring server. This not only allows you to customize what you are monitoring by writing a simple script, but with a slightly more complex program or script (one that stores values in a temporary file), you can also monitor the system for sudden changes (like a sudden increase in swap space usage). By default, SNMP runs your script and places the exit status of the program after each execution into MIB 1.3.6.1.4.1.2021.8.1.100 and the first line of output (on stdout) from the script into MIB 1.3.6.1.4.1.2021.8.1.101. (Any additional output returned by the script is ignored.)

The syntax in the /etc/snmp/snmpd.conf file to specify the name of the script, or program, to run looks like this:

    sh NAME PROG [ARGS] 

where:

  • NAME is an arbitrary name to describe your monitoring script.

  • PROG is the full path and file name of your script, or program.

  • ARGS an optional argument or list of arguments to pass to the script.

A sample entry in the /etc/snmp/snmpd.conf configuration file looks like this:

 sh check-dns /usr/local/check-dns 

Here, the program /usr/local/check-dns will run (without arguments) when the MIB is queried, or every 30 seconds if the MIB is queried more often than that.

A Sample Custom SNMP Script

Now, let's create the /usr/local/check-dns script with the following simple bash script:

 #!/bin/bash host=`uname -n` hostnotfound=`/usr/bin/host www.google.com | grep "not found"` if [ "$hostnotfound" ];then echo "DNS lookup not working properly on $host" exit 1 fi 

If the DNS query of host www.google.com fails, the script returns one line of error and exits with a return value of 1. Save this script to the file /usr/local/ check-dns (be sure to set the execute bits with the command chmod 755 /usr/ local/check-dns) and run it first with a known good host like www.google.com; then modify the script and try a bogus host name (one that does not resolve). Then add the line shown above to your /etc/snmp/snmpd.conf file and start, or restart, the snmpd daemon.

When using a bogus host entry in the script file, you should see the following output when you examine the MIB entry:

 #snmpwalk -v 1 localhost -c public 1.3.6.1.4.1.2021.8.1.101 UCD-SNMP-MIB::extOutput.1 = STRING: DNS services not working properly on primary.yourdomain.com 

And when you change the script back to a valid host name for the DNS query, you should see nothing in the MIB when you run the same snmpwalk command.



The Linux Enterprise Cluster. Build a Highly Available Cluster with Commodity Hardware and Free Software
Linux Enterprise Cluster: Build a Highly Available Cluster with Commodity Hardware and Free Software
ISBN: 1593270364
EAN: 2147483647
Year: 2003
Pages: 219
Authors: Karl Kopper

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net