<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Monitor number of active connections to MySQL using Nagios</title>
	<atom:link href="http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/feed/" rel="self" type="application/rss+xml" />
	<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Fri, 12 Mar 2010 02:10:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Administrator</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-18139</link>
		<dc:creator>Administrator</dc:creator>
		<pubDate>Sat, 27 Jun 2009 19:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-18139</guid>
		<description>I have also submitted my plugin to &lt;a href="http://www.monitoringexchange.org/cgi-bin/page.cgi?g=Detailed%2F3119.html;d=1" rel="nofollow"&gt;MonitoringExchange&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>I have also submitted my plugin to <a href="http://www.monitoringexchange.org/cgi-bin/page.cgi?g=Detailed%2F3119.html;d=1" rel="nofollow">MonitoringExchange</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Administrator</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17568</link>
		<dc:creator>Administrator</dc:creator>
		<pubDate>Sat, 13 Jun 2009 19:12:57 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17568</guid>
		<description>Below find an updated version of my script with Berny's extensions. It also includes a proper license (the GPL). You can download the updated version from &lt;a href="http://bergs.biz/blog/static/check_mysql_conn.pl" rel="nofollow"&gt;this&lt;/a&gt; URL.
&lt;pre&gt;
#!/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:Lp:P:S:u:w:V', \%opt);

if (exists $opt{h}) {
    usage();
    exit(0);
}

if (exists $opt{V}) {
    print '$Id: check_mysql_conn.pl 27 2009-06-13 19:14:27Z rabe $', "\n";
    exit(0);
}

if (exists $opt{L}) {
    print &lt; &lt; "EOF";
Copyright (C) 2008-2009 Ralf G. R. Bergs

Thanks for bugfixes and feature updates to Ian K. and Bernd
Stroessenreuther.

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 3 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.

You should have received a copy of the GNU General Public License
along with this program; if not, see &lt;http://www.gnu.org/licenses/&gt;.
EOF
    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 = "";
if (exists $opt{S}) {
	$socket = $opt{S};
}

my $cmdline = "/usr/bin/mysql -u $username -p$password";
if ($socket eq "") {
	$cmdline = "$cmdline -h $host -P $port";
} else {
	$cmdline = "$cmdline -S $socket";
}

debug("\$cmdline=\"$cmdline\"\n");
my $query_output = `/bin/echo "SHOW GLOBAL STATUS LIKE 'Threads_connected';" \&#124; $cmdline 2&gt;/dev/null \&#124; /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 &gt; $critical_threshold) {
    print "Critical: $threads_connected active connections&#124;$perfdata\n";
    exit $ERRORS{'CRITICAL'}
} elsif ($threads_connected &gt; $warn_threshold) {
    print "Warning: $threads_connected active connections&#124;$perfdata\n";
    exit $ERRORS{'WARNING'}
} else {
    print "OK: $threads_connected active connections&#124;$perfdata\n";
    exit $ERRORS{'OK'}
}



###########################################################################

sub usage {
    if (@_ == 1) {
	print "$0: $_[0].\n";
    }
    print &lt; &lt; "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
  -L
     display license
  -V
     display version number      
EOF
}

sub debug($) {
    if ($debug) {
	print STDERR $_[0];
    }
}
&lt;/pre&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Below find an updated version of my script with Berny&#8217;s extensions. It also includes a proper license (the GPL). You can download the updated version from <a href="http://bergs.biz/blog/static/check_mysql_conn.pl" rel="nofollow">this</a> URL.</p>
<pre>
#!/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:Lp:P:S:u:w:V', \%opt);

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

if (exists $opt{V}) {
    print '$Id: check_mysql_conn.pl 27 2009-06-13 19:14:27Z rabe $', "\n";
    exit(0);
}

if (exists $opt{L}) {
    print < < "EOF";
Copyright (C) 2008-2009 Ralf G. R. Bergs

Thanks for bugfixes and feature updates to Ian K. and Bernd
Stroessenreuther.

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 3 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.

You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
EOF
    exit(0);
}

my $debug = 0;
if (exists $opt{d}) {
    print &#8220;Enabling debug mode&#8230;\n&#8221;;
    $debug = 1;
}

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

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

my $username = &#8220;monitoring&#8221;;
if (exists $opt{u}) {
    $username = $opt{u};
}

my $password = &#8220;monitoring&#8221;;
if (exists $opt{p}) {
    $password = $opt{p};
}

my $host = &#8220;127.0.0.1&#8243;;
if (exists $opt{H}) {
	$host = $opt{H};
}

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

my $socket = &#8220;&#8221;;
if (exists $opt{S}) {
	$socket = $opt{S};
}

my $cmdline = &#8220;/usr/bin/mysql -u $username -p$password&#8221;;
if ($socket eq &#8220;&#8221;) {
	$cmdline = &#8220;$cmdline -h $host -P $port&#8221;;
} else {
	$cmdline = &#8220;$cmdline -S $socket&#8221;;
}

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

my $threads_connected = $1;
debug(&#8221;\$threads_connected=$threads_connected\n&#8221;);

my $perfdata = &#8220;mysql_connections=$threads_connected;$warn_threshold;$critical_threshold;0;0;&#8221;;

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

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

sub usage {
    if (@_ == 1) {
	print &#8220;$0: $_[0].\n&#8221;;
    }
    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
  -L
     display license
  -V
     display version number
EOF
}

sub debug($) {
    if ($debug) {
	print STDERR $_[0];
    }
}
</pre>
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17521</link>
		<dc:creator>Christian</dc:creator>
		<pubDate>Fri, 12 Jun 2009 15:49:13 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17521</guid>
		<description>Re: check_mysql doesn't support tracking the currently running threads.

check_mysql does show that, if you pass -S (or --check-slave). Sure, it isn't completely desirable to show the complete load of information if you just wish to parse the amount of current running threads.

Also, if you want a completed version of the plugin (like what Berny added $host and perfdata, and $port and $socket), you'll be finding it here: http://chrischie.users.barfoo.org/check_mysql_connections.pl</description>
		<content:encoded><![CDATA[<p>Re: check_mysql doesn&#8217;t support tracking the currently running threads.</p>
<p>check_mysql does show that, if you pass -S (or &#8211;check-slave). Sure, it isn&#8217;t completely desirable to show the complete load of information if you just wish to parse the amount of current running threads.</p>
<p>Also, if you want a completed version of the plugin (like what Berny added $host and perfdata, and $port and $socket), you&#8217;ll be finding it here: <a href="http://chrischie.users.barfoo.org/check_mysql_connections.pl" rel="nofollow">http://chrischie.users.barfoo.org/check_mysql_connections.pl</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Berny</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17479</link>
		<dc:creator>Berny</dc:creator>
		<pubDate>Wed, 10 Jun 2009 11:35:53 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17479</guid>
		<description>Hi,

also added perfdata now...

Berny</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>also added perfdata now&#8230;</p>
<p>Berny</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Berny</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17478</link>
		<dc:creator>Berny</dc:creator>
		<pubDate>Wed, 10 Jun 2009 11:09:00 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-17478</guid>
		<description>Hi,

I corrected an error in use of parameter password and and added an option to tell the plugin on which host the mysql is running (if not localhost).
Please contact me by eMail, if You are interested in these changes.

Berny</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I corrected an error in use of parameter password and and added an option to tell the plugin on which host the mysql is running (if not localhost).<br />
Please contact me by eMail, if You are interested in these changes.</p>
<p>Berny</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Administrator</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-1227</link>
		<dc:creator>Administrator</dc:creator>
		<pubDate>Thu, 02 Aug 2007 13:25:51 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-1227</guid>
		<description>@ian: Thanks for your contribution.

I've indeed overlooked the fact that the client might be unable to connect to the server and handle it apropriately. I've included your code into my little script and uploaded it to the webserver, so that the above link contains the newest version.</description>
		<content:encoded><![CDATA[<p>@ian: Thanks for your contribution.</p>
<p>I&#8217;ve indeed overlooked the fact that the client might be unable to connect to the server and handle it apropriately. I&#8217;ve included your code into my little script and uploaded it to the webserver, so that the above link contains the newest version.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ian</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-1225</link>
		<dc:creator>ian</dc:creator>
		<pubDate>Thu, 02 Aug 2007 12:46:57 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-1225</guid>
		<description>Nice work.  One thing, though:


$ ./check_mysql_conn.pl 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Use of uninitialized value in concatenation (.) or string at ./check_mysql_conn.pl line 50.
Use of uninitialized value in numeric gt (&#62;) at ./check_mysql_conn.pl line 52.
Use of uninitialized value in numeric gt (&#62;) at ./check_mysql_conn.pl line 52.
Use of uninitialized value in concatenation (.) or string at ./check_mysql_conn.pl line 59.
OK:  active connections
$ echo $?
0


(Obviously there's no SQL server running on that box, but the plugin still returns OK.

Inability to connect should cause the plugin to return unknown or critical or warning.. something other than OK :).

The problem is that on line 48, when you capture the \d+ from the line of output, there is no check to make sure that the regex matched and $1 actually exists.  Something like this might help:


unless ($query_output =~ /^Threads_connected\s+(\d+)\s+$/) {
    print "Unable to read output from MySQL\n";
    exit $ERRORS{'CRITICAL'};
}
my $threads_connected = $1;



$ ./check_mysqlconnections 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Unable to read output from MySQL
$ echo $?
</description>
		<content:encoded><![CDATA[<p>Nice work.  One thing, though:</p>
<p>$ ./check_mysql_conn.pl<br />
ERROR 2002 (HY000): Can&#8217;t connect to local MySQL server through socket &#8216;/var/run/mysqld/mysqld.sock&#8217; (2)<br />
Use of uninitialized value in concatenation (.) or string at ./check_mysql_conn.pl line 50.<br />
Use of uninitialized value in numeric gt (&gt;) at ./check_mysql_conn.pl line 52.<br />
Use of uninitialized value in numeric gt (&gt;) at ./check_mysql_conn.pl line 52.<br />
Use of uninitialized value in concatenation (.) or string at ./check_mysql_conn.pl line 59.<br />
OK:  active connections<br />
$ echo $?<br />
0</p>
<p>(Obviously there&#8217;s no SQL server running on that box, but the plugin still returns OK.</p>
<p>Inability to connect should cause the plugin to return unknown or critical or warning.. something other than OK :).</p>
<p>The problem is that on line 48, when you capture the \d+ from the line of output, there is no check to make sure that the regex matched and $1 actually exists.  Something like this might help:</p>
<p>unless ($query_output =~ /^Threads_connected\s+(\d+)\s+$/) {<br />
    print &#8220;Unable to read output from MySQL\n&#8221;;<br />
    exit $ERRORS{&#8217;CRITICAL&#8217;};<br />
}<br />
my $threads_connected = $1;</p>
<p>$ ./check_mysqlconnections<br />
ERROR 2002 (HY000): Can&#8217;t connect to local MySQL server through socket &#8216;/var/run/mysqld/mysqld.sock&#8217; (2)<br />
Unable to read output from MySQL<br />
$ echo $?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Administrator</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-536</link>
		<dc:creator>Administrator</dc:creator>
		<pubDate>Tue, 22 May 2007 12:14:09 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-536</guid>
		<description>@Nathan: The Problem is that there is a bug in WordPress which ruins the script quoted in "pre" tags -- thus certain characters are lost.

I've linked the script to my original post, so that you can download it. Should you make any improvements, I'd like to hear from you. Thanks!</description>
		<content:encoded><![CDATA[<p>@Nathan: The Problem is that there is a bug in WordPress which ruins the script quoted in &#8220;pre&#8221; tags &#8212; thus certain characters are lost.</p>
<p>I&#8217;ve linked the script to my original post, so that you can download it. Should you make any improvements, I&#8217;d like to hear from you. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nathan</title>
		<link>http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-477</link>
		<dc:creator>Nathan</dc:creator>
		<pubDate>Wed, 16 May 2007 17:28:53 +0000</pubDate>
		<guid isPermaLink="false">http://bergs.biz/blog/2007/05/08/monitor-number-of-active-connections-to-mysql-using-nagios/#comment-477</guid>
		<description>./check_mysql_query -H  -u nagios -p uCie9qua -q "show status like 'Threads_connected';"
QUERY CRITICAL: Is not a numeric - 'Threads_connected'

That's super close but I can't figure out the "not a numeric" part. 

Any ideas?</description>
		<content:encoded><![CDATA[<p>./check_mysql_query -H  -u nagios -p uCie9qua -q &#8220;show status like &#8216;Threads_connected&#8217;;&#8221;<br />
QUERY CRITICAL: Is not a numeric - &#8216;Threads_connected&#8217;</p>
<p>That&#8217;s super close but I can&#8217;t figure out the &#8220;not a numeric&#8221; part. </p>
<p>Any ideas?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
