From 0866999f9d2a0780081984230e89683ef8e1cb37 Mon Sep 17 00:00:00 2001 From: Vlad Marian Date: Wed, 11 Jun 2014 15:43:48 +0100 Subject: [PATCH] Make it usable Fixed the display issue for formerly active users that wore no longer showing once they closed all connections. Also made other fixes like the negative others and improved performance. It could still be improved but for now it should do. --- plugins/mysql/mysql_connections_per_user | 173 +++++++++++------------ 1 file changed, 84 insertions(+), 89 deletions(-) diff --git a/plugins/mysql/mysql_connections_per_user b/plugins/mysql/mysql_connections_per_user index 334fe04f..9cc50908 100755 --- a/plugins/mysql/mysql_connections_per_user +++ b/plugins/mysql/mysql_connections_per_user @@ -23,7 +23,7 @@ # # To enable, link mysql_connections to this file. E.g. # -# ln -s /usr/share/node/node/plugins/mysql_connections /etc/munin/plugins/mysql_connections +# ln -s /usr/share/node/node/plugins/mysql_connections_per_user /etc/munin/plugins/mysql_connections_per_user # # Revision 1.0 2007/08/03 # Created by Justin Shepherd @@ -31,6 +31,10 @@ # Revision 2.0 2013/01/02 # Per user support by anarcat@koumbit.org # +# Revision 3.0 2014/06/11 +# Fix now showing if not connected by transilvlad@gmail.com +# Other fixes and performance by transilvlad@gmail.com +# # Parameters: # # config @@ -39,10 +43,10 @@ # Configuration variables # # mysqlopts - Options to pass to mysql -# mysqladmin - Override location of mysqladmin +# mysqlcli - Override location of mysql # numusers - Override maximum number of users to display -# warning - Override default warning limit -# critical - Override default critical limit +# warning - Override default warning limit +# critical - Override default critical limit # #%# family=auto #%# capabilities=autoconf @@ -50,12 +54,13 @@ 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"; -my $numusers = $ENV{numusers} || 10; +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; # Pull in any arguments my $arg = shift(); @@ -72,46 +77,36 @@ if ($arg eq 'config') { exit; sub print_graph_data() { - # Define the values that are returned to munin + # Define the values that are returned to munin - # Return the values to Munin - my $counts = count_thread_users(); - my %counts = %{$counts}; + # Return the values to Munin + my $counts = count_thread_users(); + my %counts = %{$counts}; - sub valsort { - return $counts{$a} <=> $counts{$b}; + 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_"; } - 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"; - } - my $other = poll_variables($MYSQL_VARIABLES,"Threads_connected") - $total; - print "other.value $other\n"; + print "$print_user.value $counts{$user}\n"; + } + my $other = $numthreads - $total; + if($other < 0) { + $other = 0; + } + print "other.value $other\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 < $counts{$b}; -} -my $i = 0; -foreach my $user (sort valsort keys(%counts)) { - last if $i++ >= $numusers; - print < $counts{$b}; + } + 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 < 1) { - print "STACK\n"; - } - else { - print "AREA\n"; - } -} + print "$print_user.draw "; -print < 1) { + print "STACK\n"; + } + else { + print "AREA\n"; + } + } + + print <) { - my ($threadid, $user) = split "\t"; - next unless ($threadid); - $counts{$user} = 0 unless defined($counts{$user}); - $counts{$user}++; + my ($no, $user) = split "\t"; + $user =~ s/^\s+|\s+$//g; + $counts{$user} = $no; + $numthreads += $no; } return \%counts; } 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"; - } + 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"; + } } - else - { - print "no (mysqladmin not found)\n"; + else { + print "no (mysql not found)\n"; } exit $return; }