From c223def9d5f0ed30e618377001a4b8cf45350b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 2 Jan 2014 17:42:28 -0500 Subject: [PATCH 1/7] fork the mysql_connections plugin to add per-user support --- plugins/mysql/mysql_connections_per_user | 192 +++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100755 plugins/mysql/mysql_connections_per_user diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user new file mode 100755 index 00000000..e1da7130 --- /dev/null +++ b/plugins/mysql/mysql_connections_per_user @@ -0,0 +1,192 @@ +#!/usr/bin/perl +# +# Copyright (C) 2008 Rackspace US, Inc. +# +# 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. +# +# ln -s /usr/share/node/node/plugins/mysql_connections /etc/munin/plugins/mysql_connections +# +# Revision 1.0 2007/08/03 +# Created by Justin Shepherd +# +# Revision 2.0 2013/01/02 +# Per user support by anarcat@koumbit.org +# +# Parameters: +# +# config +# autoconf +# +# Configuration variables +# +# mysqlopts - Options to pass to mysql +# mysqladmin - Override location of mysqladmin +# warning - Override default warning limit +# critical - Override default critical limit +# +#%# family=auto +#%# capabilities=autoconf + +use strict; + +# Define the mysqladmin paths, and commands +my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin"; +my $TEST_COMMAND = "$MYSQLADMIN $ENV{mysqlopts} processlist"; +my $MYSQL_VARIABLES = "$MYSQLADMIN $ENV{mysqlopts} extended-status variables"; +my $warning = $ENV{warning} || "80"; +my $critical = $ENV{critical} || "90"; + +# Pull in any arguments +my $arg = shift(); + +# Check to see how the script was called +if ($arg eq 'config') { + print_graph_information(); + exit(); +} elsif ($arg eq 'autoconf') { + if (test_service()) { print "yes\n"; } + else { print "no\n"; } + exit; +} else { + # Define the values that are returned to munin + my ($current, $upper_limit) = (0,0,0); + + # Gather the values from mysqladmin + $current = poll_variables($MYSQL_VARIABLES,"Threads_connected"); + $upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections"); + + # Return the values to Munin + print "current.value $current\n"; + print "limit.value $upper_limit\n"; + + my $threads = get_thread_list(); + my %counts = (); + my ($thread, $user, $count); + while (($thread, $user) = each %$threads) { + $counts{$user} = 0 unless defined($counts{$user}); + $counts{$user} += 1; + } + while (($user, $count) = each %counts) { + print "$user.value $count\n"; + } +} + + +sub poll_variables { + my $command = shift; + my $expression = shift; + my $ret = 0; + open(SERVICE, "$command |") + or die("Coult not execute '$command': $!"); + while () { + my ($field, $value) = (m/(\w+).*?(\d+(?:\.\d+)?)/); + next unless ($field); + if ($field eq $expression ) { + $ret = "$value"; + } + } + close(SERVICE); + return $ret; +} + + +sub print_graph_information { +print <) { + my ($threadid, $user) = split "\t"; + next unless ($threadid); + $threads{$threadid} = $user; + } + return \%threads; +} + + +sub test_service { + my $return = 1; + system ("$MYSQLADMIN --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"; + } + } + else + { + print "no (mysqladmin not found)\n"; + } + exit $return; +} From c2214999c390777fd3a3533e8c9d09bc1e7838dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 2 Jan 2014 18:47:44 -0500 Subject: [PATCH 2/7] limit to ten the number of users by default --- plugins/mysql/mysql_connections_per_user | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index e1da7130..ee000707 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -40,6 +40,7 @@ # # mysqlopts - Options to pass to mysql # mysqladmin - Override location of mysqladmin +# numusers - Override maximum number of users to display # warning - Override default warning limit # critical - Override default critical limit # @@ -54,6 +55,7 @@ my $TEST_COMMAND = "$MYSQLADMIN $ENV{mysqlopts} processlist"; my $MYSQL_VARIABLES = "$MYSQLADMIN $ENV{mysqlopts} extended-status variables"; my $warning = $ENV{warning} || "80"; my $critical = $ENV{critical} || "90"; +my $numusers = $ENV{numusers} || 10; # Pull in any arguments my $arg = shift(); @@ -67,6 +69,11 @@ if ($arg eq 'config') { else { print "no\n"; } exit; } else { + print_graph_data(); + exit; +} + +sub print_graph_data() { # Define the values that are returned to munin my ($current, $upper_limit) = (0,0,0); @@ -85,8 +92,14 @@ if ($arg eq 'config') { $counts{$user} = 0 unless defined($counts{$user}); $counts{$user} += 1; } - while (($user, $count) = each %counts) { - print "$user.value $count\n"; + + sub valsort { + return $$threads{$a} <=> $$threads{$b}; + } + my $i = 0; + foreach my $user (sort valsort keys(%counts)) { + last if $i++ >= $numusers; + print "$user.value $counts{$user}\n"; } } From ade148171755f0029313f3ed35333a80173e6c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 2 Jan 2014 18:47:52 -0500 Subject: [PATCH 3/7] cosmetic fixes --- plugins/mysql/mysql_connections_per_user | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index ee000707..a3ac5374 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -88,9 +88,10 @@ sub print_graph_data() { my $threads = get_thread_list(); my %counts = (); my ($thread, $user, $count); + while (($thread, $user) = each %$threads) { $counts{$user} = 0 unless defined($counts{$user}); - $counts{$user} += 1; + $counts{$user}++; } sub valsort { @@ -103,7 +104,6 @@ sub print_graph_data() { } } - sub poll_variables { my $command = shift; my $expression = shift; From 88ba6e86bea88873841605295d04b2eba233e801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 2 Jan 2014 18:52:58 -0500 Subject: [PATCH 4/7] reorder graph to, hopefully, fix stacking --- plugins/mysql/mysql_connections_per_user | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index a3ac5374..9ec479da 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -82,9 +82,6 @@ sub print_graph_data() { $upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections"); # Return the values to Munin - print "current.value $current\n"; - print "limit.value $upper_limit\n"; - my $threads = get_thread_list(); my %counts = (); my ($thread, $user, $count); @@ -102,6 +99,8 @@ sub print_graph_data() { last if $i++ >= $numusers; print "$user.value $counts{$user}\n"; } + print "current.value $current\n"; + print "limit.value $upper_limit\n"; } sub poll_variables { @@ -130,14 +129,6 @@ graph_vlabel Connections graph_info The number of current connexions per user. graph_category mysql graph_total Total -current.label In Use -current.draw LINE1 -current.info The number of current threads connected -current.warning $warning -current.critical $critical -limit.label Maximum -limit.draw LINE1 -limit.info The current value of the "max_connections" variable EOM my $threads = get_thread_list(); my %seen = (); @@ -148,7 +139,6 @@ while (($thread, $user) = each %$threads) { next; } else { - $seen{$user} = 1; print < Date: Thu, 2 Jan 2014 19:08:10 -0500 Subject: [PATCH 5/7] limit and sort the config entries the same way as we sort values, based on values --- plugins/mysql/mysql_connections_per_user | 62 +++++++++++------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index 9ec479da..d0c8f98e 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -82,17 +82,11 @@ sub print_graph_data() { $upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections"); # Return the values to Munin - my $threads = get_thread_list(); - my %counts = (); - my ($thread, $user, $count); - - while (($thread, $user) = each %$threads) { - $counts{$user} = 0 unless defined($counts{$user}); - $counts{$user}++; - } + my $counts = count_thread_users(); + my %counts = %{$counts}; sub valsort { - return $$threads{$a} <=> $$threads{$b}; + return $counts{$a} <=> $counts{$b}; } my $i = 0; foreach my $user (sort valsort keys(%counts)) { @@ -130,31 +124,31 @@ graph_info The number of current connexions per user. graph_category mysql graph_total Total EOM -my $threads = get_thread_list(); -my %seen = (); -my ($thread, $user); -while (($thread, $user) = each %$threads) { - # display user only once - if (defined($seen{$user})) { - next; - } - else { - print < $counts{$b}; +} +my $i = 0; +foreach my $user (sort valsort keys(%counts)) { + last if $i++ >= $numusers; + print < 1) { + print "STACK\n"; + } + else { + print "AREA\n"; } - } + print <) { my ($threadid, $user) = split "\t"; next unless ($threadid); - $threads{$threadid} = $user; + $counts{$user} = 0 unless defined($counts{$user}); + $counts{$user}++; } - return \%threads; + return \%counts; } - sub test_service { my $return = 1; system ("$MYSQLADMIN --version >/dev/null 2>/dev/null"); From 6002cb63ebc8111b2d2fe4885cc82dbb1986c5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 2 Jan 2014 19:17:09 -0500 Subject: [PATCH 6/7] overhaul graph display: remove total, make "current" the "limit" i did this because I can't figure out how to *not* stack the lines after the areas. furthermore, removing the limit is essential to have a proper resolution in the display. --- plugins/mysql/mysql_connections_per_user | 26 ++++++++---------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index d0c8f98e..cc7bfbd0 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -75,11 +75,6 @@ if ($arg eq 'config') { sub print_graph_data() { # Define the values that are returned to munin - my ($current, $upper_limit) = (0,0,0); - - # Gather the values from mysqladmin - $current = poll_variables($MYSQL_VARIABLES,"Threads_connected"); - $upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections"); # Return the values to Munin my $counts = count_thread_users(); @@ -89,12 +84,14 @@ sub print_graph_data() { return $counts{$a} <=> $counts{$b}; } my $i = 0; + my $total = 0; foreach my $user (sort valsort keys(%counts)) { last if $i++ >= $numusers; + $total += $counts{$user}; print "$user.value $counts{$user}\n"; } - print "current.value $current\n"; - print "limit.value $upper_limit\n"; + my $other = poll_variables($MYSQL_VARIABLES,"Threads_connected") - $total; + print "other.value $other\n"; } sub poll_variables { @@ -117,7 +114,7 @@ sub poll_variables { sub print_graph_information { print <= $numusers; print < Date: Thu, 2 Jan 2014 19:30:41 -0500 Subject: [PATCH 7/7] cosmetic --- plugins/mysql/mysql_connections_per_user | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index cc7bfbd0..334fe04f 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -63,15 +63,13 @@ my $arg = shift(); # Check to see how the script was called if ($arg eq 'config') { print_graph_information(); - exit(); } elsif ($arg eq 'autoconf') { if (test_service()) { print "yes\n"; } else { print "no\n"; } - exit; } else { print_graph_data(); - exit; } +exit; sub print_graph_data() { # Define the values that are returned to munin