diff --git a/CHANGES b/CHANGES index 4f95370..9c4ad76 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,10 @@ +2014-06-02: version 0.4 + + - Plenty of various tweaks + + - Added a new SchemaSize module (WARNING: SLOW) + + - Threw around the init sequence and added support for in-module collect_data() sub 2010-02-16: version 0.3.1 - Changed data source type for Opened_tables to DERIVE (again). diff --git a/README.md b/README.md index fede5d4..2d18ada 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,9 @@ Further Information The plugin documentation is contained in the plugin file as POD. View it with perldoc. +The SchemaSize plugin performs a pretty heavy query on information_schema.tables. +Careful when using this on a large environment. + There is a blog post with some general information and screenshots at @@ -69,10 +72,12 @@ Troubleshooting -Author ------- +Authors +------- + +Johan De Meersman <vegivamp AT tuxera DOT be> -Kjell-Magne Øierud <kjellm AT oierud DOT net> +Blatantly stolen, I mean forked off the code by Kjell-Magne Øierud <kjellm AT oierud DOT net> Inspired by the cacti graphs made by Xaprb http://code.google.com/p/mysql-cacti-templates/ as viewed on diff --git a/contrib/Munin/MySQL/Graph/SchemaSize.pm b/contrib/Munin/MySQL/Graph/SchemaSize.pm index ec721a2..428f2d6 100644 --- a/contrib/Munin/MySQL/Graph/SchemaSize.pm +++ b/contrib/Munin/MySQL/Graph/SchemaSize.pm @@ -5,25 +5,32 @@ package Munin::MySQL::Graph::SchemaSize; # Grabs the data_length and index_length grouped per schema. # -sub graphs { +sub collect_data { + my $self = shift; my ($dbh) = @_; - my @data_sources; - my $query = 'SELECT table_schema, sum(data_length), sum(index_length)' + my $data = {}; + + my $query = 'SELECT table_schema, sum(data_length) as data_size, sum(index_length) as index_size' . ' FROM information_schema.tables' . ' GROUP BY table_schema'; my $sth = $dbh->prepare($query); $sth->execute(); - while (my $row = $sth->fetch) { - $data->{'size_schema_data_' . $row->[0]} = $row->[1]; - $data->{'size_schema_index_' . $row->[0]} = $row->[2]; - push(@data_sources, {name => 'size_schema_index_' . $row->[0], label => 'indexsize ' . $row->[0], - graph => 'no',}); - push(@data_sources, {name => 'size_schema_data_' . $row->[0], label => $row->[0], - negative => 'size_schema_index_' . $row->[0]}); + while (my $row = $sth->fetchrow_hashref) { + $data->{'database_size'}->{$row->{'table_schema'}} = { 'data' => $row->{'data_size'}, + 'index' => $row->{'index_size'}}; + $data->{'datasize_' . $row->{'table_schema'}} = $row->{'data_size'}; + $data->{'indexsize_' . $row->{'table_schema'}} = $row->{'index_size'}; } $sth->finish(); - + + return $data; +} + + +sub graphs { + my $self = shift; + my ($data) = @_; $graph = { schema_size => { config => { @@ -36,12 +43,25 @@ sub graphs { data_source_attrs => { draw => 'LINE1', type => 'GAUGE', + min => 'U', }, }, - data_sources => @data_sources, + data_sources => [], } }; + foreach $schema (keys %{$data->{'database_size'}}) { + if ( $schema eq 'information_schema' or + $schema eq 'performance_schema' ) { + next; + } + push(@{$graph->{'schema_size'}->{'data_sources'}}, ({'name' => 'indexsize_' . $schema, + 'label' => 'indexsize_' . $schema, + 'graph' => 'no'}, + {'name' => 'datasize_' . $schema, + 'label' => $schema, + 'negative' => 'indexsize_' . $schema})); + } return $graph; } diff --git a/mysql b/mysql index e543133..7f73552 100755 --- a/mysql +++ b/mysql @@ -209,10 +209,6 @@ my %graphs = (); sub main { my $command = $ARGV[0] || 'show'; - for my $graph_package (__PACKAGE__->plugins) { - %graphs = (%graphs, %{$graph_package->graphs}); - } - my %command_map = ( 'autoconf' => \&autoconf, 'config' => \&config, @@ -222,21 +218,29 @@ sub main { die "Unknown command: $command" unless exists $command_map{$command}; - build_instance(); - if ($command eq 'autoconf') { return $command_map{$command}->(); } - else { - if ($command eq 'show') { - collect_data(); - } - for my $graph (sort keys %graphs) { - print "multigraph mysql_$graph$instance\n"; - $command_map{$command}->($graph); + + build_instance(); + + my $dbh = db_connect() || die "Can't connect to DB"; + collect_data($dbh); + + for my $graph_package (__PACKAGE__->plugins) { + if ($graph_package->can('collect_data')) { + # Looks silly, but otherwise you destroy $data before copying it. + my $dummy = {%{$data}, %{$graph_package->collect_data($dbh)}}; + $data = $dummy; } + %graphs = (%graphs, %{$graph_package->graphs($data)}); } - return 0; + + for my $graph (sort keys %graphs) { + print "multigraph mysql_$graph$instance\n"; + $command_map{$command}->($graph); + } +return 0; } @@ -339,16 +343,16 @@ sub db_connect { return DBI->connect($dsn, $config{user}, $config{password}, { RaiseError => 1, - PrintError => 0, + PrintError => 1, FetchHashKeyName => 'NAME_lc', }); } sub collect_data { - $data = {}; + my ($dbh) = @_; - my $dbh = db_connect(); + $data = {}; # Set up defaults in case the server is not a slave $data->{relay_log_space} = 0;