#! /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.
# 
# For more information : contact@centreon.com
# 
#####################################################################################

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);


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

####################
## Verifying options
####################
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'};
}

###################################################################################
## vmstat output example :
#[nagios@linux]$ vmstat -s
# procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
#  r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
#  0  0    156 759428 553468 4897580    0    0     0   118    1    1  4  1 95  0  0
###################################################################################

######################################
## getting CPU : user system idle wait
######################################
#my $cpu = `vmstat | tail -1 | awk {'print \$13":"\$14":"\$15":"\$16 '}`;
my $cpu = `vmstat | tail -1 | awk {'print \$16":"\$17":"\ '}`;
#my ($user, $system, $idle, $wait) = split /:/, $cpu;
my ($user, $system, $wait) = split /:/, $cpu;
my $total = $system + $user;  


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

##############################
## printing cpu usage infos
##############################
my $warning = $OPTIONS{'warning'};
my $critical = $OPTIONS{'critical'};
printf "CPU usage : [total: %.2f%%] [user : %.2f%%] [system : %.2f%%] ", $total, $user, $system;
#printf "|cpu_user=%.2f%%;%.2f;%.2f cpu_system=%.2f%%;%.2f;%.2f cpu_idle=%.2f%%;%.2f;%.2f cpu_wait=%.2f%%;%.2f;%.2f\n", $user, $warning, $critical, $system, $warning, $critical, $idle, $warning, $critical, $wait, $warning, $critical;
printf "|cpu_total=%.2f%%;%.0f;%.0f;0;100 cpu_user=%.2f%%; cpu_system=%.2f%%;\n", $total, $warning, $critical, $user, $system;

##############
## return code
##############
if ($user + $system >= $critical) {
    exit $ERRORS{'CRITICAL'};
}
if ($user + $system >= $warning) {
    exit $ERRORS{'WARNING'};
}
exit $ERRORS{'OK'};

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