File: //etc/munin/plugins/mysql_queries
#!/usr/local/cpanel/3rdparty/perl/536/bin/perl
# -*- perl -*-
=head1 NAME
mysql_queries - Munin plugin to display MySQL query rate.
=head1 APPLICABLE SYSTEMS
Any MySQL platform
=head1 CONFIGURATION
  [mysql*]
     user root
     env.mysqlopts <options-for-mysqladmin-here>
     env.mysqladmin <optional-override-of-mysqladmin-path>
It is most usual that root must run the mysqladmin command.
Only use the .env settings if you need to override the defaults.
=head1 INTERPRETATION
To Be Written
=head1 BUGS
None known
=head1 AUTHOR
Copyright 2003-2008 - Per Andreas Buer
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
  #%# family=manual
  #%# capabilities=autoconf
=cut
use strict;
my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin";
my $COMMAND    = "$MYSQLADMIN $ENV{mysqlopts} extended-status";
my @WANTED_ORDER = ( "Com_select", "Com_delete", "Com_insert", "Com_update", "Com_replace", "Qcache_hits" );
my %WANTED = ( "Com_delete"  => "delete",
               "Com_insert"  => "insert",
               "Com_select"  => "select",
               "Com_update"  => "update",
               "Com_replace" => "replace",
               "Qcache_hits" => "cache_hits",
             );
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 0;
}
open(SERVICE, "$COMMAND |")
  or die("Could not execute '$COMMAND': $!");
while (<SERVICE>) {
    my ($k, $v) = (m/(\w+).*?(\d+(?:\.\d+)?)/);
    next unless ($k);
    if (exists $WANTED{$k} ) {
	print("$WANTED{$k}.value $v\n");
    }
}
close(SERVICE);
sub print_config {
    my $num = 0;
    print("graph_title MySQL queries
graph_args --base 1000
graph_vlabel queries / \${graph_period}
graph_category mysql
graph_info Note that this is a old plugin which is no longer installed by default.  It is retained for compatability with old installations.
graph_total total\n");
    foreach my $key (@WANTED_ORDER){
        my $title = $WANTED{$key};
        print("$title.label ${title}\n",
              "$title.min 0\n",
              "$title.type DERIVE\n",
              "$title.max 500000\n",
              "$title.draw ", ($num) ? "STACK" : "AREA" ,  "\n",
             );
        $num++;
    }
}
sub test_service {
    system ("$MYSQLADMIN --version >/dev/null 2>/dev/null");
    if ($? == 0)
    {
	system ("$COMMAND >/dev/null 2>/dev/null");
	if ($? == 0)
	{
	    print "yes\n";
	}
	else
	{
	    print "no (could not connect to mysql)\n";
	}
    }
    else
    {
	print "no (mysqladmin not found)\n";
    }
    exit 0;
}