set('devel_dsn','dbi:mysql:host=192.168.1.115:username=foo:password=bar'); ( # # Execute shell commands # { match => qr{^shell (.*)}io, columns => sub { return $_[1] }, data => sub { split (m{[\r\n]}o, `$_[1]`) } }, # # Print the current environment as a response to an 'env' command # { match => 'env', columns => ['key','value'], data => sub { \%ENV } }, # # Send queries prefixed with devel to another server. Since a regular expression is used, the outgoing query # will be automatially taken from the string matched by the first set of brackets in the regexp. # { match => qr{^devel (.*)}o, dsn => sub { get('devel_dsn') } }, # # Redirect all queries issued after "devel" to another server. Restore with "restore" # { match => 'devel', ok => sub { set('old_dsn', get('dsn')); set('dsn', get('devel_dsn')); } }, { match => 'restore', ok => sub { set('dsn', get('old_dsn')); } }, # # Wrap every query starting with 'STATS' in SHOW STATUS # In 'before', we store the counters before the query, then on 'after', we calculate the difference and store # it again so that it can be displayed on 'SHOW STATS' # { match => qr{^stats (.*)}io, before => sub { set('stats_before', get('dbh')->selectall_hashref("SHOW STATUS","Variable_name")) }, after => sub { my $before = get('stats_before'); my $after = get('dbh')->selectall_hashref("SHOW STATUS","Variable_name"); my $diff; foreach my $key (keys %{$after}) { $diff->{$key} = $after->{$key}->{Value} - $before->{$key}->{Value}; delete $diff->{$key} if $diff->{$key} == 0; # Skip unchanged values } set('stats_diff', $diff); } }, { match => 'show stats', columns => ['Variable_name','Value'], data => sub { return (get('stats_diff') || []) } }, # # Final rule - Forward all other queries as they are to the default DBH # { match => qr{(.*)}o } );