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 c223def9 Antoine Beaupré
#!/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 0866999f Vlad Marian
#    ln -s /usr/share/node/node/plugins/mysql_connections_per_user /etc/munin/plugins/mysql_connections_per_user
27 c223def9 Antoine Beaupré
#
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 0866999f Vlad Marian
# 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 c223def9 Antoine Beaupré
# Parameters:
39
#
40
#   config
41
#   autoconf
42
#
43
# Configuration variables
44
#
45
#   mysqlopts     - Options to pass to mysql
46 0866999f Vlad Marian
#   mysqlcli      - Override location of mysql
47 c2214999 Antoine Beaupré
#   numusers      - Override maximum number of users to display
48 0866999f Vlad Marian
#   warning	      - Override default warning limit
49
#   critical	    - Override default critical limit 
50 c223def9 Antoine Beaupré
#
51
#%# family=auto
52
#%# capabilities=autoconf
53
54
use strict;
55
56
# Define the mysqladmin paths, and commands
57 0866999f Vlad Marian
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 c223def9 Antoine Beaupré
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 c2214999 Antoine Beaupré
    print_graph_data();
76
}
77 efc20c67 Antoine Beaupré
exit;
78 c2214999 Antoine Beaupré
79
sub print_graph_data() {
80 0866999f Vlad Marian
    # Define the values that are returned to munin
81 c223def9 Antoine Beaupré
82 0866999f Vlad Marian
    # Return the values to Munin
83
    my $counts = count_thread_users();
84
    my %counts = %{$counts};
85 c2214999 Antoine Beaupré
86 0866999f Vlad Marian
    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 c223def9 Antoine Beaupré
        }
99 0866999f Vlad Marian
        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 c223def9 Antoine Beaupré
}
107
108
sub print_graph_information {
109 0866999f Vlad Marian
    print <<EOM;
110 6002cb63 Antoine Beaupré
graph_title MySQL Connections per user
111 c223def9 Antoine Beaupré
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 02c5c4b6 Antoine Beaupré
118 0866999f Vlad Marian
    my $counts = count_thread_users();
119
    my %counts = %{$counts};
120
    my $stacked = 0;
121 02c5c4b6 Antoine Beaupré
122 0866999f Vlad Marian
    sub valsort {
123
        return $counts{$a} <=> $counts{$b};
124 02c5c4b6 Antoine Beaupré
    }
125 0866999f Vlad Marian
    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 c223def9 Antoine Beaupré
    }
146 02c5c4b6 Antoine Beaupré
147 0866999f Vlad Marian
    print <<EOM;
148 6002cb63 Antoine Beaupré
other.label Others
149
other.draw STACK
150
other.info Other connected threads not in the top $numusers
151 88ba6e86 Antoine Beaupré
EOM
152 c223def9 Antoine Beaupré
}
153
154 02c5c4b6 Antoine Beaupré
sub count_thread_users {
155
    my %counts = ();
156 0866999f Vlad Marian
    open(SERVICE, "$MYSQL_QUERY |")
157
        or die("Could not execute '$MYSQL_QUERY': $!");
158 c223def9 Antoine Beaupré
    while (<SERVICE>) {
159 0866999f Vlad Marian
        my ($no, $user) = split "\t";
160
        $user =~ s/^\s+|\s+$//g;
161
        $counts{$user} = $no;
162
        $numthreads += $no;
163 c223def9 Antoine Beaupré
    }
164 02c5c4b6 Antoine Beaupré
    return \%counts;
165 c223def9 Antoine Beaupré
}
166
167
sub test_service {
168
    my $return = 1;
169 0866999f Vlad Marian
    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 c223def9 Antoine Beaupré
    }
180 0866999f Vlad Marian
    else {
181
        print "no (mysql not found)\n";
182 c223def9 Antoine Beaupré
    }
183
    exit $return;
184
}