#! /usr/bin/perl -w
################################################################################
# 
# 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.
# 
# 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; if not, see <http://www.gnu.org/licenses>.
# 
# Linking this program statically or dynamically with other modules is making a 
# combined work based on this program. Thus, the terms and conditions of the GNU 
# General Public License cover the whole combination.
# 
# As a special exception, the copyright holders of this program give MERETHIS 
# permission to link this program with independent modules to produce an executable, 
# regardless of the license terms of these independent modules, and to copy and 
# distribute the resulting executable under terms of MERETHIS choice, provided that 
# MERETHIS also meet, for each linked independent module, the terms  and conditions 
# of the license of that module. An independent module is a module which is not 
# derived from this program. If you modify this program, you may extend this 
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
# 
#####################################################################################

use strict;
use Getopt::Long;

my $PROGNAME = "$0";
my $VERSION = "1.0";

sub print_usage ();
my %OPTIONS = ("help" => undef, "version" => undef, "warning" => undef, "critical" => undef);
my %ERRORS = ("OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3);

Getopt::Long::Configure('bundling');
GetOptions
    ("h|help"   => \$OPTIONS{'help'},
    "V|version"   => \$OPTIONS{'version'},
    "w|warning=s" => \$OPTIONS{'warning'},
    "c|critical=s" => \$OPTIONS{'critical'});

if ($OPTIONS{'version'}) {
    print "$PROGNAME : version $VERSION\n";
exit $ERRORS{'UNKNOWN'};
}
if ($OPTIONS{'help'}) {
    print_usage();
exit $ERRORS{'UNKNOWN'};
}
if (!defined($OPTIONS{'warning'}) || !defined($OPTIONS{'critical'})) {
    print_usage;
    exit $ERRORS{'UNKNOWN'};
}
#######################################
## swapinfo output example :
#[nagios@hp-ux]$ /usr/sbin/swapinfo
# Kb      Kb      Kb   PCT  START/      Kb
# TYPE      AVAIL    USED    FREE  USED   LIMIT RESERVE  PRI  NAME
# dev     25165824       0 25165824    0%       0       -    1  /dev/vg00/lvol2
# reserve       - 19199744 -19199744
# memory  33485832 17980224 15505608   54%
#######################################

my $command_line = `/usr/sbin/swapinfo -tM | tail -1 | awk '{print \$3" "\$4}'`;
chomp $command_line;
my ($used, $free) = split(/ /, $command_line);

if ($free !~ /[0-9]+/ || $used !~ /[0-9]+/) {
	print "Cannot get memory size and usage informations, verify vmstat command options, command execute by this plugin : vmstat -s\n";
	exit $ERRORS{'UNKNOWN'};
}

my $total = $free + $used;

###############################
## Converting mem info to Bytes
###############################
my $used_percent = $used * 100 / $total;

printf "Total Used : %.2f%% ",$used_percent;
printf "|used=%.2f%%;%.0f;%.0f;0;100\n", $used_percent, $OPTIONS{'warning'}, $OPTIONS{'critical'};
if ($used_percent >= $OPTIONS{'critical'}) {
    exit $ERRORS{'CRITICAL'};
}
if ($used_percent >= $OPTIONS{'warning'}) {
    exit $ERRORS{'WARNING'};
}
exit $ERRORS{'OK'};

sub print_usage () {
    print "Usage : $PROGNAME -w <warning treshold in %> -c <critical treshold in %>\n";
}
