#! /usr/bin/perl -w
################################################################################
# Copyright 2004-2011 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: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_snmp_uptime $
# SVN : $Id: check_centreon_snmp_uptime 11631 2011-02-08 17:02:51Z shotamchay $
#
####################################################################################
#
# Plugin init
#
use strict;
use Net::SNMP qw(:snmp);
use FindBin;
use lib "$FindBin::Bin";
use lib "/usr/lib/nagios/plugins";
use utils qw($TIMEOUT %ERRORS &print_revision &support);

if (eval "require centreon" ) {
	use centreon qw(get_parameters);
	use vars qw($VERSION %centreon);
	%centreon=get_parameters();
} else {
	print "Unable to load centreon perl module\n";
    exit $ERRORS{'UNKNOWN'};
}

use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_h $opt_V $opt_H $opt_C $opt_v $opt_d $opt_P $day);

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

Getopt::Long::Configure('bundling');
GetOptions
    ("h"   => \$opt_h, "help"         => \$opt_h,
     "V"   => \$opt_V, "version"      => \$opt_V,
     "v=s" => \$opt_v, "snmp=s"       => \$opt_v,
     "C=s" => \$opt_C, "community=s"  => \$opt_C,
	 "P=s"   => \$opt_P, "snmp-port=s" => \$opt_P,
     "d"   => \$opt_d, "day"     => \$opt_d,
     "H=s" => \$opt_H, "hostname=s"   => \$opt_H);

if ($opt_V) {
    print_revision($PROGNAME,'$Revision: 1.2 $');
    exit $ERRORS{'OK'};
}
if (!defined($opt_P)) {
	$opt_P = 161;
}
if ($opt_h) {
    print_help();
    exit $ERRORS{'OK'};
}

$opt_H = shift unless ($opt_H);
(print_usage() && exit $ERRORS{'OK'}) unless ($opt_H);


($opt_v) || ($opt_v = shift) || ($opt_v = "1");
my $snmp = $1 if ($opt_v =~ /(\d)/);

($opt_C) || ($opt_C = shift) || ($opt_C = "public");

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


# Plugin snmp requests
my $OID_OBJECTID =$centreon{MIB2}{OBJECTID};
my $OID_UPTIME_WINDOWS =$centreon{MIB2}{UPTIME_WINDOWS};
my $OID_UPTIME_OTHER =$centreon{MIB2}{UPTIME_OTHER};

# create a SNMP session
my ( $session, $error ) = Net::SNMP->session(-hostname  => $opt_H,-community => $opt_C, -version  => $snmp, -port => $opt_P);
if ( !defined($session) ) {
    print("CRITICAL: $error");
    exit $ERRORS{'CRITICAL'};
}

my $result = $session->get_request(-varbindlist => [$OID_OBJECTID]);
if (!defined($result)) {
    printf("UNKNOWN: %s.\n", $session->error);
    $session->close;
    exit $ERRORS{'UNKNOWN'};
}

my $return_result =  $result->{$OID_OBJECTID};
my $OID = "";
if ($return_result =~ /.*Windows.*/i ) {
    $OID = $OID_UPTIME_WINDOWS;
} else {
    $OID = $OID_UPTIME_OTHER;
}

$result = $session->get_request(-varbindlist => [$OID]);
if (!defined($result)) {
    printf("UNKNOWN: %s.\n", $session->error);
    $session->close;
    exit $ERRORS{'UNKNOWN'};
}

my $un = 0;

$return_result =  $result->{$OID};
if ( $return_result =~ m/(\d*) day[s]?,\s*(\d*):(\d*):(\d*).(\d*)/ ) {
 	$un = $5 + $4 * 100 + $3 * 100 * 60 + $2 * 100 * 60 * 60 + $1 * 100 * 60 * 60 * 24;
 	$day = $1;
}

if ( $return_result =~ m/(\d*) hour.*(\d*):(\d*).(\d*)/ ) {
	$un = $4 + $3 * 100 + $3 * 100 * 60 + $1 * 100 * 60 * 60 ;
}

if ($opt_d) {
    $un = $day;
}

if ($un || ( $un == 0) ){
    if ($opt_d) {
        print "OK - Uptime (in day): $un|uptime=".$un."day\(s\)\n";
    } else {
        print "OK - Uptime (in hundredths of a second): $un|uptime=".$un."hs\n";
    }
    exit $ERRORS{'OK'};
}
else{
   print "CRITICAL Host unavailable\n";
   exit $ERRORS{'CRITICAL'};
}


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 "   -P (--snmp-port)	SNMP port (default: 161)\n";
    print "   -d (--day)        Uptime in day\n";
    print "   -V (--version)    Plugin version\n";
    print "   -h (--help)       usage help\n";

}

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