Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mysql / mysql_connections_per_user @ 6002cb63

Historique | Voir | Annoter | Télécharger (4,75 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

    
79
	# Return the values to Munin
80
        my $counts = count_thread_users();
81
        my %counts = %{$counts};
82

    
83
        sub valsort {
84
            return $counts{$a} <=> $counts{$b};
85
        }
86
        my $i = 0;
87
        my $total = 0;
88
        foreach my $user (sort valsort keys(%counts)) {
89
            last if $i++ >= $numusers;
90
            $total += $counts{$user};
91
            print "$user.value $counts{$user}\n";
92
        }
93
        my $other = poll_variables($MYSQL_VARIABLES,"Threads_connected") - $total;
94
	print "other.value $other\n";
95
}
96

    
97
sub poll_variables {
98
	my $command = shift;
99
	my $expression = shift;
100
	my $ret = 0;
101
	open(SERVICE, "$command |")
102
  		or die("Coult not execute '$command': $!");
103
	while (<SERVICE>) {
104
            my ($field, $value) = (m/(\w+).*?(\d+(?:\.\d+)?)/);
105
	    next unless ($field);
106
	    if ($field eq $expression ) {
107
		$ret = "$value";
108
	    }
109
	}
110
	close(SERVICE);
111
	return $ret;
112
}
113

    
114

    
115
sub print_graph_information {
116
print <<EOM;
117
graph_title MySQL Connections per user
118
graph_args --base 1000 --lower-limit 0
119
graph_vlabel Connections
120
graph_info The number of current connexions per user.
121
graph_category mysql
122
graph_total Total
123
EOM
124

    
125
my $counts = count_thread_users();
126
my %counts = %{$counts};
127
my $stacked = 0;
128

    
129
sub valsort {
130
    return $counts{$a} <=> $counts{$b};
131
}
132
my $i = 0;
133
foreach my $user (sort valsort keys(%counts)) {
134
    last if $i++ >= $numusers;
135
    print <<EOM;
136
$user.label $user
137
$user.info Number of connexions used by user $user
138
EOM
139
print "$user.draw ";
140
    # if we already printed an entry, make the next ones stacked
141
    if ($i > 1) {
142
        print "STACK\n";
143
    }
144
    else {
145
        print "AREA\n";
146
    }
147
}
148

    
149
print <<EOM;
150
other.label Others
151
other.draw STACK
152
other.info Other connected threads not in the top $numusers
153
EOM
154
}
155

    
156
sub count_thread_users {
157
    my %counts = ();
158
    my $command = 'mysql -N -B -e "SHOW PROCESSLIST;"';
159
    open(SERVICE, "$command |")
160
        or die("Could not execute '$command': $!");
161
    while (<SERVICE>) {
162
        my ($threadid, $user) = split "\t";
163
        next unless ($threadid);
164
        $counts{$user} = 0 unless defined($counts{$user});
165
        $counts{$user}++;
166
    }
167
    return \%counts;
168
}
169

    
170
sub test_service {
171
    my $return = 1;
172
    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
173
    if ($? == 0)
174
    {
175
		system ("$TEST_COMMAND >/dev/null 2>/dev/null");
176
		if ($? == 0)
177
		{
178
		    print "yes\n";
179
		    $return = 0;
180
		}
181
		else
182
		{
183
	   		print "no (could not connect to mysql)\n";
184
		}
185
    }
186
    else
187
    {
188
		print "no (mysqladmin not found)\n";
189
    }
190
    exit $return;
191
}