#! /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: http://svn.centreon.com/trunk/plugins-2.x/src/check_centreon_snmp_traffic $
# SVN : $Id: check_centreon_snmp_traffic 13081 2012-06-07 09:15:11Z jmathis $
#
####################################################################################
#
# 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_i $opt_n $opt_w $opt_c $opt_s $opt_T $opt_a $opt_r $opt_S $opt_o);

my $centplugins_path = "/DBA/centreon-data/centreon-plugins/tmp";
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
my $session;
my ($row, $last_check_time, $last_in_bits, $last_out_bits, @last_values, $update_time, $in_traffic, $out_traffic, $in_usage, $out_usage);

$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,
    "64-bits" => undef, "snmptimeout" => undef,

    "disable-warn-state" => undef
);

my %supported_oids = (
    ifDescr => "IF_DESC",
    ifAlias => "IF_ALIAS",
    ifName => "IF_NAME"
);

# Catch UNKNOWN From GetOptions (like -i without value)
$SIG{'__WARN__'} = sub { print $_[0]; exit($ERRORS{'UNKNOWN'}); };

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'},

    "disable-warn-state"    => \$OPTION{'disable-warn-state'},
    "h"   => \$opt_h, "help"        => \$opt_h,
    "s"   => \$opt_s, "show"        => \$opt_s,
    "V"   => \$opt_V, "version"     => \$opt_V,
    "i=s" => \$opt_i, "interface=s" => \$opt_i,
    "n"   => \$opt_n, "name"        => \$opt_n,
    "w=s" => \$opt_w, "warning=s"   => \$opt_w,
    "c=s" => \$opt_c, "critical=s"  => \$opt_c,
    "T=s" => \$opt_T, "r"           => \$opt_r,
    "S"   => \$opt_S,
    "a=i" => \$opt_a, "cache=s"     => \$opt_a,
    "o=s" => \$opt_o, "oid=s"       => \$opt_o
);

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

if ($opt_h) {
    print_help();
    exit $ERRORS{'OK'};
    Getopt::Long::Configure('bundling');
}

##################################################
#####      Verify Options
##

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

######
### Others
##
if (defined($opt_n) && !defined($opt_i)) {
    print "Option -n (--name) need option -i (--interface)\n";
    exit $ERRORS{'UNKNOWN'};
}

if (!defined($opt_i)) {
    $opt_i = 1;
}

if (!defined($opt_a)) {
    $opt_a = 3;
}

if (defined $opt_o) {
    if (!exists $supported_oids{$opt_o}) {
        print "Unsupported oid\n";
        exit $ERRORS{UNKNOWN};
    }
} else {
    $opt_o = 'ifDescr';
}

my $critical = 95;
if ($opt_c && $opt_c =~ /[0-9]+/) {
    $critical = $opt_c;
}
my $warning = 80;
if ($opt_w && $opt_w =~ /[0-9]+/) {
    $warning = $opt_w;
}

my $interface;
if ($opt_i =~ /^([0-9]+)$/ && !defined($opt_n)) {
    $interface = $1;
} elsif (!defined($opt_n)) {
    print "Unknown -i number expected... or it doesn't exist, try another interface - number\n";
    exit $ERRORS{'UNKNOWN'};
}

if ($critical <= $warning){
    print "(--crit) must be superior to (--warn)";
    print_usage();
    exit $ERRORS{'OK'};
}

#################################################
#####            Plugin snmp requests
##

$session = Centreon::SNMP::Utils::connection($ERRORS{'UNKNOWN'}, $session_params);

my $OID_DESC = $centreon{MIB2}{$supported_oids{$opt_o}};
my $OID_OPERSTATUS = $centreon{MIB2}{IF_OPERSTATUS};
my @operstatus = ("up","down","testing", "unknown", "dormant", "notPresent", "lowerLayerDown");

my $cacheFile = "$centplugins_path/traffic_cache_". $OPTION{'host'};
my $result;
my $mustCreateFile = 0;
my $countLine;

#
# Cache File exists, lets read it
#
if (-e $cacheFile) {
    open(FILE,"<".$cacheFile);
    $row = <FILE>;
    if (defined($row)) {
        chomp $row;
        my $deltaTime = time() - $row;
        if ($deltaTime > ($opt_a * 3600)) {
            $mustCreateFile = 1;
        }
    }
    close(FILE);
    
    # Manage file empty or line 1 empty
    if (!defined($row) || $row eq '') {
        $mustCreateFile = 1;
    }
} else {
    $mustCreateFile = 1;
}

if ($mustCreateFile) {
    $result = Centreon::SNMP::Utils::get_snmp_table($OID_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
    unless (open(FILE,">".$cacheFile)){
        print "Check mod for temporary file : ".$cacheFile."...\n";
        exit $ERRORS{"UNKNOWN"};
    }
    my $currentTime = time();
    print FILE $currentTime."\n";
    foreach my $key (oid_lex_sort(keys %$result)) {
        my @oid_list = split (/\./,$key);
        my $interfaceIndex = pop (@oid_list);
        print FILE $interfaceIndex.";".$result->{$key}."\n";
    }
    close(FILE);
}

################################################################
# Getting interface using its name instead of its oid index
if ($opt_n) {
    if (!-e $cacheFile) {
        printf("ERROR: Could not open " . $cacheFile);
        exit $ERRORS{'UNKNOWN'};
    }
        
    open(FILE,"<".$cacheFile);
    $countLine = 0;
    while ($row = <FILE>) {
        if ($countLine) {
            my @resLine = split(/\;/, $row);            
         
            if (defined($opt_r) && $resLine[1] =~ /$opt_i/) {
                $interface = $resLine[0];
            } else {
                $resLine[1] =~ s/\x00//g;
                $resLine[1] =~ s/\n//g;
                $resLine[1] =~ s/\s*$//g;
                if ($resLine[1] eq $opt_i) {
                    $interface = $resLine[0];
                }
            } 
        }
        $countLine++;
    }
    close(FILE);
    
    # Can't find an interface in cache file
    if (!defined($interface)) {
        print "ERROR: Can't find interface name '$opt_i' in cache file '$cacheFile'. Maybe you need a cache rebuild (Command with option '-a 0').";
        exit($ERRORS{'UNKNOWN'});
    }
}

my ($OID_IN, $OID_OUT, $OID_SPEED, $OID_SPEED_BASE);
if (defined($OPTION{'64-bits'})) {
    $OID_IN =$centreon{MIB2}{IF_IN_OCTET_64_BITS}.".".$interface;
    $OID_OUT = $centreon{MIB2}{IF_OUT_OCTET_64_BITS}.".".$interface;
    $OID_SPEED = $centreon{MIB2}{IF_SPEED_64_BITS}.".".$interface;
    $OID_SPEED_BASE = $centreon{MIB2}{IF_SPEED_64_BITS};
} else {
    $OID_IN =$centreon{MIB2}{IF_IN_OCTET}.".".$interface;
    $OID_OUT = $centreon{MIB2}{IF_OUT_OCTET}.".".$interface;
    $OID_SPEED = $centreon{MIB2}{IF_SPEED}.".".$interface;
    $OID_SPEED_BASE = $centreon{MIB2}{IF_SPEED};
}

# Get desctiption table
if ($opt_s) {
    if (!-e $cacheFile) {
        $result = Centreon::SNMP::Utils::get_snmp_table($OID_DESC, $session, $ERRORS{'UNKNOWN'}, \%OPTION);
        unless (open(FILE,">".$cacheFile)){
            print "Check mod for temporary file : ".$cacheFile."...\n";
            exit $ERRORS{"UNKNOWN"};
        }
        my $currentTime = time();
        print FILE $currentTime."\n";
        foreach my $key (oid_lex_sort(keys %$result)) {
            my @oid_list = split (/\./,$key);
            my $interfaceIndex = pop (@oid_list);
            print FILE $interfaceIndex.";".$result->{$key}."\n";
        }
        close(FILE);
    }
    
    if (!-e $cacheFile) {
        printf("ERROR: Could not open " . $cacheFile);        
        exit $ERRORS{'UNKNOWN'};
    }
    open(FILE,"<".$cacheFile);
    $countLine = 0;
    while ($row = <FILE>){
        if ($countLine) {
            my @resLine = split(/\;/, $row);            
            my $index = $resLine[0];
            my $interface_status = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$index], $session, $ERRORS{'UNKNOWN'});
            $resLine[1] =~ s/\x00//g;
            $resLine[1] =~ s/\n//g;
            print "Interface ". $index . " :: " . $resLine[1] . " :: ".$operstatus[$interface_status->{$OID_OPERSTATUS.".".$index} - 1];
            if ($opt_S) {
                my $link_speed = Centreon::SNMP::Utils::get_snmp_leef([$OID_SPEED_BASE.".".$index], $session, $ERRORS{'UNKNOWN'});
                if (!defined($link_speed)) {
                    printf("ERROR: Interface Speed Request : %s", $session->error);
                    exit $ERRORS{'UNKNOWN'};
                }
                my $unit = "bit/s";
                my $speed = $link_speed->{$OID_SPEED_BASE.".".$index};
                print " :: speed ".$speed." ".$unit."\n";
            } else {
                print "\n";
            }
        }
        $countLine++;
    }
    close(FILE);
    exit $ERRORS{'OK'};
}


$result = Centreon::SNMP::Utils::get_snmp_leef([$OID_OPERSTATUS.".".$interface, $OID_IN, $OID_OUT, $OID_SPEED], $session, $ERRORS{'UNKNOWN'},
                                               defined($opt_n) ? "You must specify interface name when option -n is used." : undef);
if (!defined($result->{$OID_OPERSTATUS.".".$interface}) || $result->{$OID_OPERSTATUS.".".$interface} eq "") {
    print "ERROR: Can't get interface '$interface' status\n";
    exit $ERRORS{'CRITICAL'};
}
if ($operstatus[$result->{$OID_OPERSTATUS.".".$interface} - 1] ne "up") {
    if (defined($OPTION{'disable-warn-state'})) {
        print "OK: interface is not ready - status : " . $operstatus[$result->{$OID_OPERSTATUS.".".$interface} - 1] . "\n";
        exit $ERRORS{'OK'};
    } else {
        print "ERROR: interface is not ready - status : " . $operstatus[$result->{$OID_OPERSTATUS.".".$interface} - 1] . "\n";
        exit $ERRORS{'CRITICAL'};
    }
}

#######  Get IN bytes
my $in_bits;
if (!defined($result->{$OID_IN})) {
    print "ERROR: Can't get interface '$interface' IN Bits\n";
    exit $ERRORS{'CRITICAL'};
}
$in_bits =  $result->{$OID_IN} * 8;

#######  Get OUT bytes
my $out_bits;
if (!defined($result->{$OID_OUT})) {
    print "ERROR: Can't get interface '$interface' OUT Bits\n";
    exit $ERRORS{'CRITICAL'};
}
$out_bits = $result->{$OID_OUT} * 8;

#######  Get SPEED of interface
my $speed_card;
if (defined($opt_T)){
    $speed_card = $opt_T * 1000000;
} else {
    $speed_card = $result->{$OID_SPEED};
    if (!defined($result->{$OID_SPEED}) || int($result->{$OID_SPEED}) !~ /^[0-9]+$/) {
        print "ERROR: Card speed is null or incorrect. You should force the value with -T option.\n";
        exit $ERRORS{'UNKNOWN'};
    }
    if (defined($OPTION{'64-bits'})) {
        $speed_card = $speed_card * 1000000;
    }
}

#############################################
#####          Plugin return code
##

$last_in_bits = 0;
$last_out_bits  = 0;

my $flg_created = 0;

if (-e "$centplugins_path/traffic_if".$interface."_".$OPTION{'host'}) {
    open(FILE,"<"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'});
    while($row = <FILE>){
        @last_values = split(":",$row);
        $last_check_time = $last_values[0];
        $last_in_bits = $last_values[1];
        $last_out_bits = $last_values[2];
        $flg_created = 1;
    }
    close(FILE);
} else {
    $flg_created = 0;
}

$update_time = time();

unless (open(FILE,">"."$centplugins_path/traffic_if".$interface."_".$OPTION{'host'})){
    print "Check mod for temporary file : $centplugins_path/traffic_if".$interface."_".$OPTION{'host'}. " !\n";
    exit $ERRORS{"UNKNOWN"};
}
print FILE "$update_time:$in_bits:$out_bits";
close(FILE);

if ($flg_created == 0){
    print "First execution : Buffer in creation.... \n";
    exit($ERRORS{"UNKNOWN"});
}

## Bandwith = IN + OUT / Delta(T) = 6 Mb/s
## (100 * Bandwith) / (2(si full duplex) * Ispeed)
## Count must round at 4294967296 
##

if (($in_bits - $last_in_bits != 0) && defined($last_in_bits)) {
    my $total = 0;
    if ($in_bits < $last_in_bits && !defined($OPTION{'64-bits'})) {
        print "ERROR: IN counter is going back. Two cases: 1) equipment restarted (= ok the next check), COUNTER in 32 bits (= should use COUNTER in 64 bits with option '--64-bits')\n";  
        exit($ERRORS{"UNKNOWN"});
    } elsif ($in_bits < $last_in_bits && defined($OPTION{'64-bits'})) {
        print "ERROR: IN counter is going back. One case: 1) equipment restarted (= ok the next check)\n";  
        exit($ERRORS{"UNKNOWN"});
    } else {
        $total = $in_bits - $last_in_bits;
    }
    my $diff = time() - $last_check_time;
    if ($diff == 0) {
        $diff = 1;
    }
    my $pct_in_traffic = $in_traffic = abs($total / $diff);
} else {
    $in_traffic = 0;
} 

if ($out_bits - $last_out_bits != 0 && defined($last_out_bits)) {
    my $total = 0;
    if ($out_bits < $last_out_bits && !defined($OPTION{'64-bits'})) {
        print "ERROR: OUT counter is going back. Two cases: 1) equipment restarted (= ok the next check), COUNTER in 32 bits (= should use COUNTER in 64 bits with option '--64-bits')\n";  
        exit($ERRORS{"UNKNOWN"});
    } elsif ($out_bits < $last_out_bits && defined($OPTION{'64-bits'})) {
        print "ERROR: OUT counter is going back. One case: 1) equipment restarted (= ok the next check)\n";  
        exit($ERRORS{"UNKNOWN"});
    } else {
        $total = $out_bits - $last_out_bits;
    }
    my $diff =  time() - $last_check_time;
    if ($diff == 0) {
        $diff = 1;
    }
    my $pct_out_traffic = $out_traffic = abs($total / $diff);
} else {
    $out_traffic = 0;
}

if ( $speed_card != 0 ) {
    $in_usage = sprintf("%.1f",($in_traffic * 100) / $speed_card);
    $out_usage = sprintf("%.1f",($out_traffic * 100) / $speed_card);
}

my $in_prefix = "";
my $out_prefix = "";

my $in_perfparse_traffic = $in_traffic;
my $out_perfparse_traffic = $out_traffic;

if ($in_traffic > 1000) {
    $in_traffic = $in_traffic / 1000;
    $in_prefix = "k";
    if($in_traffic > 1000){
        $in_traffic = $in_traffic / 1000;
        $in_prefix = "M";
    }
    if($in_traffic > 1000){
        $in_traffic = $in_traffic / 1000;
        $in_prefix = "G";
    }
}

if ($out_traffic > 1000){
    $out_traffic = $out_traffic / 1000;
    $out_prefix = "k";
    if ($out_traffic > 1000){
        $out_traffic = $out_traffic / 1000;
        $out_prefix = "M";
    }
    if ($out_traffic > 1000){
        $out_traffic = $out_traffic / 1000;
        $out_prefix = "G";
    }
}

my $in_bits_unit = "";
$in_bits = $in_bits/1048576;
if ($in_bits > 1000){
    $in_bits = $in_bits / 1000;
    $in_bits_unit = "G";
} else { 
    $in_bits_unit = "M";
}

my $out_bits_unit = "";
$out_bits = $out_bits/1048576;
if ($out_bits > 1000){
    $out_bits = $out_bits / 1000;
    $out_bits_unit = "G";
} else {
    $out_bits_unit = "M";
}

if ( $speed_card == 0 ) {
    print "CRITICAL: Interface speed equal 0! Interface must be down.|traffic_in=0B/s traffic_out=0B/s\n";
    exit($ERRORS{"CRITICAL"});
}

#####################################
#####        Display result
##


my $in_perfparse_traffic_str = sprintf("%.1f",abs($in_perfparse_traffic));
my $out_perfparse_traffic_str = sprintf("%.1f",abs($out_perfparse_traffic));

$in_perfparse_traffic_str =~ s/\./,/g;
$out_perfparse_traffic_str =~ s/\./,/g;

my $status = "OK";

if(($in_usage > $warning) or ($out_usage > $warning)){
    $status = "WARNING";
}
if (($in_usage > $critical) or ($out_usage > $critical)){
    $status = "CRITICAL";
}

my $warningBit = $warning * $speed_card / 100;
my $criticalBit = $critical * $speed_card / 100;

printf("Traffic In : %.2f ".$in_prefix."b/s (".$in_usage." %%), Out : %.2f ".$out_prefix."b/s (".$out_usage." %%) ", $in_traffic, $out_traffic);
if ($opt_S) {
    printf(" - Link Speed : %d", $speed_card);
}
printf("|traffic_in=".$in_perfparse_traffic_str."Bits/s;$warningBit;$criticalBit;0;$speed_card traffic_out=".$out_perfparse_traffic_str."Bits/s;$warningBit;$criticalBit;0;$speed_card\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 "   -s (--show)          Describes all interfaces number (debug mode)\n";
    print "   -i (--interface)     Set the interface number (2 by default)\n";
    print "   -o (--oid)           Specify the OID used to retrieve the interface (ifDescr/ifAlias/ifName)\n";
    print "   -n (--name)          Allows to use interface name with option -i instead of interface oid index\n";
    print "                        (ex: -i \"eth0\" -n, -i \"VMware Virtual Ethernet Adapter for VMnet8\" -n\n";
    print "                        (choose an unique expression for each interface)\n";
    print "   -w (--warning)       Signal strength at which a warning message will be generated\n";
    print "                        (default 80)\n";
    print "   -c (--critical)      Signal strength at which a critical message will be generated\n";
    print "   --disable-warn-state Option for not alerting on interface state\n";
    print "   -T                   Set maximum bandwidth\n";
    print "   -S                   Show link speed in output\n";
    print "   -V (--version)       Plugin version\n";
    print "   -r                   Regexp Match Mode\n";
    print "   -a (--cache)         Updates cache file every n hours instead of doing snmpwalk for every check (default: 3)\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";
}
