Perlbal の Reproxy URL Cache の統計を Ganglia に送りつけるやっつけスクリプト。
mgmt のポートにつないで、/show service
% echo "show service web" | nc localhost 5700
show service tp_web
SERVICE tp_web
listening: --
role: reverse_proxy
pend clients: 0
pend backend: 0
cache size: 0/1024 (0.00%)
cache hits: 0
cache hit rate: 0.00%
connect-ahead: 0/0
pool: web_pool
nodes:
127.0.0.1:6500 0
.
(空っぽの状態だけど)これを parse して gmetric コマンドで定期的に ganglia に送りつけるだけ。
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw( :config posix_default no_ignore_case bundling auto_help );
use Pod::Usage;
use IO::Socket::INET;
GetOptions(\my %opt, qw( host=s service=s gmetric verbose ))
or pod2usage(1);
my $sock = IO::Socket::INET->new(PeerAddr => $opt{host}, Timeout => 2, Proto => 'tcp')
or die $!;
print $sock "show service $opt{service}\r\n";
my ($cached, $max, $hit, $rate);
while (my $line = <$sock>) {
last if $line =~ m{^\.};
chomp $line;
if ($line =~ m{\s*cache size: (\d+)/(\d+) \((\d+(?:\.\d+)?)\%\)}) {
$cached = $1;
$max = $2;
}
elsif ($line =~ m{\s*cache hits: (\d+)}) {
$hit = $1;
}
elsif ($line =~ m{\s*cache hit rate: (\d+(?:\.\d+)?)\%}) {
$rate = $1;
}
}
die unless (defined $cached && defined $max && defined $hit && defined $rate);
if ($opt{verbose}) {
print "$cached cached, $hit hits, $rate% hit\n";
}
if ($opt{gmetric}) {
system 'gmetric', '--name', 'perlbal_reproxy_cache_urls', '--value', $cached, '--type', 'uint32', '--group', 'perlbal';
system 'gmetric', '--name', 'perlbal_reproxy_cache_hits', '--value', $hit, '--type', 'uint32', '--group', 'perlbal';
system 'gmetric', '--name', 'perlbal_reproxy_cache_hit_rate', '--value', $rate, '--type', 'float', '--group', 'perlbal';
}
__END__
=pod
=head1 NAME
reproxy_cache_stats.pl - get reproxy cache stats
=head1 SYNOPSIS
% reproxy_cache_stats.pl --host=<host:port> --service=<name> [--gmetric] [--verbose]
=cut
というメモ。