1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 02:51:03 +00:00

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.
This commit is contained in:
Vlad Marian 2014-06-11 15:43:48 +01:00
parent 624c826a27
commit 0866999f9d

View file

@ -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 <galstrom21@gmail.com>
@ -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,7 +43,7 @@
# 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
@ -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 $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();
@ -83,33 +88,23 @@ sub print_graph_data() {
}
my $i = 0;
my $total = 0;
foreach my $user (sort valsort keys(%counts)) {
my $print_user = "";
foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
last if $i++ >= $numusers;
$total += $counts{$user};
print "$user.value $counts{$user}\n";
$print_user = $user;
if($print_user eq "root") {
$print_user = "root_";
}
print "$print_user.value $counts{$user}\n";
}
my $other = $numthreads - $total;
if($other < 0) {
$other = 0;
}
my $other = poll_variables($MYSQL_VARIABLES,"Threads_connected") - $total;
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 (<SERVICE>) {
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 <<EOM;
graph_title MySQL Connections per user
@ -128,13 +123,18 @@ sub valsort {
return $counts{$a} <=> $counts{$b};
}
my $i = 0;
foreach my $user (sort valsort keys(%counts)) {
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;
$user.label $user
$user.info Number of connexions used by user $user
$print_user.label $user
$print_user.info Number of connexions used by user $print_user
EOM
print "$user.draw ";
print "$print_user.draw ";
# if we already printed an entry, make the next ones stacked
if ($i > 1) {
print "STACK\n";
@ -153,37 +153,32 @@ EOM
sub count_thread_users {
my %counts = ();
my $command = 'mysql -N -B -e "SHOW PROCESSLIST;"';
open(SERVICE, "$command |")
or die("Could not execute '$command': $!");
open(SERVICE, "$MYSQL_QUERY |")
or die("Could not execute '$MYSQL_QUERY': $!");
while (<SERVICE>) {
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 ("$MYSQLCLI --version >/dev/null 2>/dev/null");
if ($? == 0) {
system ("$TEST_COMMAND >/dev/null 2>/dev/null");
if ($? == 0)
{
if ($? == 0) {
print "yes\n";
$return = 0;
}
else
{
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;
}