check_mem.pl
changeset 1 b5d9151a01f4
parent 0 6f49d13a20aa
child 2 d8be657656e4
equal deleted inserted replaced
0:6f49d13a20aa 1:b5d9151a01f4
    39     DEPENDENT => 4
    39     DEPENDENT => 4
    40 );
    40 );
    41 
    41 
    42 my $ME      = basename $0;
    42 my $ME      = basename $0;
    43 my $NAME    = "MEM";
    43 my $NAME    = "MEM";
    44 my $VERSION = "0.1";
    44 my $VERSION = "0.2";
    45 
    45 
    46 my %opt = (
    46 my %opt = (
    47     warning  => 70,
    47     warning  => 70,
    48     critical => 90
    48     critical => 90
    49 );
    49 );
    79 "$NAME OK: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}";
    79 "$NAME OK: $percent\% used memory - total: $total MB, used: $used MB, free: $free MB|MemUsed=$percent\%;$opt{warning};$opt{critical}";
    80     exit $ERRORS{OK};
    80     exit $ERRORS{OK};
    81 }
    81 }
    82 
    82 
    83 sub get_meminfo() {
    83 sub get_meminfo() {
    84     my ( $total, $free, $used, $percent );
    84     my ( $total, $used, $free, $percent );
    85     my $file = '/proc/meminfo';
    85     my @cmd = ( '/usr/bin/free', '-m' );
    86 
    86 
    87     open( FH, $file )
    87     foreach (`/usr/bin/free -m`) {
    88       or say "$NAME CRITICAL: $file - $!." and exit $ERRORS{CRITICAL};
    88         /^Mem:\s+(?<total>[\d.]+)/
    89 
    89           and $total = $+{total}
    90     while (<FH>) {
       
    91         /^MemTotal:\s+(?<total>[\d.]+)/
       
    92           and $total = ( sprintf( "%.0f", $+{total} / 1024 ) )
       
    93           and next;
    90           and next;
    94         /^MemFree:\s+(?<free>[\d.]+)/
    91         /^.+buffers\/cache:\s+(?<used>[\d.]+)/
    95           and $free = ( sprintf( "%.0f", $+{free} / 1024 ) )
    92           and $used = $+{used}
    96           and last;
    93           and last;
    97     }
    94     }
    98     close(FH);
       
    99 
    95 
   100     $used = ( $total - $free );
    96     $free = ( $total - $used );
   101     $percent = ( sprintf( "%.0f", ( $used / $total ) * 100 ) );
    97     $percent = ( sprintf( "%.0f", ( $used / $total ) * 100 ) );
   102 
    98 
   103     return ( $percent, $total, $used, $free );
    99     return ( $percent, $total, $used, $free );
   104 }
   100 }
   105 
   101 
   162 
   158 
   163 This nagios plugin check the memory usage.
   159 This nagios plugin check the memory usage.
   164 
   160 
   165 =head1 VERSION
   161 =head1 VERSION
   166 
   162 
   167 This man page is current for version 0.1 of B<check_mem>.
   163 This man page is current for version 0.2 of B<check_mem>.
   168 
   164 
   169 =head1 AUTHOR
   165 =head1 AUTHOR
   170 
   166 
   171 Written by Christian Arnold L<arnold@schlittermann.de>
   167 Written by Christian Arnold L<arnold@schlittermann.de>
   172 
   168