All Linux HowTo's

Configure SNMP on CentOS for use with Nagios (monitor disk space)

This article explains how to configure SNMP for use with Nagios on a CentOS server. It’s assumed you already have Nagios installed but not necessarily configure to use SNMP to monitor a host. We use “Disk Space” monitoring in this article.

For this article, i’ve used the following versions:

1. Nagios Version 3.5.0
2. CentOS 6.2

Configure SNMP

Install SNMP:

yum install net-snmp*

Change the “community name” from “public” to something more secure. Edit the file “/etc/snmp/snmpd.conf” using this example as a guide:

com2sec mynetwork 127.0.0.1 public
com2sec mynetwork 192.168.0.0/24 public
com2sec mynetwork nagios.agix.com.au public
group MyROGroup v1 mynetwork
group MyROGroup v2c mynetwork
view all included .1
access MyROGroup "" any noauth exact all none none
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root  (configure /etc/snmp/snmp.local.conf)
disk / 1000000
pass .1.3.6.1.4.1.4413.4.1 /usr/bin/ucd5820stat
rwuser agixmon

Restart SNMP:

service snmpd restart

Test if from the localhost (the server running SNMP):

snmpwalk -v2c -c myComNameSecret localhost:161

You should get plenty of information out of it. Make sure to allow UDP port “161” through your firewall/router to this SNMP server.

Configure Nagios

Add the following to your “/usr/local/nagios/etc/objects/services.cfg” file:

define service{
        use                             generic-service
        hostgroup_name                  group_disk_space
        service_description             checkDiskSpace
        is_volatile                     0
        check_period                    24x7
        max_check_attempts              3
        normal_check_interval           60
        retry_check_interval            1
        notification_interval           240
        notification_period             24x7
        notification_options            c,r
        check_command                   check_hd
}

And now list the computers that need to be monitored (“MYSERVER1” and “MYSERVER2” in this example). This is in the same file as above:

define hostgroup{
        hostgroup_name                  group_disk_space
        alias                           group_disk_space
        members                         MYSERVER1, MYSERVER2
        #hostgroup_members               hostgroups
        notes                           group_disk_space
        notes_url                       url
        action_url                      url
}

Now edit your “/usr/local/nagios/etc/objects/commands.cfg” and add the following. Note that you should change the “public” community name to be the same as used earlier – assuming you changed it earlier:

define command{
        command_name check_hd
        command_line $USER1$/check_hd $HOSTADDRESS$ public 90 95 /home
}

You will need to add the following script contents into the file “/usr/local/nagios/libexec/check_hd”:

#!/usr/bin/perl
# Dont use the embedded apache perl....
# Author : Peter
# Date : Apr 11 2006
# check_hd IP COMMUNITY warnlevel criticallevel disc

use POSIX;

sub print_usage 
{
print "
############################## check_hd ###############################
# Version : 1.0 
# Date : Apr 11 2006  
# Author  : Peter Stimpel 
# Modified  : By AGIX July 03 2013
# Thanks to Benjamin Jakubowski for the idea to walk through snmp
# Help : http://www.peters-webcorner.de/nagios/
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt 
#######################################################################n";
	print "check_hd IP COMMUNITY warnlevel criticallevel discn";
	print"ncheck_hd -v for version infon";
	
}
$PROGNAME = "check_hd";
if (@ARGV[0] eq "-v") {
	print_usage();
	exit 0;

}
if  ( @ARGV[0] eq "" || @ARGV[1] eq "" || @ARGV[2] eq "" || @ARGV[3] eq "" || @ARGV[4] eq "")
{
    print_usage();
    exit 0;
}
$IP=@ARGV[0];
$COMMUNITY=@ARGV[1];
$Service=@ARGV[2];
$LW=@ARGV[4];
$resultat =`/usr/bin/snmpwalk -v 2c -c $COMMUNITY $IP  hrStorageDescr | grep -m 1 $LW`;
$fullsize1=0;
$usedsize1=0;
$freespace=0;
if ( $resultat ) {
		$resstring= $resultat;
		if ($resultat = ~/hrStorageDescr./) {
			$tsid = substr($resstring,35,2);
			$resultat2 =`/usr/bin/snmpwalk -v 2c -c $COMMUNITY $IP hrStorageAllocationUnits.$tsid`;
			$resstring2 = $resultat2;
			if ($resultat2 = ~/hrStorageAllocationUnits.$tsid/) {
				#@unit = substr($resstring2,58,5);
				@unit=split(/:/,$resstring2);
				@unit1=split(/ /,$unit[3]);
				$unit1=$unit1[1];
				$resultat3 =`/usr/bin/snmpwalk -v 2c -c $COMMUNITY $IP hrStorageSize.$tsid`;
				$resstring3 = $resultat3;
				if ($resultat3 = ~/hrStorageSize.$tsid/) {
					@ta=split(/INTEGER/,$resstring3);				
					chomp($ta[1]);
					$size1=substr($ta[1],1);
					$fullsize1 = $fullsize1 + $size1;
					$fullsize1 = $fullsize1 * $unit1;
				}
				$resultat4 =`/usr/bin/snmpwalk -v 2c -c $COMMUNITY $IP hrStorageUsed.$tsid`;
				$resstring4 = $resultat4;
				if ($resultat4 = ~/hrStorageUsed.$tsid/) {
					@tb=split(/INTEGER/,$resstring4);				
					chomp($tb[1]);
					$size1=substr($tb[1],1);
					$usedsize1 = $usedsize1 + $size1;
					$usedsize1 = $usedsize1 * $unit1;
				}
				if ($usedsize1 > 0 && $fullsize1 > 0) {
					$freespace=$fullsize1 - $usedsize1;
					$freespace=$freespace / 1024 / 1024 / 1024;
					$percfilled=$usedsize1 * 100 / $fullsize1;
					if ($percfilled > @ARGV[3]) {
						print "critical: $LW in use " . ceil($percfilled) .
							"% and " . ceil($freespace) . "GB free w-$ARGV[2]% c-$ARGV[3]%n";
					        exit 2;
					}
					if ($percfilled > @ARGV[2]) {
						print "warning: $LW in use " . ceil($percfilled) .
							"% and " . ceil($freespace) . "GB free w-$ARGV[2]% c-$ARGV[3]%n";
					        exit 1;
					}
					print "OK: $LW in use " . ceil($percfilled) .
						"% and " . ceil($freespace) . "GB free w-$ARGV[2]% c-$ARGV[3]%n";
					exit 0;
				}
			}
		}
	print "Critical  : Response unknownn";
        exit 2;
}
else
{
	print "Critical  : no responsen";
        exit 2;
}

Set the permissions and ownership of the above script:

chmod 755 /usr/local/nagios/libexec/check_hd
chown nagios.root /usr/local/nagios/libexec/check_hd

Restart Nagios:

service nagios restart

Similar Posts: