#! /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, 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.
# 
#    For information : david.barbion@adeoservices.com
####################################################################
#
# Script init
#

use strict;
use Switch ;
use List::Util qw[min max];
use Net::SNMP qw(:snmp);
use FindBin;
use Data::Dumper;
use lib "$FindBin::Bin";
use lib "/usr/lib/nagios/plugins";
use lib "/tmp" ;


use utils qw($TIMEOUT %ERRORS &print_revision &support);

use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_h $opt_V $opt_H $opt_C $opt_v $opt_o $opt_c $opt_w $opt_t $opt_p $opt_k $opt_u $opt_l);

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

Getopt::Long::Configure('bundling');
GetOptions
    ("h"   => \$opt_h, "help"         => \$opt_h,
     "u=s" => \$opt_u, "username=s"   => \$opt_u,
     "p=s" => \$opt_p, "password=s"   => \$opt_p,
     "k=s" => \$opt_k, "key=s"        => \$opt_k,
     "V"   => \$opt_V, "version"      => \$opt_V,
     "l"   => \$opt_l, "longoutput"   => \$opt_l,
     "v=s" => \$opt_v, "snmp=s"       => \$opt_v,
     "C=s" => \$opt_C, "community=s"  => \$opt_C,
     "w=s" => \$opt_w, "warning=s"    => \$opt_w,
     "c=s" => \$opt_c, "critical=s"   => \$opt_c,
     "H=s" => \$opt_H, "hostname=s"   => \$opt_H);

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

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

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

my $snmp = "1";
if ($opt_v && $opt_v =~ /^[0-9]$/) {
	$snmp = $opt_v;
}

if ($snmp eq "3") {
	if (!$opt_u) {
		print "Option -u (--username) is required for snmpV3\n";
		exit $ERRORS{'OK'};
	}
	if (!$opt_p && !$opt_k) {
		print "Option -k (--key) or -p (--password) is required for snmpV3\n";
		exit $ERRORS{'OK'};
	}elsif ($opt_p && $opt_k) {
		print "Only option -k (--key) or -p (--password) is needed for snmpV3\n";
		exit $ERRORS{'OK'};
	}
}

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

my $DS_type = "GAUGE";
($opt_t) || ($opt_t = shift) || ($opt_t = "GAUGE");
$DS_type = $1 if ($opt_t =~ /(GAUGE)/ || $opt_t =~ /(COUNTER)/);


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

#===  create a SNMP session ====

my ($session, $error);
if ($snmp eq "1" || $snmp eq "2") {
	($session, $error) = Net::SNMP->session(-hostname => $opt_H, -community => $opt_C, -version => $snmp);
	if (!defined($session)) {
	    print("UNKNOWN: SNMP Session : $error\n");
	    exit $ERRORS{'UNKNOWN'};
	}
}elsif ($opt_k) {
	($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp, -username => $opt_u, -authkey => $opt_k);
	if (!defined($session)) {
	    print("UNKNOWN: SNMP Session : $error\n");
	    exit $ERRORS{'UNKNOWN'};
	}
}elsif ($opt_p) {
	($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp,  -username => $opt_u, -authpassword => $opt_p);
	if (!defined($session)) {
	    print("UNKNOWN: SNMP Session : $error\n");
	    exit $ERRORS{'UNKNOWN'};
	}
}


# Here we go !
my $result ;
my $label ;
my $oid ;
my $unit = "";
my $return_result ;
my $return_code = 0 ;
my $output ;
my $total_connection = 0;
# parse sysDescr
my $sysdescr = $session->get_request(-varbindlist => [".1.3.6.1.2.1.1.1.0"]) ;
my $outlabel = $sysdescr->{".1.3.6.1.2.1.1.1.0"}.": " ;
my $longoutput = '';
my $perfdata = '';

# define some useful arrays
my %msa_sensors_oids = (
                        ".1.3.6.1.3.94.1.8.1.1" => "Sensor unitid",
                        ".1.3.6.1.3.94.1.8.1.2" => "Sensor index",
                        ".1.3.6.1.3.94.1.8.1.3" => "Sensor name",
                        ".1.3.6.1.3.94.1.8.1.4" => "Sensor status",
                        ".1.3.6.1.3.94.1.8.1.5" => "Sensor info",
                        ".1.3.6.1.3.94.1.8.1.6" => "Sensor message",
                        ".1.3.6.1.3.94.1.8.1.7" => "Sensor type",  
                        ".1.3.6.1.3.94.1.8.1.8" => "Sensor characteristic"
                        ) ;
my %sensor_data_table = ("SENSOR_UNITID"          => ".1.3.6.1.3.94.1.8.1.1",
                         "SENSOR_INDEX"           => ".1.3.6.1.3.94.1.8.1.2",
                         "SENSOR_NAME"            => ".1.3.6.1.3.94.1.8.1.3",
                         "SENSOR_STATUS"          => ".1.3.6.1.3.94.1.8.1.4",
                         "SENSOR_INFO"            => ".1.3.6.1.3.94.1.8.1.5",
                         "SENSOR_MESSAGE"         => ".1.3.6.1.3.94.1.8.1.6",
                         "SENSOR_TYPE"            => ".1.3.6.1.3.94.1.8.1.7",
                         "SENSOR_CHARACTERISTIC"  => ".1.3.6.1.3.94.1.8.1.8",) ;

# all sensors types
my @msa_sensors_type = ("undefined",
                        "unknown",
                        "other",
                        "battery",
                        "fan",
                        "power-supply",
                        "transmitter",
                        "enclosure",
                        "board",
                        "receiver") ;
# all sensors status
my @msa_sensors_status = ("undefined",
                          "unknown",
                          "other",
                          "ok",
                          "warning",
                          "failed") ;

# all sensors characteristics
my @msa_sensors_characteristic = ("undefined",
                                  "unknown",
                                  "other",
                                  "temperature",
                                  "pressure",
                                  "emf",
                                  "currentValue",
                                  "airflow",
                                  "frequency",
                                  "power",
                                  "door"
                                 ) ;

# get the sensor table
$result = $session->get_table(".1.3.6.1.3.94.1.8") ;
my %msa_sensors_values = %{$result} ;

my $sensor_id ;
my $key ;
my $value ;
my @msa_sensors ;
my $sensor ;
# create the sensor data table
foreach (keys %msa_sensors_values) {
    $value = $key = $sensor_id = $_ ;
    $sensor_id =~ s/.*\.([0-9]+)$/$1/;
    $key =~ s/(\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*).*/$1/ ;
    $msa_sensors[$sensor_id]{$key} = $msa_sensors_values{$value};
}

# parse the table to get the worse status and append it to the output string.
my $worse_status = 0;
foreach $sensor (@msa_sensors) {
    if (defined($sensor)) {
        my %sensor_data = %{$sensor} ;
	chomp($sensor_data{".1.3.6.1.3.94.1.8.1.6"});
        $worse_status = max($worse_status, $sensor_data{$sensor_data_table{"SENSOR_STATUS"}}) ;
	#print Dumper(\%sensor_data);
#Voltage (Controller Module B):  0.00V
	if ($sensor_data{".1.3.6.1.3.94.1.8.1.6"} =~ /Temperature.*:\s+(-*\d+)C/) {

		my $temperature = $1;
		if ($temperature > 0) {
			$longoutput .= "\n" . $sensor_data{".1.3.6.1.3.94.1.8.1.6"};
			my $pd = $sensor_data{".1.3.6.1.3.94.1.8.1.6"};
			$pd =~ s/\:.*$//g;
			$pd =~ s/[\(\),]//g;
			$pd =~ s/ /_/g;
			$perfdata .= "\"$pd\"=$temperature" . "C ";
		} else {
		}
	} elsif ($sensor_data{".1.3.6.1.3.94.1.8.1.6"} =~ /Voltage.*:\s+(\d+\.\d+)V/) {
		my $voltage = $1;
		if ($1 > 0) {
			$longoutput .= "\n" . $sensor_data{".1.3.6.1.3.94.1.8.1.6"};
			my $pd = $sensor_data{".1.3.6.1.3.94.1.8.1.6"};
			$pd =~ s/\:.*$//g;
			$pd =~ s/[\(\),]//g;
			$pd =~ s/ /_/g;
			$perfdata .= "\"$pd\"=$voltage" . "V ";
		} else {
		}
	} elsif ($sensor_data{".1.3.6.1.3.94.1.8.1.6"} =~ /Capacitor Charge.*: 0%/) {
	} else {
		$longoutput .= "\n" . $sensor_data{".1.3.6.1.3.94.1.8.1.6"};
	}

 #$longoutput .= $sensor_data{".1.3.6.1.3.94.1.8.1.6"} . "\n";
	#print Dumper(\%sensor_data);
        # a problem is found
        if ($sensor_data{$sensor_data_table{"SENSOR_STATUS"}} > 3) {
            $outlabel.=$sensor_data{$sensor_data_table{"SENSOR_MESSAGE"}} ;
            $outlabel.=" [".$msa_sensors_type[$sensor_data{$sensor_data_table{"SENSOR_TYPE"}}]."]. " ;
        }
    }
}

# get the return_code
switch ($worse_status) {
    case 0 { $return_code = -1 ;}
    case 1 { $return_code = -1 ;}
    case 2 { $return_code = -1 ;}
    case 3 { $return_code = 0 ;}
    case 4 { $return_code = 1 ;}
    case 5 { $return_code = 2 ;}
    else   { $return_code = -1 }
}

################################################################################
switch ($return_code) {
    case 0 { $outlabel.="OK"; }
    case 1 { $outlabel.="WARNING"; }
    case 2 { $outlabel.="CRITICAL"; }
    else   { $outlabel.="UNKOWN"; }
}
print $outlabel ;
print "$longoutput" if ($opt_l);
print " | $perfdata \n";
exit($return_code) ;

sub print_usage () {
    print "Usage:";
    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 "   -k (--key)        snmp V3 key\n";
    print "   -p (--password)   snmp V3 password\n";
    print "   -u (--username)   snmp v3 username \n";
    print "   -V (--version)    Plugin version\n";
    print "   -h (--help)       usage help\n";
}

sub print_help () {
    print "##############################################\n";
    print "#                ADEO Services               #\n";
    print "##############################################\n";
    print_usage();
    print "\n";
}