root / plugins / other / mysql_slave @ 7445b9d4
Historique | Voir | Annoter | Télécharger (3,11 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
# |
| 3 |
# Copyright (C) 2008 - Nathan Haneysmith <nathan@hjksolutions.com> |
| 4 |
# Copyright (C) 2007 - Rodolphe Quiedeville <rodolphe@quiedeville.org> |
| 5 |
# Copyright (C) 2003-2004 - Andreas Buer |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or |
| 8 |
# modify it under the terms of the GNU General Public License |
| 9 |
# as published by the Free Software Foundation; version 2 dated June, |
| 10 |
# 1991. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program; if not, write to the Free Software |
| 19 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 |
# |
| 21 |
# $Log$ |
| 22 |
# Revision 1.2 2008/06/18 Nathan Haneysmith |
| 23 |
# Massive rewrite to check and graph MySQL Slave status |
| 24 |
# |
| 25 |
# Revision 1.1 2007/01/17 10:41:01 rodo |
| 26 |
# Change incorrect family |
| 27 |
# |
| 28 |
# Revision 1.0 2007/01/16 15:57:01 rodo |
| 29 |
# Created by Rodolphe Quiedeville |
| 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 |
# |
| 41 |
#%# family=manual |
| 42 |
#%# capabilities=autoconf |
| 43 |
|
| 44 |
use strict; |
| 45 |
|
| 46 |
my $MYSQLADMIN = $ENV{mysqladmin} || "mysql";
|
| 47 |
my $MYSQLOPTS = $ENV{mysqlopts} || "";
|
| 48 |
|
| 49 |
my %WANTED = ( "Seconds" => "seconds", |
| 50 |
); |
| 51 |
|
| 52 |
my $arg = shift(); |
| 53 |
|
| 54 |
if ($arg eq 'config') {
|
| 55 |
print_config(); |
| 56 |
exit(); |
| 57 |
} elsif ($arg eq 'autoconf') {
|
| 58 |
unless (test_service() ) {
|
| 59 |
print "yes\n"; |
| 60 |
} else {
|
| 61 |
print "no\n"; |
| 62 |
} |
| 63 |
exit; |
| 64 |
} |
| 65 |
|
| 66 |
my $seconds = 0; |
| 67 |
my (@infos,$info,$i_seconds); |
| 68 |
|
| 69 |
my $COMMAND = "$MYSQLADMIN $MYSQLOPTS -e 'show slave status;' | grep 'Slave'"; |
| 70 |
|
| 71 |
open(SERVICE, "$COMMAND |") |
| 72 |
or die("Coult not execute '$COMMAND': $!");
|
| 73 |
|
| 74 |
while (<SERVICE>) {
|
| 75 |
(@infos) = split; |
| 76 |
} |
| 77 |
close(SERVICE); |
| 78 |
|
| 79 |
my $i = 0; |
| 80 |
foreach $info (@infos) {
|
| 81 |
$i++; |
| 82 |
if ($info eq 'Seconds_Behind_Master') {
|
| 83 |
$i_seconds = $i; |
| 84 |
next; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$COMMAND = "$MYSQLADMIN $MYSQLOPTS -e 'show slave status;' | cut -f $i_seconds | grep -v leng"; |
| 89 |
|
| 90 |
open(SERVICE, "$COMMAND |") |
| 91 |
or die("Coult not execute '$COMMAND': $!");
|
| 92 |
|
| 93 |
while (<SERVICE>) {
|
| 94 |
(m/(\d+).*?(\d+(?:\.\d+)?)/); |
| 95 |
$seconds += $1; |
| 96 |
} |
| 97 |
close(SERVICE); |
| 98 |
|
| 99 |
print("seconds.value $seconds\n");
|
| 100 |
|
| 101 |
|
| 102 |
sub print_config {
|
| 103 |
|
| 104 |
print "graph_title MySQL Slave Status\n"; |
| 105 |
print "graph_args --base 1000 -l 0\n"; |
| 106 |
print "graph_vlabel Seconds\n"; |
| 107 |
print "graph_category mysql\n"; |
| 108 |
print "seconds.label Seconds behind master\n"; |
| 109 |
print "seconds.min 0\n"; |
| 110 |
print "seconds.draw LINE2\n"; |
| 111 |
print "graph_info Plugin available at <a href='http://oss.hjksolutions.com/munin/'>http://oss.hjksolutions.com/munin/</a>\n"; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
sub test_service {
|
| 117 |
|
| 118 |
my $return = 1; |
| 119 |
|
| 120 |
system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
|
| 121 |
if ($? == 0) |
| 122 |
{
|
| 123 |
system ("$COMMAND >/dev/null 2>/dev/null");
|
| 124 |
if ($? == 0) |
| 125 |
{
|
| 126 |
print "yes\n"; |
| 127 |
$return = 0; |
| 128 |
} |
| 129 |
else |
| 130 |
{
|
| 131 |
print "no (could not connect to mysql)\n"; |
| 132 |
} |
| 133 |
} |
| 134 |
else |
| 135 |
{
|
| 136 |
print "no (mysqladmin not found)\n"; |
| 137 |
} |
| 138 |
exit $return; |
| 139 |
} |
