Skip to content

Instantly share code, notes, and snippets.

@toritori0318
Created June 11, 2011 14:57
Show Gist options
  • Select an option

  • Save toritori0318/1020631 to your computer and use it in GitHub Desktop.

Select an option

Save toritori0318/1020631 to your computer and use it in GitHub Desktop.
plackのステータスを表示するmuninプラグイン
#!/usr/bin/perl -w
# -*- cperl -*-
=head1 NAME
plack_status - Munin plugin to show the connection status for plack
=encoding utf8
=head1 APPLICABLE SYSTEMS
Any plack host
=head1 CONFIGURATION
This shows the default configuration of this plugin. You can override
the status URL and the User Agent.
[plack*]
env.url http://localhost/server-status
env.ua plack-status-verifier/0.1
psgi file must also be configurd.
builder {
enable "Plack::Middleware::ServerStatus::Lite",
path => '/server-status',
scoreboard => '/var/run/scoreboard',
allow => [ '127.0.0.1', '10.0.0.0/8' ];
};
=head1 VERSION
1.0
=head1 BUGS
None known
=head1 AUTHOR
Unknown. torii <[email protected]>
=cut
use strict;
my $ret = undef;
if (! eval "require LWP::UserAgent;") {
$ret = "LWP::UserAgent not found";
}
chomp(my $fqdn=`hostname -f`);
## Environment defined variables.
## The default URL is plack_status if different set it in the environment.
my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://$fqdn/server-status";
## The default user agent is ngnix-status-verifier/0.1 if different
## set it in the environment.
my $UA = exists $ENV{'ua'} ? $ENV{'ua'} : 'plack-status-verifier/0.1';
## Munin autoconf method.
if (exists $ARGV[0] and $ARGV[0] eq "autoconf" ) {
if ($ret) {
print "no ($ret)\n";
exit 1;
}
my $ua = LWP::UserAgent->new(timeout => 30);
# Set the UA to something different from the libwww-perl.
# This UA is blocked.
$ua->agent($UA);
my $response = $ua->request(HTTP::Request->new('GET',$URL));
unless ($response->is_success and $response->content =~ /Uptime/im) {
print "no (no plack status on $URL)\n";
exit 1;
} else {
print "yes\n";
exit 0;
}
}
## Munin config method.
if (exists $ARGV[0] and $ARGV[0] eq "config") {
print "graph_title Plack status\n";
print "graph_args --base 1000\n";
print "graph_category plack\n";
print "graph_vlabel Connections\n";
print "busy.label Busy\n";
print "busy.info Busy\n";
print "busy.draw LINE2\n";
print "idle.label Idle\n";
print "idle.info Idle\n";
print "idle.draw LINE2\n";
exit 0;
}
my $ua = LWP::UserAgent->new(timeout => 30);
# Set the UA to something different from the libwww-perl.
# That UA is blocked.
$ua->agent($UA);
my $response = $ua->request(HTTP::Request->new('GET',$URL));
# Uptime: 1307506487
# BusyWorkers: 3
# IdleWorkers: 27
# --
my $content = $response->content;
my @output = ();
foreach my $line ( split /[\r\n]+/, $content ) {
if ( $line =~ /^Busy.+: (\d+)/ ) {
push @output, "busy.value $1";
}
if ( $line =~ /^Idle.+: (\d+)/ ) {
push @output, "idle.value $1";
}
}
if (scalar @output > 0) {
print "$_\n" for @output;
} else {
foreach (qw(busy idle)) {
print "$_.value U\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment