"Fossies" - the Fresh Open Source Software Archive

Member "unix/plugins/pandora_mem_used" (15 Sep 2021, 1984 Bytes) of package /linux/misc/pandorafms_agent_unix-7.0NG.757.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 
    3 # Returns memory USED
    4 # (c) 2017 Sancho Lerena <slerena@artica.es>
    5 
    6 use POSIX;
    7 
    8 my $STOTAL=`vmstat -s | grep "total swap" | awk  '{ print $1 } '`;
    9 my $SUSED=`vmstat -s | grep "free swap" | awk  '{ print $1 } '`;
   10 my $SFREE;
   11 eval {
   12 $SFREE=($SUSED/$STOTAL)*100;
   13 };
   14 if ($@) {
   15         $SFREE = 100;
   16 }
   17 
   18 $SFREE = floor($SFREE);
   19 $FREEP = floor($FREEP);
   20 
   21 # Available memory as FreeMemory + Cached + SwapCached.
   22 my $freemem=`cat /proc/meminfo | grep 'MemFree' | awk '{ print \$2 } '`;
   23 my $cached=`cat /proc/meminfo | grep '^Cached:' | awk '{ print \$2 } '`;
   24 my $cachedswap=`cat /proc/meminfo | grep '^SwapCached:' | awk '{ print \$2 }'`;
   25 my $total_meminfo=`cat /proc/meminfo | grep 'MemTotal:' | awk '{ print \$2 }'`;
   26 my $available_new=`cat /proc/meminfo | grep 'MemAvailable:' | awk '{ print \$2 }'`;
   27 my $available;
   28 if ($available_new == 0){
   29         $available=$freemem+$cached+$cachedswap;
   30 }else{
   31         $available=$available_new;
   32 }
   33 my $available_percent = floor(($available / $total_meminfo)*100);
   34 
   35 my $USED = 100 - $available_percent;
   36 my $SWAP_USED = 100 - $SFREE;
   37 
   38 print "<module>\n";
   39 print "<name><![CDATA[Memory_Used]]></name>\n";
   40 print "<type><![CDATA[generic_data]]></type>\n";
   41 print "<description><![CDATA[Used memory %]]></description>\n";
   42 print "<unit><![CDATA[%]]></unit>\n";
   43 print "<min_critical><![CDATA[95]]></min_critical>\n";
   44 print "<max_critical><![CDATA[100]]></max_critical>\n";
   45 print "<data><![CDATA[$USED]]></data>\n";
   46 print "<module_group><![CDATA[System]]></module_group>\n";
   47 print "</module>\n";
   48 
   49 print "<module>\n";
   50 print "<name><![CDATA[Swap_Used]]></name>\n";
   51 print "<type><![CDATA[generic_data]]></type>\n";
   52 print "<description><![CDATA[Used Swap %]]></description>\n";
   53 print "<unit><![CDATA[%]]></unit>\n";
   54 print "<min_critical><![CDATA[95]]></min_critical>\n";
   55 print "<max_critical><![CDATA[100]]></max_critical>\n";
   56 print "<data><![CDATA[$SWAP_USED]]></data>\n";
   57 print "<module_group><![CDATA[System]]></module_group>\n";
   58 print "</module>\n";