Skip to content

Commit

Permalink
SchemaSize plugin; added plugin::collect_data() option for better sep…
Browse files Browse the repository at this point in the history
…aration
  • Loading branch information
meersjo committed Jun 2, 2014
1 parent 023d342 commit 7faa2db
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<http://oierud.name/bliki/ImprovedMuninGraphsForMySQL.html>

Expand Down Expand Up @@ -69,10 +72,12 @@ Troubleshooting
<http://github.com/kjellm/munin-mysql/issues>


Author
------
Authors
-------

Johan De Meersman &lt;vegivamp AT tuxera DOT be&gt;

Kjell-Magne Øierud &lt;kjellm AT oierud DOT net&gt;
Blatantly stolen, I mean forked off the code by Kjell-Magne Øierud &lt;kjellm AT oierud DOT net&gt;

Inspired by the cacti graphs made by Xaprb
http://code.google.com/p/mysql-cacti-templates/ as viewed on
Expand Down
44 changes: 32 additions & 12 deletions contrib/Munin/MySQL/Graph/SchemaSize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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;
}

Expand Down
38 changes: 21 additions & 17 deletions mysql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}


Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7faa2db

Please sign in to comment.