#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS);

use vars qw/ %opt /;
sub debug($);

getopts('c:dhH:p:P:S:u:w:V', \%opt);

if (exists $opt{h}) {
    usage();
    exit(0);
}

if (exists $opt{V}) {
    print '$Id: check_mysql_conn.pl 352 2007-08-02 13:21:42Z root $', "\n";
    exit(0);
}

my $debug = 0;
if (exists $opt{d}) {
    print "Enabling debug mode...\n";
    $debug = 1;
}

my $warn_threshold = 5;
if (exists $opt{w}) {
    $warn_threshold = $opt{w};
}
debug("\$warn_threshold=$warn_threshold\n");

my $critical_threshold = 10;
if (exists $opt{c}) {
    $critical_threshold = $opt{c};
}
debug("\$critical_threshold=$critical_threshold\n");

my $username = "monitoring";
if (exists $opt{u}) {
    $username = $opt{u};
}

my $password = "monitoring";
if (exists $opt{p}) {
    $password = $opt{p};
}

my $host = "127.0.0.1";
if (exists $opt{H}) {
	$host = $opt{H};
}

my $port = 3306;
if (exists $opt{P}) {
	$port = $opt{P};
}

my $socket = 0;
if (exists $opt{S}) {
	$socket = $opt{S};
}

my $cmdline = "/usr/bin/mysql -u $username -p$password";
if ($socket == 0) {
	$cmdline = "$cmdline -h $host -P $port";
} else {
	$cmdline = "$cmdline -S $socket";
}

my $query_output = `/bin/echo "SHOW GLOBAL STATUS LIKE 'Threads_connected';" \| $cmdline 2>/dev/null \| /bin/grep "Threads_connected"`;
debug("\$query_output=\"$query_output\"\n");
unless ($query_output =~ /^Threads_connected\s+(\d+)\s+$/) {
    print "Unknown: Unable to read output from MySQL\n";
    exit $ERRORS{'UNKNOWN'};
}

my $threads_connected = $1;
debug("\$threads_connected=$threads_connected\n");

my $perfdata = "mysql_connections=$threads_connected;$warn_threshold;$critical_threshold;0;0;";

if ($threads_connected > $critical_threshold) {
    print "Critical: $threads_connected active connections|$perfdata\n";
    exit $ERRORS{'CRITICAL'}
} elsif ($threads_connected > $warn_threshold) {
    print "Warning: $threads_connected active connections|$perfdata\n";
    exit $ERRORS{'WARNING'}
} else {
    print "OK: $threads_connected active connections|$perfdata\n";
    exit $ERRORS{'OK'}
}



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

sub usage {
    if (@_ == 1) {
	print "$0: $_[0].\n";
    }
    print << "EOF";
Usage: $0 [options]
  -w THRESHOLD
     Warning threshold for number of active connections (default: 5)
  -c THRESHOLD
     critical threshold for number of active connections (default: 10)
  -H HOST
  	 Connect to TCP address/DNS name (default: 127.0.0.1).
  -P PORT
  	 Use this alternate TCP port (default: 3306).
  -S SOCKET
  	 Use this MySQL socket instead of the default/or TCPIP.
  -u USERNAME
     The MySQL username to use when connecting to the server.
  -p PASSWORD
     The password to use when connecting to the server.
  -d
     enable debug mode (mutually exclusive to -q)
  -h
     display usage information
  -V
     display version number      
EOF
}

sub debug($) {
    if ($debug) {
	print STDERR $_[0];
    }
}
