root / plugins / mysql / mysql_connections_per_user @ 17f78427
Historique | Voir | Annoter | Télécharger (5,36 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 | 17f78427 | Lars Kruse | # This plugin is based off of the Connection Usage |
| 20 | c223def9 | Antoine Beaupré | # section of the MySQL Connection Health Page |
| 21 | 17f78427 | Lars Kruse | # |
| 22 | c223def9 | Antoine Beaupré | # 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 | 17f78427 | Lars Kruse | # 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 | 17f78427 | Lars Kruse | # Check to see how the script was called |
| 69 | c223def9 | Antoine Beaupré | 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 | 78a229dd | Gabriel Filion | if ($user eq "system user") {
|
| 95 | #skip internal user that manages binlog operations for slave servers. |
||
| 96 | next; |
||
| 97 | } |
||
| 98 | 0866999f | Vlad Marian | $total += $counts{$user};
|
| 99 | $print_user = $user; |
||
| 100 | if($print_user eq "root") {
|
||
| 101 | $print_user = "root_"; |
||
| 102 | c223def9 | Antoine Beaupré | } |
| 103 | 0866999f | Vlad Marian | print "$print_user.value $counts{$user}\n";
|
| 104 | } |
||
| 105 | my $other = $numthreads - $total; |
||
| 106 | if($other < 0) {
|
||
| 107 | $other = 0; |
||
| 108 | } |
||
| 109 | print "other.value $other\n"; |
||
| 110 | c223def9 | Antoine Beaupré | } |
| 111 | |||
| 112 | sub print_graph_information {
|
||
| 113 | 0866999f | Vlad Marian | print <<EOM; |
| 114 | 6002cb63 | Antoine Beaupré | graph_title MySQL Connections per user |
| 115 | c223def9 | Antoine Beaupré | graph_args --base 1000 --lower-limit 0 |
| 116 | graph_vlabel Connections |
||
| 117 | graph_info The number of current connexions per user. |
||
| 118 | 8af93fce | dipohl | graph_category db |
| 119 | c223def9 | Antoine Beaupré | graph_total Total |
| 120 | EOM |
||
| 121 | 02c5c4b6 | Antoine Beaupré | |
| 122 | 0866999f | Vlad Marian | my $counts = count_thread_users(); |
| 123 | my %counts = %{$counts};
|
||
| 124 | my $stacked = 0; |
||
| 125 | 02c5c4b6 | Antoine Beaupré | |
| 126 | 0866999f | Vlad Marian | sub valsort {
|
| 127 | return $counts{$a} <=> $counts{$b};
|
||
| 128 | 02c5c4b6 | Antoine Beaupré | } |
| 129 | 0866999f | Vlad Marian | my $i = 0; |
| 130 | foreach my $user (reverse sort { $counts{$a} <=> $counts{$b} } keys %counts) {
|
||
| 131 | last if $i++ >= $numusers; |
||
| 132 | 78a229dd | Gabriel Filion | if ($user eq "system user") {
|
| 133 | #skip internal user that manages binlog operations for slave servers. |
||
| 134 | next; |
||
| 135 | } |
||
| 136 | 0866999f | Vlad Marian | my $print_user = $user; |
| 137 | if($print_user eq "root") {
|
||
| 138 | $print_user = "root_"; |
||
| 139 | } |
||
| 140 | print <<EOM; |
||
| 141 | $print_user.label $user |
||
| 142 | $print_user.info Number of connexions used by user $print_user |
||
| 143 | EOM |
||
| 144 | print "$print_user.draw "; |
||
| 145 | |||
| 146 | # if we already printed an entry, make the next ones stacked |
||
| 147 | if ($i > 1) {
|
||
| 148 | print "STACK\n"; |
||
| 149 | } |
||
| 150 | else {
|
||
| 151 | print "AREA\n"; |
||
| 152 | } |
||
| 153 | c223def9 | Antoine Beaupré | } |
| 154 | 02c5c4b6 | Antoine Beaupré | |
| 155 | 0866999f | Vlad Marian | print <<EOM; |
| 156 | 6002cb63 | Antoine Beaupré | other.label Others |
| 157 | other.draw STACK |
||
| 158 | other.info Other connected threads not in the top $numusers |
||
| 159 | 88ba6e86 | Antoine Beaupré | EOM |
| 160 | c223def9 | Antoine Beaupré | } |
| 161 | |||
| 162 | 02c5c4b6 | Antoine Beaupré | sub count_thread_users {
|
| 163 | my %counts = (); |
||
| 164 | 0866999f | Vlad Marian | open(SERVICE, "$MYSQL_QUERY |") |
| 165 | or die("Could not execute '$MYSQL_QUERY': $!");
|
||
| 166 | c223def9 | Antoine Beaupré | while (<SERVICE>) {
|
| 167 | 0866999f | Vlad Marian | my ($no, $user) = split "\t"; |
| 168 | $user =~ s/^\s+|\s+$//g; |
||
| 169 | $counts{$user} = $no;
|
||
| 170 | $numthreads += $no; |
||
| 171 | c223def9 | Antoine Beaupré | } |
| 172 | 02c5c4b6 | Antoine Beaupré | return \%counts; |
| 173 | c223def9 | Antoine Beaupré | } |
| 174 | |||
| 175 | sub test_service {
|
||
| 176 | my $return = 1; |
||
| 177 | 0866999f | Vlad Marian | system ("$MYSQLCLI --version >/dev/null 2>/dev/null");
|
| 178 | if ($? == 0) {
|
||
| 179 | system ("$TEST_COMMAND >/dev/null 2>/dev/null");
|
||
| 180 | if ($? == 0) {
|
||
| 181 | print "yes\n"; |
||
| 182 | $return = 0; |
||
| 183 | } |
||
| 184 | else {
|
||
| 185 | print "no (could not connect to mysql)\n"; |
||
| 186 | } |
||
| 187 | c223def9 | Antoine Beaupré | } |
| 188 | 0866999f | Vlad Marian | else {
|
| 189 | print "no (mysql not found)\n"; |
||
| 190 | c223def9 | Antoine Beaupré | } |
| 191 | exit $return; |
||
| 192 | } |
