Unexpected SNMP rResults

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Giperboloid
Posts: 21
Joined: Sat May 14, 2016 5:47 am

Unexpected SNMP rResults

Post by Giperboloid »

Box293 wrote:From your screenshots we can determine that the remote server 192.168.1.2 cannot talk to the monitor server 192.168.1.1 (however the monitoring server can talk to itself AND the remote server).

Do you want the remote server to be able to query the monitoring server?

Normally the traffic direction is:
Monitoring Server > Remote server.

The remote server responds back on that same connection, it doesn't need to establish another connection in the reverse direction.

There's been a few issues brought up in the past forum replies. What is your current problem and what is the goal you are trying to achieve?
Thank you for explanation. The last problem I have is this one:
Image
Last edited by Giperboloid on Tue May 31, 2016 5:56 am, edited 1 time in total.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Return code of 13 is out of bounds

Post by rkennedy »

It doesn't look like your image came through, could you re-upload it and comment back after doing so?
Former Nagios Employee
Giperboloid
Posts: 21
Joined: Sat May 14, 2016 5:47 am

Re: Return code of 13 is out of bounds

Post by Giperboloid »

rkennedy wrote:It doesn't look like your image came through, could you re-upload it and comment back after doing so?
It's ok now.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Unexpected SNMP rResults

Post by scottwilkerson »

I split this off the old thread as it is a different problem.

Can you provide the command that is being used for the problem items, as well as the plugins you are using.
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
Giperboloid
Posts: 21
Joined: Sat May 14, 2016 5:47 am

Re: Unexpected SNMP rResults

Post by Giperboloid »

scottwilkerson wrote:I split this off the old thread as it is a different problem.

Can you provide the command that is being used for the problem items, as well as the plugins you are using.
clients.cfg

Code: Select all

define host{
	use                             linux-server
	host_name                       node_1
	alias                           server
	address                         192.168.1.2
	max_check_attempts              5
	check_period                    24x7
	notification_interval           30
	notification_period             24x7
}

define service {
	use			generic-service
	host_name		node_1
	service_description	SSH
	check_command		check_ssh
	#notifications_enabled	0
}
define service {
	use			generic-service
	host_name		node_1
	service_description	Current_cpu_usage
	check_command		check_proc_cpu!50
	#notifications_enabled	0
}

define service {
	use			generic-service
	host_name		node_1
	service_description	Processor_usage
	check_command		check_cpu!70!90
}

define service {
	use			generic-service
	host_name		node_1
	service_description	Processor_swap
	check_command		check_swap_cpu!70!90
}

define service {
	use			generic-service
	host_name		node_1
	service_description	RAM
	check_command		check_ram_cpu!70!90
}

define service {
	use			generic-service
	host_name		node_1
	service_description	Processor_load
	check_command		check_load_cpu!70!90
}
commands.cfg

Code: Select all

###############################################################################
# COMMANDS.CFG - SAMPLE COMMAND DEFINITIONS FOR NAGIOS 4.0.4
#
#
# NOTES: This config file provides you with some example command definitions
#        that you can reference in host, service, and contact definitions.
#       
#        You don't need to keep commands in a separate file from your other
#        object definitions.  This has been done just to make things easier to
#        understand.
#
###############################################################################


################################################################################
#
# SAMPLE NOTIFICATION COMMANDS
#
# These are some example notification commands.  They may or may not work on
# your system without modification.  As an example, some systems will require 
# you to use "/usr/bin/mailx" instead of "/usr/bin/mail" in the commands below.
#
################################################################################


# 'notify-host-by-email' command definition
define command{
	command_name	notify-host-by-email
	command_line	/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/sendmail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
	}

# 'notify-service-by-email' command definition
define command{
	command_name	notify-service-by-email
	command_line	/usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/sendmail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
	}





################################################################################
#
# SAMPLE HOST CHECK COMMANDS
#
################################################################################


# This command checks to see if a host is "alive" by pinging it
# The check must result in a 100% packet loss or 5 second (5000ms) round trip 
# average time to produce a critical error.
# Note: Five ICMP echo packets are sent (determined by the '-p 5' argument)

# 'check-host-alive' command definition
define command{
        command_name    check-host-alive
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
        }




################################################################################
#
# SAMPLE SERVICE CHECK COMMANDS
#
# These are some example service check commands.  They may or may not work on
# your system, as they must be modified for your plugins.  See the HTML 
# documentation on the plugins for examples of how to configure command definitions.
#
# NOTE:  The following 'check_local_...' functions are designed to monitor
#        various metrics on the host that Nagios is running on (i.e. this one).
################################################################################

# 'check_local_disk' command definition
define command{
        command_name    check_local_disk
        command_line    $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
        }


# 'check_local_load' command definition
define command{
        command_name    check_local_load
        command_line    $USER1$/check_load -w $ARG1$ -c $ARG2$
        }


# 'check_local_procs' command definition
define command{
        command_name    check_local_procs
        command_line    $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
        }


# 'check_local_users' command definition
define command{
        command_name    check_local_users
        command_line    $USER1$/check_users -w $ARG1$ -c $ARG2$
        }


# 'check_local_swap' command definition
define command{
	command_name	check_local_swap
	command_line	$USER1$/check_swap -w $ARG1$ -c $ARG2$
	}


# 'check_local_mrtgtraf' command definition
define command{
	command_name	check_local_mrtgtraf
	command_line	$USER1$/check_mrtgtraf -F $ARG1$ -a $ARG2$ -w $ARG3$ -c $ARG4$ -e $ARG5$
	}


################################################################################
# NOTE:  The following 'check_...' commands are used to monitor services on
#        both local and remote hosts.
################################################################################

# 'check_ftp' command definition
define command{
        command_name    check_ftp
        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$
        }


# 'check_hpjd' command definition
define command{
        command_name    check_hpjd
        command_line    $USER1$/check_hpjd -H $HOSTADDRESS$ $ARG1$
        }


# 'check_snmp' command definition
define command{
        command_name    check_snmp
        command_line    $USER1$/check_snmp -H $HOSTADDRESS$ $ARG1$
        }


# 'check_http' command definition
define command{
        command_name    check_http
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
        }


# 'check_ssh' command definition
define command{
	command_name	check_ssh
	command_line	$USER1$/check_ssh $ARG1$ $HOSTADDRESS$
	}


# 'check_dhcp' command definition
define command{
	command_name	check_dhcp
	command_line	$USER1$/check_dhcp $ARG1$
	}


# 'check_ping' command definition
define command{
        command_name    check_ping
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
        }


# 'check_pop' command definition
define command{
        command_name    check_pop
        command_line    $USER1$/check_pop -H $HOSTADDRESS$ $ARG1$
        }


# 'check_imap' command definition
define command{
        command_name    check_imap
        command_line    $USER1$/check_imap -H $HOSTADDRESS$ $ARG1$
        }


# 'check_smtp' command definition
define command{
        command_name    check_smtp
        command_line    $USER1$/check_smtp -H $HOSTADDRESS$ $ARG1$
        }


# 'check_tcp' command definition
define command{
	command_name	check_tcp
	command_line	$USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ $ARG2$
	}


# 'check_udp' command definition
define command{
	command_name	check_udp
	command_line	$USER1$/check_udp -H $HOSTADDRESS$ -p $ARG1$ $ARG2$
	}


# 'check_nt' command definition
define command{
	command_name	check_nt
	command_line	$USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
	}

#My commands
#check_proc_cpu
define command{
	command_name	check_proc_cpu
	command_line	$USER1$/check_proc_cpu -H $HOSTADDRESS$ $ARG1$
	}

# 'check_cpu' command definition
# w = Warning level (if CPU % idle falls below this level - must be a percentage)
# c = Critical level
define command{
command_name check_cpu
command_line $USER1$/check_cpu -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $USER3$
}

# 'check_swap' command definition
# w = Warning level (if % or MB swap free drops below this level)
# c = Critical level
define command{
command_name check_swap_cpu
command_line $USER1$/check_swap_cpu -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $USER3$
}

# 'check_ram' command definition
# w = Warning level defined as percentage (with % sign) or in megabytes (without % sign)
# c = Critical level (can be defined as percentage or MB independently of warning)
# o = below|above --> if mem [free drops BELOW] or [used rises ABOVE] thresholds
define command{
command_name check_ram_cpu
command_line $USER1$/check_ram_cpu -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $USER3$ -o $ARG3$
}

# 'check_load' command definition
# w = Warning levels (alert if n,n,n (1,5,15 minute load averages) go above these levels)
# c = Critical levels
define command{
command_name check_load_cpu
command_line $USER1$/check_load_cpu -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $USER3$
} 

# 'check_pbs_E.pl' command definition
define command{
        command_name    check_pbs_E
        command_line    $USER1$/check_by_ssh -t 30 -H $HOSTADDRESS$ -C
"/usr/local/nagios/libexec/check_pbs_E.pl"


################################################################################
#
# SAMPLE PERFORMANCE DATA COMMANDS
#
# These are sample performance data commands that can be used to send performance
# data output to two text files (one for hosts, another for services).  If you
# plan on simply writing performance data out to a file, consider using the 
# host_perfdata_file and service_perfdata_file options in the main config file.
#
################################################################################


# 'process-host-perfdata' command definition
define command{
	command_name	process-host-perfdata
	command_line	/usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n" >> /usr/local/nagios/var/host-perfdata.out
	}


# 'process-service-perfdata' command definition
define command{
	command_name	process-service-perfdata
	command_line	/usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n" >> /usr/local/nagios/var/service-perfdata.out
	}
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Unexpected SNMP rResults

Post by rkennedy »

Code: Select all

# 'check_swap' command definition
# w = Warning level (if % or MB swap free drops below this level)
# c = Critical level
define command{
command_name check_swap_cpu
command_line $USER1$/check_swap_cpu -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $USER3$
}

# 'check_ram' command definition
# w = Warning level defined as percentage (with % sign) or in megabytes (without % sign)
# c = Critical level (can be defined as percentage or MB independently of warning)
# o = below|above --> if mem [free drops BELOW] or [used rises ABOVE] thresholds
define command{
command_name check_ram_cpu
command_line $USER1$/check_ram_cpu -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $USER3$ -o $ARG3$

Code: Select all

define service {
   use         generic-service
   host_name      node_1
   service_description   RAM
   check_command      check_ram_cpu!70!90
}
It does not look like you're specifying an OID for the check_ram_cpu command which I believe is why it's not working. As for the other one, there is an issue with the check_swap_cpu. What is your $USER3$ variable equal to? Does it need an OID specified? Can you post the script check_swap_cpu for us to look at?
Former Nagios Employee
Giperboloid
Posts: 21
Joined: Sat May 14, 2016 5:47 am

Re: Unexpected SNMP rResults

Post by Giperboloid »

$USER3$ = to all users

https://exchange.nagios.org/directory/P ... pu/details
From this link I downloaded and set up the services. But the problem appeared - the files check_load and check_swap were already in the folder/usr/local/nagios/libexec. That's why I was forced to name OID in my own way.

File from this link - check_cpu

Code: Select all

#!/usr/bin/perl -w

# SUMMARY: To get cpu, ram, load, swap info via snmp for Nagios

use lib '/usr/local/nagios/libexec';
use utils qw(%ERRORS);
use strict;

my $VERSION = '1.9.2';
my $UPDATE = '03.17.2011';

my $SNMPDIR = '/usr/bin';
my $D = 0;


####------------- CHANGE LOGS ----------------####
#
# 10/26/2004 by BParish
# Initial build
#
# 01/16/2006 by BParish
# Added a second mechanism on SWAP check, using UCB OIDs if HrStorage fails
#
# 05/05/2009 by BParish
# Modified paths for use with Ubuntu, and variablized where possible
#
# 05/07/2009 by BParish
# Modified SNMP section to be more flexible on values returned
#
# 05/17/2009 by BParish
# Added 'use strict', imported %ERRORS and other requirements now forced in Nagios 3.x
#
# 10/21/2009 by BParish
# Converted RAM to allow percentages or percent/MB mix 
# Now works with Linux
# Uses snmpwalk instead of get
# Can specify if alarm is triggered when RAM used rises above or RAM free drops below threshold 
#   NOTE:  If check returns 'OK', result will be expressed in $WARNING units (MB|%)
# Improved logging
#
# 05/07/2010 by BParish
# Changed RAM to accomodate new ZFS RAM usage.  See this article by Justin Ellison:
#    http://www.sysadminsjourney.com/content/2009/06/05/new-and-improved-checkmempl-nagios-plugin
#    I took his script and hard coded the -C option (to report on caches).  Push this out as
#    /usr/local/nagios/libexec/snmpd_check_mem to all Solaris 10/ZFS boxes.  Then add this line:
#  	extend checkmem /usr/local/nagios/libexec/snmpd_check_mem
#     to the end of the snmpd.conf file on those hosts.
#    For boxes running (SMA) Solaris version of SNMPd (5.0.9 at the time of this writing) add:
#       exec checkmem /usr/local/bin/perl /usr/local/nagios/libexec/snmpd_check_mem
#     to the end of the /etc/sma/snmp/snmpd.conf file
#
# 05/26/2010 by BParish
# Moved all checks to -v 2c.  
#
# 06/16/2010 by BParish
# Added a SSH fallback for Solaris containers that cannot run SNMPd
#
# 03/17/2011 by BParish
# Fixed an error in the processing of SWAP percentages
#
####------------------------------------------####



my $counter=0;
my ($argument, $HOST, $PASS, $WARNING, $CRITICAL, $OPTION);
my $WP = '';
my $CP = '';

foreach $argument (@ARGV) {
   $counter++;
   usage() if ($argument =~ m/^-{1,2}h/);
   if ($argument =~ m/^-{1,2}v/i) { 
     print "$0 version $VERSION last updated $UPDATE\n";
     exit $ERRORS{'OK'};
   }
   if ($argument =~ m/^-{1,2}d/i) {
      $D = $ARGV[$counter];
      print "Debug set to level $D\n";
   }
   if ($argument =~ m/^-{1,2}H/) {
      $HOST = $ARGV[$counter];
   }
   if ($argument =~ m/^-{1,2}p/i) {
      $PASS = $ARGV[$counter];
   }
   if ($argument =~ m/^-{1,2}o/i) {
      $OPTION = $ARGV[$counter];
   }
   if ($argument =~ m/^-{1,2}w/i) {
     $WARNING = $ARGV[$counter];
     if ($WARNING =~ m/\%/) {
       $WP = '%';
       $WARNING =~ s/\%//;
     }
   }
   if ($argument =~ m/^-{1,2}c/i) {
     $CRITICAL = $ARGV[$counter];
     if ($CRITICAL =~ m/\%/) {
       $CP = '%';
       $CRITICAL =~ s/\%//;
     }
   }
}

my $LOGFILE="/tmp/nagios.log";

if ($D > 0) {
   open(LOG, ">$LOGFILE");
}

print LOG "Target set to $HOST\n" if ($D > 0);
print LOG "Set password to $PASS\n" if ($D > 1);
print LOG "Set threshold option to $OPTION\n" if ($OPTION && $D > 1);
print LOG "WARNING IS A PERCENTAGE ($WARNING)%\n" if ($D > 1);
print LOG "CRITICAL IS A PERCENTAGE ($CRITICAL)%\n" if ($D > 1);

print "Command : $0\n" if ($D > 0);

my @junk = split('\/',$0);
my $program = pop(@junk);
print LOG "Calling program is $program\n" if ($D > 3);

if (! defined $HOST || ! defined $PASS) {
  print "Missing Host or Password arguments\n";
  &usage();
     close(LOG) if ($D > 0);
  exit $ERRORS{'UNKNOWN'};
}
$OPTION = 'below' if (! $OPTION || ($OPTION ne 'below' && $OPTION ne 'above'));
$CP = 'MB' if ($CP ne '%');
$WP = 'MB' if ($WP ne '%');

#   ALERT CODES:
my %ALERTLABEL = (
 '0','OK',
 '1','WARNING',
 '2','CRITICAL',
 '3','UNKNOWN',
);

my ($line, $keyword, $target, $value, $resul, $result);
my (%SWAP, %RAM);

my %OID = (
  'SwapIn','UCD-SNMP-MIB::ssSwapIn.0',
  'SwapOut','UCD-SNMP-MIB::ssSwapOut.0',
  'SysInterrupts','UCD-SNMP-MIB::ssSysInterrupts.0',
  'ContextSwitches','UCD-SNMP-MIB::ssSysContext.0',
  'CPUUser','UCD-SNMP-MIB::ssCpuUser.0',
  'CPUSystem','UCD-SNMP-MIB::ssCpuSystem.0',
  'CPUIdle','UCD-SNMP-MIB::ssCpuIdle.0',
  'load1','UCD-SNMP-MIB::laLoad.1',
  'load5','UCD-SNMP-MIB::laLoad.2',
  'load15','UCD-SNMP-MIB::laLoad.3',
  'TotalSwap','UCD-SNMP-MIB::memTotalSwap.0',
  'AvailSwap','UCD-SNMP-MIB::memAvailSwap.0',
  'memTotalReal','UCD-SNMP-MIB::memTotalReal.0',
  'memAvailReal','UCD-SNMP-MIB::memAvailReal.0',
  'memTotalFree','UCD-SNMP-MIB::memTotalFree.0'
);

my ($freeram, $totalram, $usedram, $compare, $snmpworked);
my (%memhash, @memwalk, $mempercentused, $mempercentfree);

if ($program =~ m/ram/) {
    @memwalk = `$SNMPDIR/snmpwalk -v2c -mALL -t 1 -r 4 -c $PASS $HOST UCD-SNMP-MIB::memory`;
    if (scalar(@memwalk) < 2) {
       $snmpworked=0;
		# NO-GO ON SNMP, TRY SSH:
       @memwalk = `/usr/bin/ssh $HOST '/usr/local/nagios/libexec/snmpd_check_mem'`;
       if (scalar(@memwalk) < 2) {
		# NOPE, NO SSH EITHER -- JUST HANG IT UP...
          print "RAM ERROR: Unexpected SNMP results\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'UNKNOWN'};
       }
    } else {
       $snmpworked=1;
    } 
    foreach $result (@memwalk) {
       chomp($result);
       if ($result =~ m/MIB::(\w+)\.0 = INTEGER:\s+(\d+)/) {
          $memhash{$1} = $2;
	  print LOG "\t$1 ==> $2\n" if ($D > 5);
       }
    }
    if ($snmpworked > 0) {
		#  SNMP WORKED, ALSO TRY SOLARIS 10 ZFS EXTENSION:
       @memwalk = `$SNMPDIR/snmpwalk -v2c -t 1 -r 4 -c $PASS $HOST 'NET-SNMP-EXTEND-MIB::nsExtendOutputFull."checkmem"'`;
       if (scalar(@memwalk) < 2) {
          print LOG "RAM extension not installed\n" if ($D > 3);
          my $execmem = `$SNMPDIR/snmpwalk -v2c -t 1 -r 4 -Ov -c $PASS $HOST UCD-SNMP-MIB::extOutput.1`;
          if (! $execmem) {
             print LOG "Alternate (SUN SMA agent) RAM extension not installed, either\n" if ($D > 3);
          } else {
             print LOG "Polling SUN SMA agent RAM extension\n" if ($D > 3);
             print LOG "\t(Which returned: $execmem)\n" if ($D > 5);
             foreach $result (split('\|\|',$execmem)) {
                chomp($result);
	        print LOG "Dissecting line: $result\n" if ($D > 5);
                if ($result =~ m/MIB::(\w+)\.0 = INTEGER:\s+(\d+)/) {
                   $memhash{$1} = $2;
                   print LOG "Update: \t$1 ==> $2\n" if ($D > 5);
                }
             }
          }
       } else {
           # HAS SOLARIS 10/ZFS snmpd extension
          foreach $result (@memwalk) {
             chomp($result);
	     next if ($result =~ m/\|\|/);
             if ($result =~ m/MIB::(\w+)\.0 = INTEGER:\s+(\d+)/) {
                $memhash{$1} = $2;
                print LOG "\t$1 ==> $2\n" if ($D > 5);
             }
          }
       }
    }

	#  SOLARIS DOES NOT SET THESE TWO:
    $memhash{memBuffer} = 0  if (! $memhash{memBuffer});
    $memhash{memCached} = 0  if (! $memhash{memCached});

	#  MEMORY CONSUMED:
    $usedram = $memhash{memTotalReal} - $memhash{memAvailReal} - $memhash{memBuffer} - $memhash{memCached};
    $mempercentused = int(($usedram / $memhash{memTotalReal}) * 100);

	#  MEMORY AVAILABLE:
    $freeram = $memhash{memAvailReal} + $memhash{memBuffer} + $memhash{memCached};
    $mempercentfree = int(($freeram / $memhash{memTotalReal}) * 100);
 
    $totalram = int($memhash{memTotalReal} / 1000);

    if ($freeram eq "" || $usedram eq "") { 
       print "RAM ERROR: Unexpected SNMP results\n";
       close(LOG) if ($D > 0);
       exit $ERRORS{'UNKNOWN'};
    }

	# SNMP results are in KB (divide by 1000 for MB)
    $usedram = int($usedram / 1000);
    $freeram = int($freeram / 1000);

    print LOG "RAM % used: $mempercentused\n    % free: $mempercentfree\n" if ($D > 0);
    print LOG "RAM used: $usedram\n    free: $freeram\n" if ($D > 0);
    print LOG "    warn: ${WARNING}$WP\n    crit: ${CRITICAL}$CP\n" if ($D > 0);

    if ($OPTION eq 'below') {
		#  ALARM IF RAM DROPS BELOW THE THRESHOLD INDICATED:
       print LOG "Checking to see if memory free falls below warning (${WARNING}$WP) or critical (${CRITICAL}$CP) levels\n" if ($D > 2);
       $compare = $freeram;
       if ($CP eq '%') {
          $compare = $mempercentfree;
	  $result = "${compare}% ($freeram of $totalram MB) free";
       } else {
	  $result = "${compare} MB (${mempercentfree}% of $totalram MB) free";
       }
       if ($compare < $CRITICAL) {
          print "RAM CRITICAL - $result|'RAM below $CP'=${compare}$CP;$WARNING;$CRITICAL;\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'CRITICAL'};
       }

       $compare = $freeram;
       if ($WP eq '%') {
          $compare = $mempercentfree;
	  $result = "${compare}% ($freeram of $totalram MB) free";
       } else {
	  $result = "${compare} MB (${mempercentfree}% of $totalram MB) free";
       }
       if ($compare < $WARNING) {
          print "RAM WARNING - $result|'RAM below $WP'=${compare}$WP;$WARNING;$CRITICAL;\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'WARNING'};
       }

       print "RAM OK - $result|'RAM below $WP'=${compare}$WP;$WARNING;$CRITICAL;\n";
          close(LOG) if ($D > 0);
       exit $ERRORS{'OK'};

    } else {
		#  ALARM IF RAM RISES ABOVE THE THRESHOLD INDICATED:
       print LOG "Checking to see if memory used rises above warning (${WARNING}$WP) or critical (${CRITICAL}$CP) levels\n" if ($D > 2);
       $compare = $usedram;
       if ($CP eq '%') {
          $compare = $mempercentused;
	  $result = "${compare}% ($usedram of $totalram MB) used";
       } else {
	  $result = "${compare} MB (${mempercentused}% of $totalram MB) used";
       }
       if ($compare > $CRITICAL) {
          print "RAM CRITICAL - $result|'RAM above $CP'=${compare}$CP;$WARNING;$CRITICAL;\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'CRITICAL'};
       }

       $compare = $usedram;
       if ($WP eq '%') {
          $compare = $mempercentused;
	  $result = "${compare}% ($usedram of $totalram MB) used";
       } else {
	  $result = "${compare} MB (${mempercentused}% of $totalram MB) used";
       }
       if ($compare > $WARNING) {
          print "RAM WARNING - $result|'RAM above $WP'=${compare}$WP;$WARNING;$CRITICAL;\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'WARNING'};
       }

       print "RAM OK - $result|'RAM above $WP'=${compare}$WP;$WARNING;$CRITICAL;\n";
          close(LOG) if ($D > 0);
       exit $ERRORS{'OK'};
    
    }
}

my ($freeswap, $usedswap);
my $totalswap = 0;
  # 'SwapIn','UCD-SNMP-MIB::ssSwapIn.0',
  # 'SwapOut','UCD-SNMP-MIB::ssSwapOut.0',
  # 'TotalSwap','UCD-SNMP-MIB::memTotalSwap.0',
  # 'AvailSwap','UCD-SNMP-MIB::memAvailSwap.0',
  # 'memTotalReal','UCD-SNMP-MIB::memTotalReal.0',
  # 'memAvailReal','UCD-SNMP-MIB::memAvailReal.0',
  # 'memTotalFree','UCD-SNMP-MIB::memTotalFree.0'


if ($program =~ m/swap/) {
    $result = `$SNMPDIR/snmpget -v2c -mALL -t 1 -r 4 -c $PASS $HOST $OID{AvailSwap}`;
	# 	"UCD-SNMP-MIB::memAvailSwap.0 = INTEGER: 2093440 kB" 	
    print LOG "Result of snmpget (avail swap): \n\t$result\n" if ($D > 3);
    if ($result !~ m/INTEGER/) {
		# NO-GO ON SNMP, TRY SSH:
       print LOG "No love on SNMP, trying SSH\n" if ($D > 3);
       $result = `/usr/bin/ssh $HOST '/usr/sbin/swap -s'`;
       print LOG "SSH result: \n\t$result" if ($D > 3);
		#	total: 39976k bytes allocated + 0k reserved = 39976k used, 1008600k available
       if ($result !~ m/total/) {
          print LOG "Rats, SSH failed too, bailing out.\n" if ($D > 3);
		# NOPE, NO SSH EITHER -- JUST HANG IT UP...
          print "SWAP ERROR: Unexpected SNMP results\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'UNKNOWN'};
       }
	#  GOT SSH RESULT, MAKE IT LOOK LIKE SNMP RESULT FOR CONSISTENT PROCESSING LATER:
	$totalswap = "$1" if ($result =~ m/(\d+)k used/); 
        $result = "INTEGER: $1" if ($result =~ m/(\d+)k avail/); 
        $totalswap = $totalswap + $1 if ($result =~ m/INTEGER: (\d+)/); 
        print LOG "SSH end result = total: $totalswap, avail: $result\n" if ($D > 3);
    } else {
	#  WE DID GET A VALID SNMP RESPONSE, NOW ASK FOR TOTAL SWAP:
	#  	UCD-SNMP-MIB::memTotalSwap.0 = INTEGER: 2097144 kB
	$totalswap = `$SNMPDIR/snmpget -v2c -mALL -t 1 -r 4 -c $PASS $HOST $OID{TotalSwap}`;
	$totalswap = "$1" if ($result =~ m/INTEGER:\s*(\d+)/);
        print LOG "Result of snmpget (total swap): \n\t$totalswap\n" if ($D > 3);
    }

    if ($totalswap < 1) {
          print LOG "OOPS: swap=$totalswap\n      free: $freeswap\n      warn: $WARNING\n      critical: $CRITICAL\n" if ($D> 0);
          print "SWAP ERROR: Unexpected SNMP results\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'UNKNOWN'};
    }

    chomp($result);
    $freeswap = $1 if ($result =~ m/INTEGER: (\d+)/); 
	# SWAP comes back in kB (divide by 1000 for MB)
    $freeswap = int($freeswap / 1000);

    chomp($totalswap);
    $totalswap = int($totalswap / 1000);

    print LOG "SWAP free: $freeswap\n     total: $totalswap\n" if ($D> 0);

  if ($WP eq '%') {
    if ($WARNING < 10) {
      $WARNING = ".0$WARNING";
    } else {
      $WARNING = ".$WARNING";
    }
    $WARNING = int($totalswap * $WARNING);
    print LOG "     warning: $WARNING\n" if ($D> 0);
  } 
  if ($CP eq '%') {
    if ($CRITICAL < 10) {
      $CRITICAL = ".0$CRITICAL";
    } else {
      $CRITICAL = ".$CRITICAL";
    }
    $CRITICAL = int($totalswap * $CRITICAL);
    print LOG "     critical: $CRITICAL\n" if ($D> 0);
  }
  	# CHECK RESULTS
  if ($freeswap < $CRITICAL) {
    print "SWAP CRITICAL - $freeswap MB free|SWAP=${freeswap}MB;$WARNING;$CRITICAL;0;\n";
     close(LOG) if ($D > 0);
    exit $ERRORS{'CRITICAL'};
  }
  if ($freeswap < $WARNING) {
    print "SWAP WARNING - $freeswap MB free|SWAP=${freeswap}MB;$WARNING;$CRITICAL;0;\n";
     close(LOG) if ($D > 0);
    exit $ERRORS{'WARNING'};
  }
  print "SWAP OK - $freeswap MB free|SWAP=${freeswap}MB;$WARNING;$CRITICAL;0;\n";
     close(LOG) if ($D > 0);
  exit $ERRORS{'OK'};
}

if ($program =~ m/cpu/) {

  print LOG "Went into check cpu subroutine\n" if ($D > 0);
  if ($WP ne '%' || $CP ne '%') {
    &usage();
     close(LOG) if ($D > 0);
    exit $ERRORS{'UNKNOWN'};
  }
  my ($cpuuser, $cpusys, $cpuidle);
  @memwalk = `$SNMPDIR/snmpwalk -v2c -mALL -t 1 -r 3 -c $PASS $HOST UCD-SNMP-MIB::systemStats`;
  foreach $result (@memwalk) {
       chomp($result);
       $cpuuser = $1 if ($result =~ m/MIB::ssCpuUser.0 = INTEGER:\s+(\d+)/);
       $cpusys = $1 if ($result =~ m/MIB::ssCpuSystem.0 = INTEGER:\s+(\d+)/);
       $cpuidle = $1 if ($result =~ m/MIB::ssCpuIdle.0 = INTEGER:\s+(\d+)/);
  }
  if (! $cpuuser && ! $cpusys && ! $cpuidle) {
		# NO-GO ON SNMP, TRY SSH:
       $result = `/usr/bin/ssh $HOST 'vmstat 1 2 | tail -1'`;
       if ($result =~ m/(\d+)\s+(\d+)\s+(\d+)$/) {
	  $cpuuser = $1;
	  $cpusys = $2;
	  $cpuidle = $3;
       } else {
		# NOPE, NO SSH EITHER -- JUST HANG IT UP...
          print "CPU ERROR: SNMP Connection Problem\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'UNKNOWN'};
       }
  }
  if ($cpuidle < $CRITICAL) {
    print "CPU CRITICAL - user=${cpuuser}%, sys=${cpusys}%, idle=${cpuidle}% |User=${cpuuser}% Sys=${cpusys}% Idle=${cpuidle}%;$WARNING;$CRITICAL\n";
     close(LOG) if ($D > 0);
    exit $ERRORS{'CRITICAL'};
  }
  if ($cpuidle < $WARNING) {
    print "CPU WARNING - user=${cpuuser}%, sys=${cpusys}%, idle=${cpuidle}% |User=${cpuuser}% Sys=${cpusys}% Idle=${cpuidle}%;$WARNING;$CRITICAL\n";
     close(LOG) if ($D > 0);
    exit $ERRORS{'WARNING'};
  }
  print "CPU OK - user=${cpuuser}%, sys=${cpusys}%, idle=${cpuidle}% |User=${cpuuser}% Sys=${cpusys}% Idle=${cpuidle}%;$WARNING;$CRITICAL\n";
     close(LOG) if ($D > 0);
  exit $ERRORS{'OK'};
}

my ($warn1, $warn5, $warn15, $crit1, $crit5, $crit15);

if ($program =~ m/load/) {
  if ($WP eq '%' || $CP eq '%') {
    &usage();
     close(LOG) if ($D > 0);
    exit $ERRORS{'UNKNOWN'};
  }
  print LOG "Warning is $WARNING\nCritical is $CRITICAL\n" if ($D > 0);
  ($warn1,$warn5,$warn15) = split('\,',$WARNING);
  ($crit1,$crit5,$crit15) = split('\,',$CRITICAL);
  print LOG "Warning is now $warn1/$warn5/$warn15\n" if ($D > 0);
  print LOG "Critical is now $crit1/$crit5/$crit15\n" if ($D > 0);

  my ($load1, $load5, $load15);
  @memwalk = `$SNMPDIR/snmpwalk -v2c -mALL -t 1 -r 3 -c $PASS $HOST UCD-SNMP-MIB::laLoad`;
  foreach $result (@memwalk) {
       chomp($result);
       $load1 = $1 if ($result =~ m/MIB::laLoad.1 = STRING:\s+(.*)/);
       $load5 = $1 if ($result =~ m/MIB::laLoad.2 = STRING:\s+(.*)/);
       $load15 = $1 if ($result =~ m/MIB::laLoad.3 = STRING:\s+(.*)/);
  }

  if (! $load1 && ! $load5 && ! $load15) {
		# NO-GO ON SNMP, TRY SSH:
       $result = `/usr/bin/ssh $HOST 'uptime'`;
       if ($result =~ m/average: (.*), (.*), (.*)$/) {
	  $load1 = $1;
	  $load5 = $2;
	  $load15 = $3;
       } else {
		# NOPE, NO SSH EITHER -- JUST HANG IT UP...
          print "LOAD ERROR: SNMP Connection Problem\n";
          close(LOG) if ($D > 0);
          exit $ERRORS{'UNKNOWN'};
       }
  }
  if ($load1 > $crit1 || $load5 > $crit5 || $load15 > $crit15) {
    print "LOAD CRITICAL - $load1, $load5, $load15 |load1=$load1;$warn1;$crit1;0 load5=$load5;$warn5;$crit5;0 load15=$load15;$warn15;$crit15;0\n";
     close(LOG) if ($D > 0);
    exit $ERRORS{'CRITICAL'};
  }
  if ($load1 > $warn1 || $load5 > $warn5 || $load15 > $warn15) {
    print "LOAD WARNING - $load1, $load5, $load15 |load1=$load1;$warn1;$crit1;0 load5=$load5;$warn5;$crit5;0 load15=$load15;$warn15;$crit15;0\n";
     close(LOG) if ($D > 0);
    exit $ERRORS{'WARNING'};
  }
  print "LOAD OK - $load1, $load5, $load15 |load1=$load1;$warn1;$crit1;0 load5=$load5;$warn5;$crit5;0 load15=$load15;$warn15;$crit15;0\n";
  close(LOG) if ($D > 0);
  exit $ERRORS{'OK'};
}

#### USAGE SUBROUTINE ####

sub usage {
  my ($msg1, $msg2);
  my $msg3 = '';

  print LOG "Went into usage subroutine\n" if ($D > 0);

  if ($0 =~ m/cpu/) {
    $msg1 = '(Unsupported - you must define a percentage)';
    $msg2 = '(alert if cpu % idle drops below this)';
  }
  if ($0 =~ m/swap/) {
    $msg1 = '(alert if free swap (MB) drops below this)';
    $msg2 = '(alert if % of swap free drops below this)';
  }
  if ($0 =~ m/load/) {
    $msg1 = "(alert if system load rises above this)\n                 - use n,n,n to denote 1,5,15 minute load averages";
    $msg2 = '(Unsupported - you cannot define a percentage)';
  }
  if ($0 =~ m/ram/) {
    $msg1 = '(alert if memory used goes above/below this threshold (in MBytes))';
    $msg2 = '(alert if memory used goes above/below this percentage)';
    $msg3 = '-o above|below  Alarm if memory [used goes above] or [free drops below] threshold';
  }

print <<"EOF";

$0 version $VERSION last updated $UPDATE

 * OVERVIEW *
This utility was written 10/25/2004 as a method for automating
the collection of SNMP based information, for the ultimate goal
of pushing the result to Nagios.

 * USAGE *

$0 ARGS
                 
ARGS:
-v 		Print version and exit
-h		Print this help message
-d n		Turn debug on at level n (1-3)
-H 		SNMP (remote) host to poll
-w n		Warning level $msg1
   n%		Warning level $msg2
-c n		Critical level 
   n%		Critical level 
-p 		SNMP Community password on remote host
$msg3

All options can be prefixed with a single or double "-".
All options are case sensitive and only eval the first character
	(e.g. --debug is the same as --d, -d, -debug)

EOF
close(LOG) if ($D > 0);
exit $ERRORS{'OK'};
}
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Unexpected SNMP rResults

Post by Box293 »

Giperboloid wrote:$USER3$ = to all users
Is that what $USER3$ is set to in the file /usr/local/nagios/etc/resource.cfg ?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
maksibonus
Posts: 7
Joined: Wed May 18, 2016 3:03 pm

Re: Unexpected SNMP rResults

Post by maksibonus »

Here the file
Attachments
resource.cfg
(1.28 KiB) Downloaded 416 times
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: Unexpected SNMP rResults

Post by tgriep »

You will need to edit the resource.cfg file and change the $USER3$ option to the community string you have setup on the remote system. Save the file and restart nagios and that should fix the issue for you.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked