root / plugins / mysql / mysql_connections_per_user @ 17f78427
Historique | Voir | Annoter | Télécharger (5,36 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_per_user /etc/munin/plugins/mysql_connections_per_user |
| 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 |
# Revision 3.0 2014/06/11 |
| 35 |
# Fix now showing if not connected by transilvlad@gmail.com |
| 36 |
# Other fixes and performance by transilvlad@gmail.com |
| 37 |
# |
| 38 |
# Parameters: |
| 39 |
# |
| 40 |
# config |
| 41 |
# autoconf |
| 42 |
# |
| 43 |
# Configuration variables |
| 44 |
# |
| 45 |
# mysqlopts - Options to pass to mysql |
| 46 |
# mysqlcli - Override location of mysql |
| 47 |
# numusers - Override maximum number of users to display |
| 48 |
# warning - Override default warning limit |
| 49 |
# critical - Override default critical limit |
| 50 |
# |
| 51 |
#%# family=auto |
| 52 |
#%# capabilities=autoconf |
| 53 |
|
| 54 |
use strict; |
| 55 |
|
| 56 |
# Define the mysqladmin paths, and commands |
| 57 |
my $MYSQLCLI = $ENV{mysqlcli} || "mysql";
|
| 58 |
my $TEST_COMMAND = "$MYSQLCLI $ENV{mysqlopts} -N -B -e \"SELECT NOW();\"";
|
| 59 |
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;\"";
|
| 60 |
my $warning = $ENV{warning} || "80";
|
| 61 |
my $critical = $ENV{critical} || "90";
|
| 62 |
my $numusers = $ENV{numusers} || 10;
|
| 63 |
my $numthreads = 0; |
| 64 |
|
| 65 |
# Pull in any arguments |
| 66 |
my $arg = shift(); |
| 67 |
|
| 68 |
# Check to see how the script was called |
| 69 |
if ($arg eq 'config') {
|
| 70 |
print_graph_information(); |
| 71 |
} elsif ($arg eq 'autoconf') {
|
| 72 |
if (test_service()) { print "yes\n"; }
|
| 73 |
else { print "no\n"; }
|
| 74 |
} else {
|
| 75 |
print_graph_data(); |
| 76 |
} |
| 77 |
exit; |
| 78 |
|
| 79 |
sub print_graph_data() {
|
| 80 |
# Define the values that are returned to munin |
| 81 |
|
| 82 |
# Return the values to Munin |
| 83 |
my $counts = count_thread_users(); |
| 84 |
my %counts = %{$counts};
|
| 85 |
|
| 86 |
sub valsort {
|
| 87 |
return $counts{$a} <=> $counts{$b};
|
| 88 |
} |
| 89 |
my $i = 0; |
| 90 |
my $total = 0; |
| 91 |
my $print_user = ""; |
| 92 |
foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
|
| 93 |
last if $i++ >= $numusers; |
| 94 |
if ($user eq "system user") {
|
| 95 |
#skip internal user that manages binlog operations for slave servers. |
| 96 |
next; |
| 97 |
} |
| 98 |
$total += $counts{$user};
|
| 99 |
$print_user = $user; |
| 100 |
if($print_user eq "root") {
|
| 101 |
$print_user = "root_"; |
| 102 |
} |
| 103 |
print "$print_user.value $counts{$user}\n";
|
| 104 |
} |
| 105 |
my $other = $numthreads - $total; |
| 106 |
if($other < 0) {
|
| 107 |
$other = 0; |
| 108 |
} |
| 109 |
print "other.value $other\n"; |
| 110 |
} |
| 111 |
|
| 112 |
sub print_graph_information {
|
| 113 |
print <<EOM; |
| 114 |
graph_title MySQL Connections per user |
| 115 |
graph_args --base 1000 --lower-limit 0 |
| 116 |
graph_vlabel Connections |
| 117 |
graph_info The number of current connexions per user. |
| 118 |
graph_category db |
| 119 |
graph_total Total |
| 120 |
EOM |
| 121 |
|
| 122 |
my $counts = count_thread_users(); |
| 123 |
my %counts = %{$counts};
|
| 124 |
my $stacked = 0; |
| 125 |
|
| 126 |
sub valsort {
|
| 127 |
return $counts{$a} <=> $counts{$b};
|
| 128 |
} |
| 129 |
my $i = 0; |
| 130 |
foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
|
| 131 |
last if $i++ >= $numusers; |
| 132 |
if ($user eq "system user") {
|
| 133 |
#skip internal user that manages binlog operations for slave servers. |
| 134 |
next; |
| 135 |
} |
| 136 |
my $print_user = $user; |
| 137 |
if($print_user eq "root") {
|
| 138 |
$print_user = "root_"; |
| 139 |
} |
| 140 |
print <<EOM; |
| 141 |
$print_user.label $user |
| 142 |
$print_user.info Number of connexions used by user $print_user |
| 143 |
EOM |
| 144 |
print "$print_user.draw "; |
| 145 |
|
| 146 |
# if we already printed an entry, make the next ones stacked |
| 147 |
if ($i > 1) {
|
| 148 |
print "STACK\n"; |
| 149 |
} |
| 150 |
else {
|
| 151 |
print "AREA\n"; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
print <<EOM; |
| 156 |
other.label Others |
| 157 |
other.draw STACK |
| 158 |
other.info Other connected threads not in the top $numusers |
| 159 |
EOM |
| 160 |
} |
| 161 |
|
| 162 |
sub count_thread_users {
|
| 163 |
my %counts = (); |
| 164 |
open(SERVICE, "$MYSQL_QUERY |") |
| 165 |
or die("Could not execute '$MYSQL_QUERY': $!");
|
| 166 |
while (<SERVICE>) {
|
| 167 |
my ($no, $user) = split "\t"; |
| 168 |
$user =~ s/^\s+|\s+$//g; |
| 169 |
$counts{$user} = $no;
|
| 170 |
$numthreads += $no; |
| 171 |
} |
| 172 |
return \%counts; |
| 173 |
} |
| 174 |
|
| 175 |
sub test_service {
|
| 176 |
my $return = 1; |
| 177 |
system ("$MYSQLCLI --version >/dev/null 2>/dev/null");
|
| 178 |
if ($? == 0) {
|
| 179 |
system ("$TEST_COMMAND >/dev/null 2>/dev/null");
|
| 180 |
if ($? == 0) {
|
| 181 |
print "yes\n"; |
| 182 |
$return = 0; |
| 183 |
} |
| 184 |
else {
|
| 185 |
print "no (could not connect to mysql)\n"; |
| 186 |
} |
| 187 |
} |
| 188 |
else {
|
| 189 |
print "no (mysql not found)\n"; |
| 190 |
} |
| 191 |
exit $return; |
| 192 |
} |
