root / plugins / mysql / mysql_connections_per_user @ 88ba6e86
Historique | Voir | Annoter | Télécharger (5,21 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
# |
| 3 |
# Copyright (C) 2008 Rackspace US, Inc. <http://www.rackspace.com> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License |
| 7 |
# as published by the Free Software Foundation; version 2 dated June, |
| 8 |
# 1991. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, see http://www.gnu.org/licenses/gpl.txt |
| 17 |
# |
| 18 |
# |
| 19 |
# This plugin is based off of the Connection Usage |
| 20 |
# section of the MySQL Connection Health Page |
| 21 |
# |
| 22 |
# http://dev.mysql.com/doc/administrator/en/mysql-administrator-health-connection-health.html |
| 23 |
# |
| 24 |
# To enable, link mysql_connections to this file. E.g. |
| 25 |
# |
| 26 |
# ln -s /usr/share/node/node/plugins/mysql_connections /etc/munin/plugins/mysql_connections |
| 27 |
# |
| 28 |
# Revision 1.0 2007/08/03 |
| 29 |
# Created by Justin Shepherd <galstrom21@gmail.com> |
| 30 |
# |
| 31 |
# Revision 2.0 2013/01/02 |
| 32 |
# Per user support by anarcat@koumbit.org |
| 33 |
# |
| 34 |
# Parameters: |
| 35 |
# |
| 36 |
# config |
| 37 |
# autoconf |
| 38 |
# |
| 39 |
# Configuration variables |
| 40 |
# |
| 41 |
# mysqlopts - Options to pass to mysql |
| 42 |
# mysqladmin - Override location of mysqladmin |
| 43 |
# numusers - Override maximum number of users to display |
| 44 |
# warning - Override default warning limit |
| 45 |
# critical - Override default critical limit |
| 46 |
# |
| 47 |
#%# family=auto |
| 48 |
#%# capabilities=autoconf |
| 49 |
|
| 50 |
use strict; |
| 51 |
|
| 52 |
# Define the mysqladmin paths, and commands |
| 53 |
my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin";
|
| 54 |
my $TEST_COMMAND = "$MYSQLADMIN $ENV{mysqlopts} processlist";
|
| 55 |
my $MYSQL_VARIABLES = "$MYSQLADMIN $ENV{mysqlopts} extended-status variables";
|
| 56 |
my $warning = $ENV{warning} || "80";
|
| 57 |
my $critical = $ENV{critical} || "90";
|
| 58 |
my $numusers = $ENV{numusers} || 10;
|
| 59 |
|
| 60 |
# Pull in any arguments |
| 61 |
my $arg = shift(); |
| 62 |
|
| 63 |
# Check to see how the script was called |
| 64 |
if ($arg eq 'config') {
|
| 65 |
print_graph_information(); |
| 66 |
exit(); |
| 67 |
} elsif ($arg eq 'autoconf') {
|
| 68 |
if (test_service()) { print "yes\n"; }
|
| 69 |
else { print "no\n"; }
|
| 70 |
exit; |
| 71 |
} else {
|
| 72 |
print_graph_data(); |
| 73 |
exit; |
| 74 |
} |
| 75 |
|
| 76 |
sub print_graph_data() {
|
| 77 |
# Define the values that are returned to munin |
| 78 |
my ($current, $upper_limit) = (0,0,0); |
| 79 |
|
| 80 |
# Gather the values from mysqladmin |
| 81 |
$current = poll_variables($MYSQL_VARIABLES,"Threads_connected"); |
| 82 |
$upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections"); |
| 83 |
|
| 84 |
# Return the values to Munin |
| 85 |
my $threads = get_thread_list(); |
| 86 |
my %counts = (); |
| 87 |
my ($thread, $user, $count); |
| 88 |
|
| 89 |
while (($thread, $user) = each %$threads) {
|
| 90 |
$counts{$user} = 0 unless defined($counts{$user});
|
| 91 |
$counts{$user}++;
|
| 92 |
} |
| 93 |
|
| 94 |
sub valsort {
|
| 95 |
return $$threads{$a} <=> $$threads{$b};
|
| 96 |
} |
| 97 |
my $i = 0; |
| 98 |
foreach my $user (sort valsort keys(%counts)) {
|
| 99 |
last if $i++ >= $numusers; |
| 100 |
print "$user.value $counts{$user}\n";
|
| 101 |
} |
| 102 |
print "current.value $current\n"; |
| 103 |
print "limit.value $upper_limit\n"; |
| 104 |
} |
| 105 |
|
| 106 |
sub poll_variables {
|
| 107 |
my $command = shift; |
| 108 |
my $expression = shift; |
| 109 |
my $ret = 0; |
| 110 |
open(SERVICE, "$command |") |
| 111 |
or die("Coult not execute '$command': $!");
|
| 112 |
while (<SERVICE>) {
|
| 113 |
my ($field, $value) = (m/(\w+).*?(\d+(?:\.\d+)?)/); |
| 114 |
next unless ($field); |
| 115 |
if ($field eq $expression ) {
|
| 116 |
$ret = "$value"; |
| 117 |
} |
| 118 |
} |
| 119 |
close(SERVICE); |
| 120 |
return $ret; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
sub print_graph_information {
|
| 125 |
print <<EOM; |
| 126 |
graph_title MySQL Connections |
| 127 |
graph_args --base 1000 --lower-limit 0 |
| 128 |
graph_vlabel Connections |
| 129 |
graph_info The number of current connexions per user. |
| 130 |
graph_category mysql |
| 131 |
graph_total Total |
| 132 |
EOM |
| 133 |
my $threads = get_thread_list(); |
| 134 |
my %seen = (); |
| 135 |
my ($thread, $user); |
| 136 |
while (($thread, $user) = each %$threads) {
|
| 137 |
# display user only once |
| 138 |
if (defined($seen{$user})) {
|
| 139 |
next; |
| 140 |
} |
| 141 |
else {
|
| 142 |
print <<EOM; |
| 143 |
$user.label Connexions for user $user |
| 144 |
$user.info Number of connexions used by user $user |
| 145 |
EOM |
| 146 |
print "$user.draw "; |
| 147 |
# if we already printed an entry, make the next ones stacked |
| 148 |
if (scalar %seen) {
|
| 149 |
print "STACK\n"; |
| 150 |
} |
| 151 |
else {
|
| 152 |
print "AREA\n"; |
| 153 |
} |
| 154 |
$seen{$user} = 1;
|
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
print <<EOM; |
| 159 |
current.label In Use |
| 160 |
current.draw LINE1 |
| 161 |
current.info The number of current threads connected |
| 162 |
current.warning $warning |
| 163 |
current.critical $critical |
| 164 |
limit.label Maximum |
| 165 |
limit.draw LINE1 |
| 166 |
limit.info The current value of the "max_connections" variable |
| 167 |
EOM |
| 168 |
} |
| 169 |
|
| 170 |
sub get_thread_list {
|
| 171 |
my %threads = (); |
| 172 |
my $command = 'mysql -N -B -e "SHOW PROCESSLIST;"'; |
| 173 |
open(SERVICE, "$command |") |
| 174 |
or die("Could not execute '$command': $!");
|
| 175 |
while (<SERVICE>) {
|
| 176 |
my ($threadid, $user) = split "\t"; |
| 177 |
next unless ($threadid); |
| 178 |
$threads{$threadid} = $user;
|
| 179 |
} |
| 180 |
return \%threads; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
sub test_service {
|
| 185 |
my $return = 1; |
| 186 |
system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
|
| 187 |
if ($? == 0) |
| 188 |
{
|
| 189 |
system ("$TEST_COMMAND >/dev/null 2>/dev/null");
|
| 190 |
if ($? == 0) |
| 191 |
{
|
| 192 |
print "yes\n"; |
| 193 |
$return = 0; |
| 194 |
} |
| 195 |
else |
| 196 |
{
|
| 197 |
print "no (could not connect to mysql)\n"; |
| 198 |
} |
| 199 |
} |
| 200 |
else |
| 201 |
{
|
| 202 |
print "no (mysqladmin not found)\n"; |
| 203 |
} |
| 204 |
exit $return; |
| 205 |
} |
