#!/usr/bin/perl -w

# COPYRIGHT:
#
# This software is Copyright (c) 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_critical
	    $opt_warning
	   );

sub print_usage();
sub print_help();

my $progname = basename($0);

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

Getopt::Long::Configure('bundling');
GetOptions
  (
   "c=s" => \$opt_critical, "critical=s" => \$opt_critical,
   "w=s" => \$opt_warning,  "warning=s"  => \$opt_warning,
   "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 - check current apache requests\n";
  print "Options are:\n";
  print "  -c, --critical \n";
  print "  -w, --warning \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";
}

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 $value=`/usr/bin/lynx -dump localhost/server-status?auto | /usr/bin/awk '/^Busy/ {print \$2}'`;
chomp($value);
my $state="OK";

if ($opt_warning && $opt_critical) {

  # generate status
  if ($value >= $opt_warning) {$state="WARNING";}
  if ($value >= $opt_critical) {$state="CRITICAL";}
  
  print "$state - $value current apache requests | 'apache requests'=".$value.";".$opt_warning.";".$opt_critical."\n";
  exit $ERRORS{$state};

}
else {
  print_usage();
  exit $ERRORS{'UNKNOWN'};
}

########################################################################################
