#! /usr/bin/perl -w
################################################################################
# Copyright 2004-2013 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
# 
# 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
# 
# SVN : $URL$
# SVN : $Id$
#
####################################################################################
#
# Script init
#

use strict;
require "/DBA/nagios/nagios-plugins/1.latest/plugins/Centreon/SNMP/Utils.pm";

use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_c $opt_w $opt_D @critical @warning);

my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);
my %centreon = Centreon::SNMP::Utils::load_oids($ERRORS{'UNKNOWN'}, "/DBA/nagios/nagios-plugins/1.latest/plugins/centreon.conf");

# Plugin var init

$PROGNAME = "$0";
sub print_help ();
sub print_usage ();

my %OPTION = (
    "host" => undef,
    "snmp-community" => "public", "snmp-version" => 1, "snmp-port" => 161, 
    "snmp-auth-key" => undef, "snmp-auth-user" => undef, "snmp-auth-password" => undef, "snmp-auth-protocol" => "MD5",
    "snmp-priv-key" => undef, "snmp-priv-password" => undef, "snmp-priv-protocol" => "DES",
    "maxrepetitions" => undef, "snmptimeout" => undef,
    "64-bits" => undef
);

Getopt::Long::Configure('bundling');
GetOptions
    (
    "H|hostname|host=s"         => \$OPTION{'host'},
    "C|community=s"             => \$OPTION{'snmp-community'},
    "v|snmp|snmp-version=s"     => \$OPTION{'snmp-version'},
    "P|snmpport|snmp-port=i"    => \$OPTION{'snmp-port'},
    "u|username=s"              => \$OPTION{'snmp-auth-user'},
    "p|authpassword|password=s" => \$OPTION{'snmp-auth-password'},
    "k|authkey=s"               => \$OPTION{'snmp-auth-key'},
    "authprotocol=s"            => \$OPTION{'snmp-auth-protocol'},
    "privpassword=s"            => \$OPTION{'snmp-priv-password'},
    "privkey=s"                 => \$OPTION{'snmp-priv-key'},
    "privprotocol=s"            => \$OPTION{'snmp-priv-protocol'},
    "maxrepetitions=s"          => \$OPTION{'maxrepetitions'},
    "snmp-timeout=i"            => \$OPTION{'snmptimeout'},
    "64-bits"                   => \$OPTION{'64-bits'},
    "h"   => \$opt_h, "help"        => \$opt_h,
    "V"   => \$opt_V, "version"     => \$opt_V,
    "w=s" => \$opt_w, "warning=s"   => \$opt_w,
    "c=s" => \$opt_c, "critical=s"  => \$opt_c);

if ($opt_V) {
    print_revision($PROGNAME,'$Revision: 1.2 $');
    exit $ERRORS{'OK'};
}

if ($opt_h) {
    print_help();
    exit $ERRORS{'OK'};
}

if ($opt_c && $opt_c =~ /^([0-9\.]+),([0-9\.]+),([0-9\.]+)$/) {
    @critical = ($1,$2,$3);
} elsif (!defined($opt_c) && !$opt_c){
    @critical = ('2', '4', '6');
} else {
    print "Specify three critical treshold separated with a coma\n";
    exit $ERRORS{'OK'};
}

if ($opt_w && $opt_w =~ /^([0-9\.]+),([0-9\.]+),([0-9\.]+)$/) {
    @warning = ($1,$2,$3);
} elsif (!defined($opt_w) && !$opt_w) {
    @warning = ('1', '3', '5');
} else {
    print "Specify three warning treshold separated with a coma\n";
    exit $ERRORS{'UNKNOWN'};
}

for (my $i = 0; $i < scalar(@warning); $i++) {
    if ($warning[$i] >= $critical[$i]) {
        print "Critical tresholds must be superior to warning tresholds.\n";
        exit $ERRORS{'UNKNOWN'};
    }
}

my $name = $0;
$name =~ s/\.pl.*//g;

# Plugin snmp requests
my $OID_CPULOAD_1 = $centreon{UNIX}{CPU_LOAD_1M};
my $OID_CPULOAD_5 = $centreon{UNIX}{CPU_LOAD_5M};
my $OID_CPULOAD_15 =$centreon{UNIX}{CPU_LOAD_15M};

my ($session_params) = Centreon::SNMP::Utils::check_snmp_options($ERRORS{'UNKNOWN'}, \%OPTION);
my $session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);

my $result = Centreon::SNMP::Utils::get_snmp_leef([$OID_CPULOAD_1, $OID_CPULOAD_5, $OID_CPULOAD_15], $session, $ERRORS{'UNKNOWN'});
my $un =  $result->{$OID_CPULOAD_1};
my $cinq  =  $result->{$OID_CPULOAD_5};
my $quinze  =  $result->{$OID_CPULOAD_15};

# Plugin return code
my $status = "OK";

$status = "WARNING" if ($warning[0] <= $un || $warning[1] <= $cinq || $warning[2] <= $quinze);
$status = "CRITICAL" if ($critical[0] <= $un || $critical[1] <= $cinq || $critical[2] <= $quinze);

# Print output
print "Load average: ".$un.", ".$cinq.", ".$quinze.".|load1=".$un." load5=".$cinq." load15=".$quinze."\n";
exit $ERRORS{$status};

sub print_usage () {
    print "\nUsage:\n";
    print "$PROGNAME\n";
    print "   -H (--hostname)   Hostname to query (required)\n";
    print "   -C (--community)  SNMP read community (defaults to public)\n";
    print "                     used with SNMP v1 and v2c\n";
    print "   -v (--snmp-version)  1 for SNMP v1 (default)\n";
    print "                        2 for SNMP v2c\n";
    print "                        3 for SNMP v3\n";
    print "   -P (--snmp-port)  SNMP port (default: 161)\n";
    print "   -k (--authkey)    snmp V3 key\n";
    print "   -u (--username)   snmp V3 username \n";
    print "   -p (--password)   snmp V3 password\n";
    print "   --authprotocol    protocol MD5/SHA  (v3)\n";
    print "   --privprotocol    encryption system (DES/AES)(v3) \n";
    print "   --privpassword    passphrase (v3) \n";
    print "   --64-bits         Use 64 bits OID\n";
    print "   --maxrepetitions  To use when you have the error: 'Message size exceeded buffer maxMsgSize'\n";
    print "                     Work only with SNMP v2c and v3 (Example: --maxrepetitions=1)\n";
    print "   --snmp-timeout    SNMP Timeout\n";
    print "   -c (--critical)   Three critical tresholds (defaults : 2,4,6)\n";
    print "   -w (--warning)   Three warning tresholds (defaults : 1,3,5)\n";
    print "   -V (--version)    Plugin version\n";
    print "   -h (--help)       usage help\n";
}

sub print_help () {
    print "##############################################\n";
    print "#    Copyright (c) 2004-2013 Centreon        #\n";
    print "#    Bugs to http://forge.centreon.com/      #\n";
    print "##############################################\n";
    print_usage();
    print "\n";
}
