Projet

Général

Profil

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

root / plugins / mysql / mysql_connections_per_user @ 0866999f

Historique | Voir | Annoter | Télécharger (5,08 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
        $total += $counts{$user};
95
        $print_user = $user;
96
        if($print_user eq "root") {
97
            $print_user = "root_";
98
        }
99
        print "$print_user.value $counts{$user}\n";
100
    }
101
    my $other = $numthreads - $total;
102
    if($other < 0) {
103
        $other = 0;
104
    }
105
    print "other.value $other\n";
106
}
107

    
108
sub print_graph_information {
109
    print <<EOM;
110
graph_title MySQL Connections per user
111
graph_args --base 1000 --lower-limit 0
112
graph_vlabel Connections
113
graph_info The number of current connexions per user.
114
graph_category mysql
115
graph_total Total
116
EOM
117

    
118
    my $counts = count_thread_users();
119
    my %counts = %{$counts};
120
    my $stacked = 0;
121

    
122
    sub valsort {
123
        return $counts{$a} <=> $counts{$b};
124
    }
125
    my $i = 0;
126
    foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
127
        last if $i++ >= $numusers;
128
        my $print_user = $user;
129
        if($print_user eq "root") {
130
            $print_user = "root_";
131
        }
132
        print <<EOM;
133
$print_user.label $user
134
$print_user.info Number of connexions used by user $print_user
135
EOM
136
        print "$print_user.draw ";
137

    
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
    open(SERVICE, "$MYSQL_QUERY |")
157
        or die("Could not execute '$MYSQL_QUERY': $!");
158
    while (<SERVICE>) {
159
        my ($no, $user) = split "\t";
160
        $user =~ s/^\s+|\s+$//g;
161
        $counts{$user} = $no;
162
        $numthreads += $no;
163
    }
164
    return \%counts;
165
}
166

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