Projet

Général

Profil

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

root / plugins / mysql / mysql_connections_per_user @ efc20c67

Historique | Voir | Annoter | Télécharger (4,72 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
} elsif ($arg eq 'autoconf') {
67
    if (test_service()) { print "yes\n"; }
68
    else { print "no\n"; }
69
} else {
70
    print_graph_data();
71
}
72
exit;
73

    
74
sub print_graph_data() {
75
	# Define the values that are returned to munin
76

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

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

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

    
112

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

    
123
my $counts = count_thread_users();
124
my %counts = %{$counts};
125
my $stacked = 0;
126

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

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

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

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