#!/usr/bin/perl
#
# $Id: mysql_queries.in 1142 2006-10-17 12:27:35Z tore $
#
# Copyright 2003-2004 - Per Andreas Buer
#
# $Log$
# Revision 1.11  2004/12/10 18:51:43  jimmyo
# linux/apt* has been forced to LANG=C, to get predictable output.
#
# Revision 1.10  2004/12/10 10:47:47  jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.9  2004/12/09 22:12:55  jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.8  2004/11/21 00:16:56  jimmyo
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
#
# Revision 1.7  2004/09/08 15:25:33  ilmari
# Use /usr/bin/perl in all perl shebang lines.
#
# Revision 1.6  2004/05/20 13:57:12  jimmyo
# Set categories to some of the plugins.
#
# Revision 1.5  2004/04/27 18:58:53  jimmyo
# Fixed bug in mysql-plugins (Deb#233762).
#
# Revision 1.4  2004/01/29 18:49:55  jimmyo
# Bugfix in plugin mysql_queries - insertions were no longer graphed. (SF#881483).
#
# Revision 1.3  2004/01/29 17:36:20  jimmyo
# Updated copyright information
#
# Revision 1.2  2004/01/15 16:35:43  jimmyo
# Bugfix from Dagfinn I. Mannsker, closing SF#876443, SF#865125.
#
# Revision 1.1  2004/01/02 18:50:00  jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.8  2003/12/01 13:31:37  jimmyo
# Bugfix to make stack/area right
#
# Revision 1.7  2003/11/08 00:10:13  jimmyo
# New mysql_queries plugin
#
#
# Parameters:
#
#   config
#   autoconf
#
# Configuration variables
#
#   mysqlopts     - Options to pass to mysql
#   mysqladmin    - Override location of mysqladmin
#
#%# family=auto
#%# capabilities=autoconf

use strict;

my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin";
my $COMMAND    =      "$MYSQLADMIN $ENV{mysqlopts} extended-status";


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;
}


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_total total\n");

    for my $key (keys %WANTED) {
        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 {

    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 (mysqadmin not found)\n";
    }
    exit $return;
}
