summaryrefslogtreecommitdiff
path: root/files/munin/mysql_connections
blob: 8ba9ee2bcf2ee54d6d7e3ecbc7a56f9ef13f48b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/perl
#
# Copyright (C) 2008 Rackspace US, Inc. <http://www.rackspace.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
#
#
# This plugin is based off of the Connection Usage 
# section of the MySQL Connection Health Page
# 
# http://dev.mysql.com/doc/administrator/en/mysql-administrator-health-connection-health.html
#
# To enable, link mysql_connections to this file. E.g.
#
#    ln -s /usr/share/node/node/plugins/mysql_connections /etc/munin/plugins/mysql_connections
#
# Revision 1.0  2007/08/03
# Created by Justin Shepherd <galstrom21@gmail.com>
#
# Parameters:
#
#   config
#   autoconf
#
# Configuration variables
#
#   mysqlopts     - Options to pass to mysql
#   mysqladmin    - Override location of mysqladmin
#   warning	  - Override default warning limit
#   critical	  - Override default critical limit 
#
#%# family=auto
#%# capabilities=autoconf

use strict;

# Define the mysqladmin paths, and commands
my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin";
my $TEST_COMMAND    = "$MYSQLADMIN $ENV{mysqlopts} extended-status";
my $MYSQL_VARIABLES = "$MYSQLADMIN $ENV{mysqlopts} extended-status variables";
my $warning = $ENV{warning} || "80";
my $critical = $ENV{critical} || "90";

# Pull in any arguments
my $arg = shift();

# Check to see how the script was called 
if ($arg eq 'config') {
    print_graph_information();
    exit();
} elsif ($arg eq 'autoconf') {
    if (test_service()) { print "yes\n"; }
    else { print "no\n"; }
    exit;
} else {
	# Define the values that are returned to munin
	my ($available, $current, $upper_limit) = (0,0,0);
	
	# Gather the values from mysqladmin
	$current = poll_variables($MYSQL_VARIABLES,"Threads_connected");
	$upper_limit = poll_variables($MYSQL_VARIABLES,"max_connections");
	$available = $upper_limit - $current;

	# Return the values to Munin
	print "current.value $current\n";
	print "available.value $available\n";
}


sub poll_variables {
	my $command = shift;
	my $expression = shift;
	my $ret = 0;
	open(SERVICE, "$command |")
  		or die("Coult not execute '$command': $!");
	while (<SERVICE>) {
            my ($field, $value) = (m/(\w+).*?(\d+(?:\.\d+)?)/);
	    next unless ($field);
	    if ($field eq $expression ) {
		$ret = "$value";
	    }
	}
	close(SERVICE);
	return $ret;
}


sub print_graph_information {
print <<EOM;
graph_title MySQL Connections
graph_args --base 1000 --lower-limit 0
graph_vlabel Connections
graph_info The number of current connections with respect to the max_connections setting.
graph_category mysql
graph_order current available
graph_total Total
current.label In Use
current.draw AREA
current.info The number of current threads connected
current.warning $warning
current.critical $critical
available.label Available
available.draw STACK
available.info The current value of the "max_connections" variable
EOM
}


sub test_service {
    my $return = 1;
    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
    if ($? == 0)
    {
		system ("$TEST_COMMAND >/dev/null 2>/dev/null");
		if ($? == 0)
		{
		    print "yes\n";
		    $return = 0;
		}
		else
		{
	   		print "no (could not connect to mysql)\n";
		}
    }
    else
    {
		print "no (mysqladmin not found)\n";
    }
    exit $return;
}