#! /usr/bin/perl -w
###################################################################
# Oreon is developped with GPL Licence 2.0 
#
# GPL License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#
# Developped by : Julien Mathis - Romain Le Merlus 
#
###################################################################
# 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 : contact@merethis.com
####################################################################
#
# Plugin init
#
use strict;
use Net::SNMP qw(:snmp);
use FindBin;
use lib "$FindBin::Bin";
use lib "/usr/share/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_p $opt_c $opt_w);
use vars qw($snmp);

my $Revision = 1.2.1;

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

Getopt::Long::Configure('bundling');
GetOptions
    ("h"   => \$opt_h, "help"         => \$opt_h,
     "v=s"   => \$opt_v, "snmp_version=s" => \$opt_v,
     "V"   => \$opt_V, "version"      => \$opt_V,
     "H=s"   => \$opt_H, "Hostname=s"     => \$opt_H,
     "p=i"   => \$opt_p, "port=s"         => \$opt_p,
     "C=s"   => \$opt_C, "Community=s"    => \$opt_C,
     "c=s"=> \$opt_c, "w=s"=> \$opt_w
);

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

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

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

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

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

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

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

my $OID_TCP_PORT = ".1.3.6.1.2.1.6.13.1.3";

my $result = $session->get_table(Baseoid => $OID_TCP_PORT);
if (!defined($result)) {
    printf("ERROR: Description Table : %s.\n", $session->error);
    $session->close;
    exit $ERRORS{'UNKNOWN'};
}

my $cpt = 0;
foreach my $key (oid_lex_sort(keys %$result)) {
	if ($result->{$key} == $opt_p) {
 		$cpt++;
	}
}

if (!defined($opt_w)){$opt_w = 20;}
if (!defined($opt_c)){$opt_c = 30;}

print "Number of connections on port $opt_p :  $cpt |nb_conn=$cpt\n";
if ($cpt >= $opt_w && $cpt < $opt_c){
	exit $ERRORS{'WARNING'};
} elsif ($cpt >= $opt_c){
	exit $ERRORS{'CRITICAL'};
} else {
	exit $ERRORS{'OK'};
}
	

sub print_usage () {
    print "\nUsage:\n";
    print "$PROGNAME\n";
    print "   -H (--hostname)   Hostname to query - (required)\n";
    print "   -p (--port)	port you want to check - (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 "   -V (--version)    Plugin version\n";
    print "   -h (--help)       usage help\n";
}

sub print_help () {
    print "##############################################\n";
    print "#    Copyright (c) 2004-2007 Centreon        #\n";
    print "#    Bugs to http://bugs.oreon-project.org/  #\n";
    print "##############################################\n";
    print_usage();
    print "\n";
}