#!/usr/bin/perl -wT
# $Id: check_mem.pl,v 1.1.1.1 2002/02/28 06:42:54 egalstad Exp $

# check_mem.pl Copyright (C) 2000 Dan Larsson <dl@tyfon.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA

# Tell Perl what we need to use
require 5.004;
use strict;
use lib utils.pm;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :thresholds);

use vars qw($opt_c $opt_f $opt_u $opt_w $opt_t $PROGNAME);

use utils qw(%ERRORS &usage);

$w_opt->description("Percent free/used when to warn");
$c_opt->description("Percent free/used when critical");
my $f_opt = new Plugin::Parameter(-name => "freecheck", -flags => [ 'f', 'free' ],
				  -optional => "yes", -valueoptional => "yes", -type => "NONE",
				  -description => "Check FREE memory");
my $u_opt = new Plugin::Parameter(-name => "usedcheck", -flags => [ 'u', 'used' ],
				  -optional => "yes", -valueoptional => "yes", -type => "NONE",
				  -description => "Check USED memory");
my $plugin = new Plugin(-revision => '$Revision: 1.1.1.1 $',
			-copyright => "2000 Dan Larsson <dl\@tyfon.net>, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Check the usage of memory on the local machine",
			-checker => sub { my ($plugin) = @_;
					  if (!$opt_f and !$opt_u) {
					    $plugin->usage();
					    usage("$PROGNAME UNKNOWN: Select either Free or Used memory to monitor\n");
					  }
					  if ($opt_f and $opt_w <= $opt_c) {
					    $plugin->usage();
					    usage("$PROGNAME UNKNOWN: Warning level must be greater than Critical for Free memory check\n");
					  }
					  if ($opt_u and $opt_w >= $opt_c) {
					    $plugin->usage();
					    usage("$PROGNAME UNKNOWN: Warning level must be less than Critical for Used memory check\n");
					  }
					},
			-parameterlists => [ [ $f_opt, $u_opt, $w_opt, $c_opt, $t_opt ], $h_opts, $V_opts ]);

$plugin->init();

$plugin->start_timeout($opt_t, "plugin timedout waiting for vmstat");

# This the unix command string that brings Perl the data

$ENV{PATH} = "/usr/bin:/bin";
$ENV{BASH_ENV}="";

my $command_line = `free | grep -i 'Mem:'`;

chomp $command_line;

# Define the calculating scalars
$command_line =~ m/^mem:\s+\d+\s+(\d+)\s+(\d+)/i;
my $used_memory  = $1;
my $free_memory  = $2;
my $total_memory = $used_memory + $free_memory;

if ($opt_f) {
  my $percent    = $free_memory / $total_memory * 100;
  my $fmt_pct    = sprintf "%.1f", $percent;
  if ($percent <= $opt_c)
  {
    print "$PROGNAME CRITICAL: $fmt_pct% ($free_memory kB) free\n";
    exit $ERRORS{'CRITICAL'};
  }  elsif ($percent <= $opt_w) {
    print "$PROGNAME WARNING: $fmt_pct% ($free_memory kB) free\n";
    exit $ERRORS{'WARNING'};
  } else {
    print "$PROGNAME OK: - $fmt_pct% ($free_memory kB) free\n";
    exit $ERRORS{'OK'};
  }
} elsif ($opt_u) {
  my $percent    = $used_memory / $total_memory * 100;
  my $fmt_pct    = sprintf "%.1f", $percent;
  if ($percent >= $opt_c)
  {
    print "$PROGNAME CRITICAL - $fmt_pct% ($used_memory kB) used\n";
    exit $ERRORS{'CRITICAL'};
  } elsif ($percent >= $opt_w) {
    print "$PROGNAME WARNING: $fmt_pct% ($used_memory kB) used\n";
    exit $ERRORS{'WARNING'};
  } else {
    print "PROGNAME OK: $fmt_pct% ($used_memory kB) used\n";
    exit $ERRORS{'OK'};
  }
}
