#!/usr/bin/perl # Program: mysql_ratios # Author: James Briggs # Env: Perl 5 # Purpose: Munin plugin to calculate and display MySQL read and write cache performance # Date: 2011-10-03 # Note: test with: # munin-run --debug mysql_ratios config use strict; use diagnostics; use Munin::Plugin; my %config = ( 'user' => $ENV{'mysqluser'} || 'root', 'password' => $ENV{'mysqlpassword'} || '', ); my $MYSQLADMIN = $ENV{mysqladmin} || "mysqladmin"; my $COMMAND = "$MYSQLADMIN $ENV{mysqlopts} -u $config{'user'} -p$config{'password'} extended-status"; my $op = shift || ''; my %info; if ($op eq 'config') { print <{Innodb_buffer_pool_read_requests}) { $innodb_reads = 1 - ($r->{Innodb_buffer_pool_reads} / $r->{Innodb_buffer_pool_read_requests}); } my $myisam_reads = 0; if ($r->{Key_read_requests}) { $myisam_reads = 1 - ($r->{Key_reads} / $r->{Key_read_requests}); } my $myisam_reads_miss = 0; if ($r->{Uptime}) { $myisam_reads_miss = $r->{Key_reads} / $r->{Uptime}; } my $myisam_writes = 0; if ($r->{Key_write_requests}) { $myisam_writes = 1 - ($r->{Key_writes} / $r->{Key_write_requests}); } my $qcache_reads_ratio = 0; if (($r->{Qcache_hits}) + $r->{Qcache_inserts}) { $qcache_reads_ratio = 100 * $r->{Qcache_hits} / ($r->{Qcache_hits} + $r->{Com_select}); } my $qcache_updates_ratio = 0; if ($r->{Qcache_hits}) { $qcache_updates_ratio = ($r->{Com_insert}+$r->{Com_delete}+$r->{Com_update}+$r->{Com_replace})/$r->{Qcache_hits}; } my $fmt = "%s %.2f\n"; print sprintf($fmt, 'innodb_reads_hit_percent.value', 100 * $innodb_reads); print sprintf($fmt, 'myisam_reads_hit_percent.value', 100 * $myisam_reads); print sprintf($fmt, 'myisam_reads_miss_rate.value', $myisam_reads_miss); print sprintf($fmt, 'myisam_writes_hit_percent.value', 100 * $myisam_writes); print sprintf($fmt, 'qcache_reads_hit_percent.value', $qcache_reads_ratio); print sprintf($fmt, 'qcache_updates_miss_rate.value', $qcache_updates_ratio); exit; # +---------------------------------------+-------------+ # | Variable_name | Value | # +---------------------------------------+-------------+ # | Aborted_clients | 63 | # | Aborted_connects | 8 | # | Binlog_cache_disk_use | 0 | # | Binlog_cache_use | 0 | sub my_status { my %v; open(SERVICE, "$COMMAND |") or die("Could not execute '$COMMAND': $!"); while () { if (/(\w+).*?(\w+)/) { next if $1 eq 'Variable_name'; $v{$1} = $2; } } close SERVICE; return \%v; } sub my_info { my ($title, $min, $max) = @_; print <