2011-10-18 21:07:28 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
=head1 INSTALLATION
|
|
|
|
|
|
|
|
This plugin does http requests to specified URLs and takes the response time.
|
|
|
|
Use it to monitor remote sites.
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2011-10-18 21:07:28 +02:00
|
|
|
LWP::UserAgent and Time::HiRes are required
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
|
|
|
[http_request_time]
|
2013-02-03 07:31:45 +01:00
|
|
|
env.url1 http://127.0.0.1/1
|
2012-11-01 09:52:06 +01:00
|
|
|
env.url2 http://127.0.0.1/2
|
|
|
|
env.url3 http://www.example.com
|
|
|
|
env.url3_name some_munin_internal_name
|
|
|
|
env.url3_label Some random page on our website
|
|
|
|
env.url3_proxy http://firewall:3128
|
2013-07-10 18:01:41 +02:00
|
|
|
env.timeout 3
|
|
|
|
|
|
|
|
Timeout is the timeout of any HTTP request. Tune to avoid a complete
|
|
|
|
timeout of the plugin.
|
2011-10-18 21:07:28 +02:00
|
|
|
|
|
|
|
=head1 MAGIC MARKERS
|
|
|
|
|
|
|
|
#%# family=auto
|
|
|
|
#%# capabilities=autoconf
|
|
|
|
|
|
|
|
=head1 LICENSE
|
|
|
|
|
|
|
|
GPLv2
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Munin::Plugin;
|
|
|
|
use Time::HiRes qw(gettimeofday tv_interval);
|
|
|
|
my $ret = undef;
|
|
|
|
|
|
|
|
need_multigraph();
|
|
|
|
|
|
|
|
sub clean {
|
|
|
|
my $surl=shift;
|
|
|
|
$surl=~s/^https?:\/\///;
|
|
|
|
$surl=~s|%[\w\d]|_|g;
|
|
|
|
$surl=~s|[^\w\d_]|_|g;
|
|
|
|
$surl=~s|_*$||g;
|
|
|
|
$surl=~s|^_*||g;
|
|
|
|
return $surl;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (! eval "require LWP::UserAgent;")
|
|
|
|
{
|
|
|
|
$ret = "LWP::UserAgent not found";
|
|
|
|
if ( ! defined $ARGV[0] ) {
|
|
|
|
die $ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my %URLS;
|
2012-11-01 09:52:06 +01:00
|
|
|
|
2013-07-09 23:13:46 +02:00
|
|
|
# timeout in seconds for requests
|
2013-07-09 23:14:20 +02:00
|
|
|
# slightly lower than the default global timeout (5 seconds)
|
|
|
|
my $timeout = $ENV{'timeout'} || 3;
|
2013-07-09 23:13:46 +02:00
|
|
|
|
2012-11-01 09:52:06 +01:00
|
|
|
for (my $i = 1; $ENV{"url$i"}; $i++)
|
|
|
|
{
|
|
|
|
my $url = $ENV{"url$i"};
|
|
|
|
my $proxy = $ENV{"url${i}_proxy"};
|
|
|
|
my $name = $ENV{"url${i}_name"} || clean($url);
|
|
|
|
my $label = $ENV{"url${i}_label"} || $url;
|
2018-08-02 02:03:42 +02:00
|
|
|
|
2012-11-01 09:52:06 +01:00
|
|
|
$URLS{$name}={
|
|
|
|
url=>$url,
|
|
|
|
proxy=>$proxy,
|
|
|
|
label=>$label,
|
2011-10-18 21:07:28 +02:00
|
|
|
time=>'U'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" )
|
|
|
|
{
|
|
|
|
if ($ret)
|
|
|
|
{
|
|
|
|
print "no ($ret)\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2013-07-09 23:13:46 +02:00
|
|
|
my $ua = LWP::UserAgent->new(timeout => $timeout);
|
2011-10-18 21:07:28 +02:00
|
|
|
|
|
|
|
foreach my $url (keys %URLS) {
|
|
|
|
my $response = $ua->request(HTTP::Request->new('GET',$url));
|
|
|
|
if ($response->is_success) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "no (URL $url: ". $response->message .")\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print "yes\n";
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( defined $ARGV[0] and $ARGV[0] eq "config" )
|
|
|
|
{
|
|
|
|
# master graph
|
|
|
|
print "multigraph http_request_time\n";
|
|
|
|
print "graph_title HTTP(S) Request response times\n";
|
|
|
|
print "graph_args --base 1000\n";
|
|
|
|
print "graph_vlabel response time in ms\n";
|
2017-02-21 15:09:54 +01:00
|
|
|
print "graph_category webserver\n";
|
2011-10-18 21:07:28 +02:00
|
|
|
|
|
|
|
my @go;
|
2012-11-01 09:52:06 +01:00
|
|
|
foreach my $name (keys %URLS) {
|
|
|
|
my $url = $URLS{$name};
|
|
|
|
print "$name.label $$url{'label'}\n";
|
|
|
|
print "$name.info The response time of a single request\n";
|
|
|
|
print "$name.min 0\n";
|
|
|
|
print "$name.draw LINE1\n";
|
|
|
|
push(@go, $name);
|
2011-10-18 21:07:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# multigraphs
|
|
|
|
|
2012-11-01 09:52:06 +01:00
|
|
|
foreach my $name (keys %URLS) {
|
|
|
|
my $url = $URLS{$name};
|
|
|
|
print "\nmultigraph http_request_time.$name\n";
|
2011-10-18 21:07:28 +02:00
|
|
|
print "graph_title $$url{'url'}\n";
|
|
|
|
print "graph_args --base 1000\n";
|
|
|
|
print "graph_vlabel response time in ms\n";
|
2017-02-21 15:09:54 +01:00
|
|
|
print "graph_category webserver\n";
|
2012-11-01 09:52:06 +01:00
|
|
|
print "$name.label $$url{'label'}\n";
|
|
|
|
print "$name.info The response time of a single request\n";
|
|
|
|
print "$name.min 0\n";
|
|
|
|
print "$name.draw LINE1\n";
|
2011-10-18 21:07:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
2013-07-09 23:13:46 +02:00
|
|
|
my $ua = LWP::UserAgent->new(timeout => $timeout);
|
2012-11-01 09:52:06 +01:00
|
|
|
foreach my $name (keys %URLS) {
|
|
|
|
my $url = $URLS{$name};
|
2011-10-18 21:07:28 +02:00
|
|
|
|
2012-11-01 09:52:06 +01:00
|
|
|
if ($url->{proxy}) {
|
|
|
|
$ua->proxy(['http', 'ftp'], $url->{proxy});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ua->proxy(['http', 'ftp'], undef);
|
|
|
|
}
|
|
|
|
|
|
|
|
# warm up
|
2011-10-18 21:07:28 +02:00
|
|
|
my $response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
|
2012-11-01 09:52:06 +01:00
|
|
|
|
|
|
|
# timed run
|
|
|
|
my $t1=[gettimeofday];
|
|
|
|
$response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
|
2011-10-18 21:07:28 +02:00
|
|
|
my $t2=[gettimeofday];
|
2012-11-01 09:52:06 +01:00
|
|
|
|
2011-10-18 21:07:28 +02:00
|
|
|
if ($response->is_success) {
|
|
|
|
$$url{'time'}=sprintf("%d",tv_interval($t1,$t2)*1000);
|
2018-08-02 02:03:42 +02:00
|
|
|
};
|
2011-10-18 21:07:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
print("multigraph http_request_time\n");
|
2012-11-01 09:52:06 +01:00
|
|
|
foreach my $name (keys %URLS) {
|
|
|
|
my $url = $URLS{$name};
|
|
|
|
print("$name.value $$url{'time'}\n");
|
2011-10-18 21:07:28 +02:00
|
|
|
}
|
|
|
|
|
2012-11-01 09:52:06 +01:00
|
|
|
foreach my $name (keys %URLS) {
|
|
|
|
my $url = $URLS{$name};
|
|
|
|
print("\nmultigraph http_request_time.$name\n");
|
|
|
|
print("$name.value $$url{'time'}\n");
|
|
|
|
}
|
2011-10-18 21:07:28 +02:00
|
|
|
|
|
|
|
# vim:syntax=perl
|