Skip to content

Commit

Permalink
Converted to plugin to use multigraph functionality. closes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjell-Magne Øierud committed May 10, 2011
1 parent 5c54722 commit 9c1ed41
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 128 deletions.
30 changes: 10 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,41 @@ INSTANCES:=""

### Don't edit below this line

.PHONY: all test links clean install
.PHONY: all test clean install

all:

test: mysql_
test: mysql
@ echo testing ...
@ > test/values.out~
@ > test/config.out~
@ for CMD in `find . -maxdepth 1 -name 'mysql_?*' -and -type l | sort`; do \
perl -Itest/mock $$CMD >> test/values.out~; \
perl -Itest/mock $$CMD config >> test/config.out~; \
@ perl -Itest/mock mysql > test/values.out~; \
perl -Itest/mock mysql config > test/config.out~; \
done
diff -q test/values.out test/values.out~ || true
diff -q test/config.out test/config.out~ || true
@ echo ---------------------------------------------------------
@ prove test


links: mysql_
./mysql_ suggest | while read X; do ln -sf mysql_ mysql_$$X; done

install:
mkdir -p $(PLUGIN_DIR)
install mysql_ $(PLUGIN_DIR)
install mysql $(PLUGIN_DIR)

if [ ! -e $(CONFIG_DIR)/plugin-conf.d/mysql_.conf ]; then \
install mysql_.conf $(CONFIG_DIR)/plugin-conf.d; \
if [ ! -e $(CONFIG_DIR)/plugin-conf.d/mysql.conf ]; then \
install mysql.conf $(CONFIG_DIR)/plugin-conf.d; \
fi; \
if [ $(INSTANCES) = "" ]; then \
./mysql_ suggest | while read X; do \
ln -sf $(PLUGIN_DIR)/mysql_ $(CONFIG_DIR)/plugins/mysql_$$X; \
done; \
ln -sf $(PLUGIN_DIR)/mysql $(CONFIG_DIR)/plugins/mysql; \
else \
INSTANCES=$(INSTANCES); \
for I in $$INSTANCES; do \
./mysql_ suggest | while read X; do \
echo $$I $$X; \
ln -sf $(PLUGIN_DIR)/mysql_ $(CONFIG_DIR)/plugins/mysql_$${I}_$$X; \
done; \
echo $$I; \
ln -sf $(PLUGIN_DIR)/mysql $(CONFIG_DIR)/plugins/mysql_$${I}; \
done; \
fi

$(MUNIN_NODE) restart

clean:
rm -f test/values.out~ test/config.out~
find . -maxdepth 1 -name 'mysql_?*' -and -type l -exec rm {} \;

4 changes: 2 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ INSTALLATION
1) Install dependencies

- munin-node
- Perl modules: DBI, DBD::mysql, IPC::ShareLite, and Cache::Cache
- Perl modules: DBI, DBD::mysql

2) Edit Makefile

3) Edit mysql_.conf
3) Edit mysql.conf

4) Run `make install'

Expand Down
130 changes: 26 additions & 104 deletions mysql_ → mysql
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@ versions 5.1.41, 5.0.87, and 4.1.22.
=head1 CONFIGURATION
This script is used to generate data for several graphs. To generate
data for one specific graph, you need to create a symbolic link with a
name like mysql_<GRAPH> to this script.
You need to configure connection parameters to override the
defaults. These are the defaults:
To get a list of symlinks that can be created, run:
./mysql_ suggest
In addition you might need to specify connection parameters in the
plugin configuration to override the defaults. These are the defaults:
[mysql_*]
[mysql]
env.mysqlconnection DBI:mysql:mysql
env.mysqluser root
Non-default example:
[mysql_*]
[mysql]
env.mysqlconnection DBI:mysql:mysql;host=127.0.0.1;port=3306
env.mysqluser root
env.mysqlpassword geheim
Expand All @@ -51,11 +43,6 @@ measure slave lag. Add this to the configuration:
=over
=item Cache::Cache and IPC::ShareLite
The plugin uses shared memory to cache the statistics gathered from
MySQL. This ensures minimal inpact on the MySQL server.
=item DBD::mysql
=back
Expand Down Expand Up @@ -111,7 +98,7 @@ git-master
=head1 MAGICK MARKERS
#%# family=auto
#%# capabilities=suggest autoconf
#%# capabilities=autoconf
=cut

Expand All @@ -128,22 +115,20 @@ use Math::BigInt; # Used to append "=> lib 'GMP'" here, but GMP caused
use Munin::Plugin;
use POSIX qw(floor);

# Check that multigraph is supported
need_multigraph();

#
# Global hash holding the data collected from mysql.
#
our $data; # Was 'my'. Changed to 'our' to facilitate testing.


my $has_cache;
#
# FIX
#
my $instance;
my $shared_memory_cache;


BEGIN {
eval 'require Cache::SharedMemoryCache';
$has_cache = $@ ? 0 : 1;
}


#---------------------------------------------------------------------
# C O N F I G
Expand Down Expand Up @@ -1056,32 +1041,32 @@ $graphs{uptime} = {


sub main {
my $graph = basename($0);
# remove prefix and instance number from command name
$graph =~ s/^mysql_([0-9]+_)?//;

my $command = $ARGV[0] || 'show';

my %command_map = (
'autoconf' => \&autoconf,
'config' => \&config,
'show' => \&show,
'suggest' => \&suggest,
);

die "Unknown command: $command"
unless exists $command_map{$command};

die "Missing dependency Cache::Cache"
unless $has_cache || $command eq 'autoconf';

build_instance();

if ($has_cache) {
build_shared_memory_cache();
if ($command eq 'autoconf') {
return $command_map{$command}->();
}

return $command_map{$command}->($graph);
else {
if ($command eq 'show') {
collect_data();
}
for my $graph (sort keys %graphs) {
print "multigraph $graph$instance\n";
$command_map{$command}->($graph);
}
}
return 0;
}


Expand All @@ -1094,11 +1079,6 @@ sub main {

# http://munin.projects.linpro.no/wiki/ConcisePlugins#autoconf
sub autoconf {
unless ($has_cache) {
print "no (Missing dependency Cache::Cache)\n";
return 0;
}

eval {
db_connect();
};
Expand All @@ -1113,42 +1093,11 @@ sub autoconf {
}


# http://munin.projects.linpro.no/wiki/ConcisePlugins#suggest
sub suggest {

# What is the best way to decide which graphs is applicable to a
# given system?
#
# Does the database use InnoDB? A zero count from:
#
# SELECT COUNT(*)
# FROM tables
# WHERE table_type = 'base table'
# AND engine = 'innodb'
#
# Does the database use binary logs? 'OFF' as the result from:
#
# SHOW GLOBAL variables LIKE 'log_bin'
#
# Is the database setup as a slave? Empty result from:
#
# SHOW SLAVE STATUS

local $_;
print "$_\n" for (sort keys(%graphs));

return 0;
}


sub config {
my $graph_name = shift;

die 'Unknown graph ' . ($graph_name ? $graph_name : '')
unless $graphs{$graph_name};

my $graph = $graphs{$graph_name};

my %conf = (%{$defaults{global_attrs}}, %{$graph->{config}{global_attrs}});
while (my ($k, $v) = each %conf) {
print "graph_$k $v\n";
Expand All @@ -1175,20 +1124,13 @@ sub config {
printf("%s.%s %s\n", clean_fieldname($ds->{name}), $k, $v);
}
}

return 0;
}

sub show {
my $graph_name = shift;

die 'Unknown graph ' . ($graph_name ? $graph_name : '')
unless $graphs{$graph_name};

my $graph = $graphs{$graph_name};

update_data();

die "Can't show data for '$graph_name' because InnoDB is disabled."
if $graph_name =~ /innodb_/ && $data->{_innodb_disabled};

Expand All @@ -1199,8 +1141,6 @@ sub show {

printf "%s.value %s\n", clean_fieldname($ds->{name}), $value;
}

return 0;
}


Expand All @@ -1212,7 +1152,7 @@ sub show {

sub build_instance {
$instance = basename($0);
if($instance =~ /^mysql_([0-9]+)_/) {
if($instance =~ /^mysql_([0-9]+)/) {
$instance = "-$1";
}
else {
Expand All @@ -1221,17 +1161,6 @@ sub build_instance {
}


sub build_shared_memory_cache {
my %cache_options = (
'namespace' => "munin_mysql$instance",
'default_expires_in' => 60,
);

$shared_memory_cache = Cache::SharedMemoryCache->new(\%cache_options)
or die("Couldn't instantiate SharedMemoryCache");
}


sub db_connect {
my $dsn = "$config{dsn};mysql_connect_timeout=5";

Expand All @@ -1243,12 +1172,7 @@ sub db_connect {
}


sub update_data {
$data = $shared_memory_cache->get('data');
return if $data;

#warn "Need to update cache";

sub collect_data {
$data = {};

my $dbh = db_connect();
Expand All @@ -1267,8 +1191,6 @@ sub update_data {
update_master($dbh);
update_slave($dbh);
update_process_list($dbh);

$shared_memory_cache->set('data', $data);
}


Expand Down
2 changes: 1 addition & 1 deletion mysql_.conf → mysql.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[mysql_*]
[mysql]

# For valid values see the documentation for perl DBI (run 'perldoc
# DBI' or browse
Expand Down
2 changes: 1 addition & 1 deletion test/parse_innodb_status.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use FindBin;
# Both mysql_ and Test::More defines the sub skip. Put mysql_ in its
# own package so it don't polute the main:: name space
package mysql_;
Test::More::require_ok("$FindBin::Bin/../mysql_");
Test::More::require_ok("$FindBin::Bin/../mysql");
package main;

sub dump_data {
Expand Down

0 comments on commit 9c1ed41

Please sign in to comment.