2014-01-02 23:42:28 +01:00
#!/usr/bin/perl
#
# Copyright (C) 2008 Rackspace US, Inc. <http://www.rackspace.com>
#
# 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; version 2 dated June,
# 1991.
#
# 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/gpl.txt
#
#
# This plugin is based off of the Connection Usage
# section of the MySQL Connection Health Page
#
# http://dev.mysql.com/doc/administrator/en/mysql-administrator-health-connection-health.html
#
# To enable, link mysql_connections to this file. E.g.
#
2014-06-11 16:43:48 +02:00
# ln -s /usr/share/node/node/plugins/mysql_connections_per_user /etc/munin/plugins/mysql_connections_per_user
2014-01-02 23:42:28 +01:00
#
# Revision 1.0 2007/08/03
# Created by Justin Shepherd <galstrom21@gmail.com>
#
# Revision 2.0 2013/01/02
# Per user support by anarcat@koumbit.org
#
2014-06-11 16:43:48 +02:00
# Revision 3.0 2014/06/11
# Fix now showing if not connected by transilvlad@gmail.com
# Other fixes and performance by transilvlad@gmail.com
#
2014-01-02 23:42:28 +01:00
# Parameters:
#
# config
# autoconf
#
# Configuration variables
#
# mysqlopts - Options to pass to mysql
2014-06-11 16:43:48 +02:00
# mysqlcli - Override location of mysql
2014-01-03 00:47:44 +01:00
# numusers - Override maximum number of users to display
2014-06-11 16:43:48 +02:00
# warning - Override default warning limit
# critical - Override default critical limit
2014-01-02 23:42:28 +01:00
#
#%# family=auto
#%# capabilities=autoconf
use strict;
# Define the mysqladmin paths, and commands
2014-06-11 16:43:48 +02:00
my $MYSQLCLI = $ENV{mysqlcli} || "mysql";
my $TEST_COMMAND = "$MYSQLCLI $ENV{mysqlopts} -N -B -e \"SELECT NOW();\"";
my $MYSQL_QUERY = "$MYSQLCLI $ENV{mysqlopts} -N -B -e \"SELECT SUM(NO) AS NO, USER FROM (SELECT 1 AS NO, USER FROM INFORMATION_SCHEMA.PROCESSLIST UNION ALL SELECT DISTINCT 0 AS NO, User AS USER FROM mysql.user WHERE User != '') AS Q GROUP BY USER ORDER BY NO DESC;\"";
my $warning = $ENV{warning} || "80";
my $critical = $ENV{critical} || "90";
my $numusers = $ENV{numusers} || 10;
my $numthreads = 0;
2014-01-02 23:42:28 +01:00
# Pull in any arguments
my $arg = shift();
# Check to see how the script was called
if ($arg eq 'config') {
print_graph_information();
} elsif ($arg eq 'autoconf') {
if (test_service()) { print "yes\n"; }
else { print "no\n"; }
} else {
2014-01-03 00:47:44 +01:00
print_graph_data();
}
2014-01-03 01:30:41 +01:00
exit;
2014-01-03 00:47:44 +01:00
sub print_graph_data() {
2014-06-11 16:43:48 +02:00
# Define the values that are returned to munin
2014-01-02 23:42:28 +01:00
2014-06-11 16:43:48 +02:00
# Return the values to Munin
my $counts = count_thread_users();
my %counts = %{$counts};
2014-01-03 00:47:44 +01:00
2014-06-11 16:43:48 +02:00
sub valsort {
return $counts{$a} <=> $counts{$b};
}
my $i = 0;
my $total = 0;
my $print_user = "";
foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
last if $i++ >= $numusers;
$total += $counts{$user};
$print_user = $user;
if($print_user eq "root") {
$print_user = "root_";
2014-01-02 23:42:28 +01:00
}
2014-06-11 16:43:48 +02:00
print "$print_user.value $counts{$user}\n";
}
my $other = $numthreads - $total;
if($other < 0) {
$other = 0;
}
print "other.value $other\n";
2014-01-02 23:42:28 +01:00
}
sub print_graph_information {
2014-06-11 16:43:48 +02:00
print <<EOM;
2014-01-03 01:17:09 +01:00
graph_title MySQL Connections per user
2014-01-02 23:42:28 +01:00
graph_args --base 1000 --lower-limit 0
graph_vlabel Connections
graph_info The number of current connexions per user.
graph_category mysql
graph_total Total
EOM
2014-01-03 01:08:10 +01:00
2014-06-11 16:43:48 +02:00
my $counts = count_thread_users();
my %counts = %{$counts};
my $stacked = 0;
2014-01-03 01:08:10 +01:00
2014-06-11 16:43:48 +02:00
sub valsort {
return $counts{$a} <=> $counts{$b};
2014-01-03 01:08:10 +01:00
}
2014-06-11 16:43:48 +02:00
my $i = 0;
foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
last if $i++ >= $numusers;
my $print_user = $user;
if($print_user eq "root") {
$print_user = "root_";
}
print <<EOM;
$print_user.label $user
$print_user.info Number of connexions used by user $print_user
EOM
print "$print_user.draw ";
# if we already printed an entry, make the next ones stacked
if ($i > 1) {
print "STACK\n";
}
else {
print "AREA\n";
}
2014-01-02 23:42:28 +01:00
}
2014-01-03 01:08:10 +01:00
2014-06-11 16:43:48 +02:00
print <<EOM;
2014-01-03 01:17:09 +01:00
other.label Others
other.draw STACK
other.info Other connected threads not in the top $numusers
2014-01-03 00:52:58 +01:00
EOM
2014-01-02 23:42:28 +01:00
}
2014-01-03 01:08:10 +01:00
sub count_thread_users {
my %counts = ();
2014-06-11 16:43:48 +02:00
open(SERVICE, "$MYSQL_QUERY |")
or die("Could not execute '$MYSQL_QUERY': $!");
2014-01-02 23:42:28 +01:00
while (<SERVICE>) {
2014-06-11 16:43:48 +02:00
my ($no, $user) = split "\t";
$user =~ s/^\s+|\s+$//g;
$counts{$user} = $no;
$numthreads += $no;
2014-01-02 23:42:28 +01:00
}
2014-01-03 01:08:10 +01:00
return \%counts;
2014-01-02 23:42:28 +01:00
}
sub test_service {
my $return = 1;
2014-06-11 16:43:48 +02:00
system ("$MYSQLCLI --version >/dev/null 2>/dev/null");
if ($? == 0) {
system ("$TEST_COMMAND >/dev/null 2>/dev/null");
if ($? == 0) {
print "yes\n";
$return = 0;
}
else {
print "no (could not connect to mysql)\n";
}
2014-01-02 23:42:28 +01:00
}
2014-06-11 16:43:48 +02:00
else {
print "no (mysql not found)\n";
2014-01-02 23:42:28 +01:00
}
exit $return;
}