Projet

Général

Profil

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

root / plugins / mysql / mysql_connections_per_user @ 95003946

Historique | Voir | Annoter | Télécharger (5,56 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
        if (  $print_user =~ /[^A-Za-z0-9]/ ) {
104
            $print_user =~ s/[^A-Za-z0-9]/_/;
105
        }
106
        print "$print_user.value $counts{$user}\n";
107
    }
108
    my $other = $numthreads - $total;
109
    if($other < 0) {
110
        $other = 0;
111
    }
112
    print "other.value $other\n";
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 db
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 (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
134
        last if $i++ >= $numusers;
135
        if ($user eq "system user") {
136
            #skip internal user that manages binlog operations for slave servers.
137
            next;
138
        }
139
        my $print_user = $user;
140
        if($print_user eq "root") {
141
            $print_user = "root_";
142
        }
143
        if (  $print_user =~ /[^A-Za-z0-9]/ ) {
144
            $print_user =~ s/[^A-Za-z0-9]/_/;
145
        }
146

    
147
        print <<EOM;
148
$print_user.label $user
149
$print_user.info Number of connexions used by user $user
150
EOM
151
        print "$print_user.draw ";
152

    
153
        # if we already printed an entry, make the next ones stacked
154
        if ($i > 1) {
155
            print "STACK\n";
156
        }
157
        else {
158
            print "AREA\n";
159
        }
160
    }
161

    
162
    print <<EOM;
163
other.label Others
164
other.draw STACK
165
other.info Other connected threads not in the top $numusers
166
EOM
167
}
168

    
169
sub count_thread_users {
170
    my %counts = ();
171
    open(SERVICE, "$MYSQL_QUERY |")
172
        or die("Could not execute '$MYSQL_QUERY': $!");
173
    while (<SERVICE>) {
174
        my ($no, $user) = split "\t";
175
        $user =~ s/^\s+|\s+$//g;
176
        $counts{$user} = $no;
177
        $numthreads += $no;
178
    }
179
    return \%counts;
180
}
181

    
182
sub test_service {
183
    my $return = 1;
184
    system ("$MYSQLCLI --version >/dev/null 2>/dev/null");
185
    if ($? == 0) {
186
        system ("$TEST_COMMAND >/dev/null 2>/dev/null");
187
        if ($? == 0) {
188
            print "yes\n";
189
            $return = 0;
190
        }
191
        else {
192
            print "no (could not connect to mysql)\n";
193
        }
194
    }
195
    else {
196
        print "no (mysql not found)\n";
197
    }
198
    exit $return;
199
}