mariog has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have the following code to monitor the real physical memory usage in a Linux Appliance.
I want to include in the free memory the buffers and cache instead of just the free that is returned by snmp

Is there a better way to do this?
use strict; use warnings; use Net::SNMP; use Nagios::Plugin; use nagios::Plugin::Getopt; 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 => shift || '192.168.1.1', -community => shift || 'private', ); if (!defined $session) { printf "ERROR: %s.\n", $error; exit 1; } 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],); my $buffers = $memRealBuffers->{$memRealBuffersOID}; my $cache = $memRealCached->{$memRealCachedOID}; my $total= $memRealTotal->{$memRealTotalOID}; my $free = $memRealFree->{$memRealFreeOID}; my $memRealUsed = $total - $free; my $realPercent = (($memRealUsed - $buffers - $cache)/ $total) * 100 +;
I am wondering how to calculate the real free percentage without using the temp variables (buffers. cache, free, total). I use those to extract the values returned by Net::SNMP and be able to calculate the percentage.

my final purpose is to create a nagios plugin

Replies are listed 'Best First'.
Re: best way to monitor real memory usage on linux
by anonymized user 468275 (Curate) on Aug 06, 2015 at 13:46 UTC
    You could read in and parse /proc/meminfo for that - it has labels in the first column which are pretty self-explanatory.

    One world, one people

      No I cannot, the perl script will be running from a windows because the appliance is in a foreign network and the only gateway to that network is a windows server. so I check the memory status of the appliance in the windows with perl+snmp and feed the results to nagios using nsclient and nrpe.
        OK but I believe the value for free memory returned by SNMP has already deducted what is being used for buffers and cache. So either you could settle for that (the usual approach) or you'd need read access to the remote filesystem for what I previously suggested.

        One world, one people