#!/usr/bin/perl # # Copyright (C) 2007 - Rodolphe Quiedeville # Copyright (C) 2003-2004 - Andreas Buer # # 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, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # $Log$ # Revision 1.0 2007/01/18 11:34:01 rodo # Created by Rodolphe Quiedeville # # Comments: # If you remove database on your system, delete the statefile to purge datas. # We don't show all the database only those used. # # Parameters: # # config # autoconf # # Configuration variables # # mysqlopts - Options to pass to mysql # mysqladmin - Override location of mysqladmin # #%# family=manual #%# capabilities=autoconf use strict; my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin"; my $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} processlist | cut -d '|' -f 5"; my $STATEFILE = "/var/lib/munin/plugin-state/plugin-mysql_process.state"; my %WANTED; if (-r $STATEFILE) { open(STATE, "<$STATEFILE") or die("Coult not open '$STATEFILE': $!"); while () { my ($db) = (m/(\w+)/); $WANTED{$db} = 0; } close(STATE); } open(SERVICE, "$COMMAND |") or die("Coult not execute '$COMMAND': $!"); my $i = 0; while () { $i++; if ($i > 3) { my ($db) = (m/\s+(\w+).*/); $WANTED{$db} += 1 if ($db); } } close(SERVICE); my $arg = shift(); if ($arg eq 'config') { print_config(); exit(); } elsif ($arg eq 'autoconf') { unless (test_service() ) { print "yes\n"; } else { print "no\n"; } exit; } open(STATE, ">$STATEFILE") or die("Coult not open '$STATEFILE': $!"); for my $key (keys %WANTED) { print STATE "$key\n"; } close(STATE); for my $key (keys %WANTED) { print("$key.value $WANTED{$key}\n"); } sub print_config { print('graph_title MySQL process graph_args --base 1000 -l 0 graph_vlabel process graph_category mysql graph_total Total graph_info Plugin available at http://rodolphe.quiedeville.org/hack/munin/ '); my $num = 0; for my $key (keys %WANTED) { print("$key.label $key\n"); print("$key.draw ", ($num) ? "STACK" : "AREA" , "\n"); $num ++; } } sub test_service { my $return = 1; system ("$MYSQLADMIN --version >/dev/null 2>/dev/null"); if ($? == 0) { system ("$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; }