Projet

Général

Profil

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

root / plugins / mysql / mysql_connections @ 17f78427

Historique | Voir | Annoter | Télécharger (3,67 ko)

1 e7c04da2 Justin Shepherd
#!/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 e7c04da2 Justin Shepherd
# section of the MySQL Connection Health Page
21 17f78427 Lars Kruse
#
22 e7c04da2 Justin Shepherd
# 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
# Parameters:
32
#
33
#   config
34
#   autoconf
35
#
36
# Configuration variables
37
#
38
#   mysqlopts     - Options to pass to mysql
39
#   mysqladmin    - Override location of mysqladmin
40
#   warning	  - Override default warning limit
41 17f78427 Lars Kruse
#   critical	  - Override default critical limit
42 e7c04da2 Justin Shepherd
#
43
#%# family=auto
44
#%# capabilities=autoconf
45
46
use strict;
47
48
# Define the mysqladmin paths, and commands
49
my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin";
50
my $TEST_COMMAND    = "$MYSQLADMIN $ENV{mysqlopts} extended-status";
51
my $MYSQL_VARIABLES = "$MYSQLADMIN $ENV{mysqlopts} extended-status variables";
52
my $warning = $ENV{warning} || "80";
53
my $critical = $ENV{critical} || "90";
54
55
# Pull in any arguments
56
my $arg = shift();
57
58 17f78427 Lars Kruse
# Check to see how the script was called
59 e7c04da2 Justin Shepherd
if ($arg eq 'config') {
60
    print_graph_information();
61
    exit();
62
} elsif ($arg eq 'autoconf') {
63
    if (test_service()) { print "yes\n"; }
64
    else { print "no\n"; }
65
    exit;
66
} else {
67
	# Define the values that are returned to munin
68
	my ($available, $current, $upper_limit) = (0,0,0);
69 17f78427 Lars Kruse
70 e7c04da2 Justin Shepherd
	# Gather the values from mysqladmin
71
	$current = poll_variables($MYSQL_VARIABLES,"Threads_connected");
72
	$upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections");
73
	$available = $upper_limit - $current;
74
75
	# Return the values to Munin
76
	print "current.value $current\n";
77
	print "available.value $available\n";
78
}
79
80
81
sub poll_variables {
82
	my $command = shift;
83
	my $expression = shift;
84
	my $ret = 0;
85
	open(SERVICE, "$command |")
86
  		or die("Coult not execute '$command': $!");
87
	while (<SERVICE>) {
88
            my ($field, $value) = (m/(\w+).*?(\d+(?:\.\d+)?)/);
89
	    next unless ($field);
90
	    if ($field eq $expression ) {
91
		$ret = "$value";
92
	    }
93
	}
94
	close(SERVICE);
95
	return $ret;
96
}
97
98
99
sub print_graph_information {
100
print <<EOM;
101
graph_title MySQL Connections
102
graph_args --base 1000 --lower-limit 0
103
graph_vlabel Connections
104
graph_info The number of current connections with respect to the max_connections setting.
105 8af93fce dipohl
graph_category db
106 e7c04da2 Justin Shepherd
graph_order current available
107
graph_total Total
108
current.label In Use
109
current.draw AREA
110
current.info The number of current threads connected
111
current.warning $warning
112
current.critical $critical
113
available.label Available
114
available.draw STACK
115
available.info The current value of the "max_connections" variable
116
EOM
117
}
118
119
120
sub test_service {
121
    my $return = 1;
122
    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
123
    if ($? == 0)
124
    {
125
		system ("$TEST_COMMAND >/dev/null 2>/dev/null");
126
		if ($? == 0)
127
		{
128
		    print "yes\n";
129
		    $return = 0;
130
		}
131
		else
132
		{
133
	   		print "no (could not connect to mysql)\n";
134
		}
135
    }
136
    else
137
    {
138
		print "no (mysqladmin not found)\n";
139
    }
140
    exit $return;
141
}