wiki:PluginPortrait/mysql_row_count_

This is a wildcard plugin that takes the name of a table, and plots the number of rows in the table.

Append the name of the table to the name of the file to make it work. "mv mysql_row_count_ mysql_row_count_people"

Also, set the environment variable MYSQL_DATABASE to specify the database.

#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;

my $table = basename $0;
$table =~ s/^mysql_row_count_//;
$table =~ /^\w+$/ or die "no table";

if ($ARGV[0] && $ARGV[0] eq 'config') {
   print <<END;
graph_title MySQL row count: $table
graph_vlabel $table
$table.label $table
END

   exit;
}

open(MYSQL, "/usr/bin/mysql -e 'SELECT COUNT(*) FROM $table' " .
            "\Q${ENV{'MYSQL_DATABASE'}}\E |") or die $!;
my @lines = <MYSQL>;

printf("%s.value %d\n", $table, $lines[1]);
Last modified at 2006-09-24T08:19:05+02:00 Last modified on 2006-09-24T08:19:05+02:00