#!/usr/bin/perl -w

# COPYRIGHT:
#
# This software is Copyright (c) 2008 NETWAYS GmbH
#                                <support@netways.de>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from http://www.fsf.org.
#
# This work 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.fsf.org.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to NETWAYS GmbH.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# this Software, to NETWAYS GmbH, you confirm that
# you are the copyright holder for those contributions and you grant
# NETWAYS GmbH a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# Nagios and the Nagios logo are registered trademarks of Ethan Galstad.


use POSIX;
use strict;
use File::Basename;
use Getopt::Long;
use vars qw(
	    $opt_help
	    $opt_usage
	    $opt_version
	    $opt_ca
	    $opt_wa
	    $opt_ci
	    $opt_wi
	    $opt_vip
	    $opt_vport
	   );

sub print_usage();
sub print_help();

my $progname = basename($0);

my %ERRORS = ('UNKNOWN'  => '-1',
	      'OK'       => '0',
	      'WARNING'  => '1',
	      'CRITICAL' => '2');

$opt_wa = 0;
$opt_ca = 0;
$opt_wi = 0;
$opt_ci = 0;

Getopt::Long::Configure('bundling');
GetOptions
  (
   "v=s" =>  \$opt_vip,		"vip"     => \$opt_vip,
   "p=s" =>  \$opt_vport,	"vport"   => \$opt_vport,
   				"ca=s"    => \$opt_ca,
   				"wa=s"    => \$opt_wa,
   				"ci=s"    => \$opt_ci,
   				"wi=s"    => \$opt_wi,
   "h"   =>  \$opt_help,	"help"    => \$opt_help,
   				"usage"   => \$opt_usage,
   "V"   =>  \$opt_version, 	"version" => \$opt_version
  ) || die "Try `$progname --help' for more information.\n";

sub print_usage() {
  print "Usage: $progname -w WARNING -c CRITICAL\n";
  print "       $progname --help\n";
  print "       $progname --version\n";
}

sub print_help() {
  print "$progname - checks number of active and inactive connections\n";
  print "Options are:\n";
  print "  -v, --vip <number>               virtual ip address\n";
  print "  -p, --vport <number>             virtual port number\n";
  print "  --wa <number>                    warning if more than <number> active connections\n";
  print "  --ca <number>                    critical if more than <number> active connections\n";
  print "  --wi <number>                    warning if more than <number> inactive connections\n";
  print "  --ci <number>                    critical if more than <number> inactive connections\n";
  print "  -h, --help                       display this help and exit\n";
  print "      --usage                      display a short usage instruction\n";
  print "  -V, --version                    output version information and exit\n";
  print "Requirements:\n";
  print "  This plugin needs lvsgsp from http://www.linuxvirtualserver.org/~acassen/\n";
}

if ($opt_help) {
  print_help();
  exit $ERRORS{'UNKNOWN'};
}

if ($opt_usage) {
  print_usage();
  exit $ERRORS{'UNKNOWN'};
}

if ($opt_version) {
  print "$progname 0.0.1\n";
  exit $ERRORS{'UNKNOWN'};
}

##### DO THE WORK ######################################################################

my $lvsgsp = "/usr/local/bin/lvsgsp";
my $connections; 
my $inactive_connections;
my @lines;
my $command;
my $state = "OK";

# arguments OK
if ($opt_vip && $opt_vport) {

  $command = $lvsgsp . " -v " . $opt_vip . " " . $opt_vport;

  open(OUT, "$command |") || die "WRONG";
    @lines = <OUT>;
  close(OUT);

  chomp($connections = $lines[0]);
  chomp($inactive_connections = $lines[1]);
  
  # generate status
  if ($connections >= $opt_wa || $inactive_connections >= $opt_wi) {$state="WARNING";}
  if ($connections >= $opt_ca || $inactive_connections >= $opt_ci) {$state="CRITICAL";}

  print "$state $connections active, $inactive_connections inactive connections";
  print "|'active connections'=$connections;$opt_wa;$opt_ca 'inactive connections'=$inactive_connections;$opt_wa;$opt_ca \n";
  exit $ERRORS{$state};

# arguments not OK
} else {
  print_usage();
  exit $ERRORS{'UNKNOWN'};
}



