in reply to Re^3: best way to monitor real memory usage on linux
in thread best way to monitor real memory usage on linux

Hello, unfortunately that is not the case, snmp has several oid for real memory, swap, etc...
UCD-SNMP-MIB::memIndex.0 = INTEGER: 0 UCD-SNMP-MIB::memErrorName.0 = STRING: swap UCD-SNMP-MIB::memTotalSwap.0 = INTEGER: 8385920 kB UCD-SNMP-MIB::memAvailSwap.0 = INTEGER: 8385920 kB UCD-SNMP-MIB::memTotalReal.0 = INTEGER: 8240104 kB UCD-SNMP-MIB::memAvailReal.0 = INTEGER: 2576224 kB UCD-SNMP-MIB::memTotalFree.0 = INTEGER: 10962144 kB UCD-SNMP-MIB::memMinimumSwap.0 = INTEGER: 16000 kB UCD-SNMP-MIB::memBuffer.0 = INTEGER: 326776 kB UCD-SNMP-MIB::memCached.0 = INTEGER: 4740904 kB
I have written the following nagios plugins that would take into account buffered and cached when calculating free memory...
#!C:\Dwimperl\perl\bin use strict; use warnings; use Net::SNMP; use Nagios::Plugin; use nagios::Plugin::Getopt; use Math::Round; my $np; $np=Nagios::Plugin->new( usage => "Usage: %s -H <host> -C <community> -w <warn> -c +<crit>", shortname => "SNMP Memory", timeout => 5, blurb => "this plugin calculates the real used memory in l +inux which includes the cache and buffers", extra => " Examples: check_snmp_mem.pl -H 127.0.0.1 -C public -w 90 -c 95 " ); # plugin arguments $np->add_arg( spec => "host|H=s", help => "ip address or hostname of device", required => 1, ); $np->add_arg( spec => "community|C=s", help => "snmp community string", required => 1, ); $np->add_arg( spec => "warning|w=i", help => "usage in percent, when a WARNING is returned", required => 1, ); $np->add_arg( spec => "critical|c=i", help => " mem usage in percent, when a CRITICAL is returned +", required => 1, ); $np->getopts; # perform sanity checking on command line options if ( (defined $np->opts->warning) && ($np->opts->warning < 0 || $np->o +pts->warning > 100) ) { $np->nagios_die( " invalid number supplied for the -w option: ente +r a value between 0 and 100 " ); } if ( (defined $np->opts->warning) && ($np->opts->critical < 0 || $np-> +opts->critical > 100) ) { $np->nagios_die( " invalid number supplied for the -c option: ente +r a value between 0 and 100 " ); } if ( (defined $np->opts->warning) && ( $np->opts->warning > $np->opts- +>critical) ) { $np->nagios_die( " invalid number supplied for the -w option: warn +ing threshold should be smaller than critical threshold " ); } my $memRealTotalOID = '.1.3.6.1.4.1.2021.4.5.0'; my $memRealFreeOID = '.1.3.6.1.4.1.2021.4.6.0'; my $memRealCachedOID = '.1.3.6.1.4.1.2021.4.15.0'; my $memRealBuffersOID = '.1.3.6.1.4.1.2021.4.14.0'; my ($session, $error) = Net::SNMP->session( -hostname => $np->opts->host, -community => $np->opts->community, ); if (!defined $session) { $np->nagios_exit (WARNING, $error) #printf "ERROR: %s.\n", $error; } my $memRealFree = $session->get_request(-varbindlist => [ $memRealF +reeOID],); my $memRealTotal = $session->get_request(-varbindlist => [ $memReal +TotalOID],); my $memRealCached = $session->get_request(-varbindlist => [ $memRea +lCachedOID],); my $memRealBuffers = $session->get_request(-varbindlist => [ $memRe +alBuffersOID],); $session->close(); my $buffers = $memRealBuffers->{$memRealBuffersOID}; my $cache = $memRealCached->{$memRealCachedOID}; my $total= $memRealTotal->{$memRealTotalOID}; my $free = $memRealFree->{$memRealFreeOID}; my $memRealUsed = $total - $free; my $memRealUsedMB = round ($memRealUsed / 1024); my $totalMB = round($total / 1024); my $realTPercent = (($memRealUsed - $buffers - $cache)/ $total) * 10 +0; my $realPercent = sprintf "%.2f", $realTPercent; $np->add_perfdata( label => "Free", value => $free, uom => "KB" ); $np->add_perfdata( label => "Total", value => $total, uom => "KB" ); $np->add_perfdata( label => "Cached", value => $cache, uom => "KB" ); $np->add_perfdata( label => "Buffered", value => $buffers, uom => "KB" ); ##################################################################### +######### # check the result against the defined warning and critical thresholds +, # output the result and exit $np->nagios_exit( return_code => $np->check_threshold($realPercent), message => " used: $realPercent% with $memRealUsedMB MB of $total +MB MB free" );
I just want to know if there not a way to calculate the available memory without dubbling the variables? line 82 onwards from my script.

Replies are listed 'Best First'.
Re^5: best way to monitor real memory usage on linux
by anonymized user 468275 (Curate) on Aug 06, 2015 at 19:36 UTC
    Surely I don't understand the question - it looks like you are asking how to eliminate e.g. the $cache variable, but that seems too simple given your apparently higher skill level.!

    One world, one people

      Hello

      That is my question for now...I have used mainly the examples of the Nagios::Plugin and Net::SNMP to build the script..

      The object returned by Net::SNMP get_request is a hash how can I make arithmetic operation on the values returned by those. the only workaround I found was to extract the value in a temporary variable..

      I am in my first baby steps in Perl.
        You are receiving a hash reference as a result. So if the call is $result = $session->get_request(<arguments>), then instead of a temporary variable you can use the expression $result->{key} instead. The last argument in the call should be a reference to array of OIDs, rather than calling get_request (blocking) for every individual OID! An example of arithmetic using referenced hash values:-
        my $AminusBminusC = $result->{A} - $result->{B} - $result->{C};
        The A B and C are actual keys. Actually any expression can go inside the braces (e.g. variable, function call, array element, yet another hash value) and the value of the expression will be used as the key to look up its corresponding hash value. If instead you were directly accessing a hash called %result instead of a hash reference $result, you'd remove the -> (dereference) operator from each expression that gets the value for a key. Hope this helps!

        One world, one people