#!/usr/bin/perl # # Plugin to show loadtime of domains and/or URLs per hour # # Contributed by GwenDragon # (c) 2010 by GwenDragon # 2010-6-15 GwenDragon # # Magic markers - optional - used by installation scripts and # munin-config: # #%# family=manual #%# capabilities=autoconf # configuration example # # [loadtime] # env.usevhosts = 1 # env.vhostsroot = /var/www/vhosts # env.domains = domain1.tld domain2.tld # env.urls = domain1.tld/test/this.htm domain2.tld/test2/ https://domain3.tld/ # use strict; use LWP::UserAgent; use Time::HiRes qw(gettimeofday tv_interval); use Data::Dumper qw(Dumper); my $DEBUG = 0; $DEBUG = 1 if $ARGV[0] and $ARGV[0] eq 'debug'; my $usevhosts = 1; $usevhosts = $ENV{'usevhosts'} if defined $ENV{'usevhosts'}; my $vhostsroot = $ENV{'vhostsroot'} || '/var/www/vhosts'; my @domains; push @domains, grep( !/^(default|chroot)$/, qx(ls $vhostsroot) ) if $usevhosts; push @domains, split /\s+/, $ENV{'domains'} if defined $ENV{'domains'}; #@domains = qw(gwendragon.de opera.com) # if $DEBUG; chomp(@domains); my @urls; @urls = split /\s+/, $ENV{'urls'} if defined $ENV{'urls'}; chomp(@urls); my %domain_data; foreach my $d (@domains,@urls) { my $label = $d; $label =~ s/^https?:\/\///g; $label =~ s/[^A-Za-z]/_/g; $domain_data{$d} = [ $label, -1 ]; } if ($ARGV[0] and $ARGV[0] eq 'autoconf') { print 'yes'; exit 0; } elsif ($ARGV[0] and $ARGV[0] eq 'config') { print <[0]; print "$label.label $label\n"; print "$label.draw LINE1\n"; print "$label.info Load time of $domain\n"; } exit 0; } $DEBUG && print STDERR Dumper(%domain_data); my $ua = LWP::UserAgent->new; $ua->agent('Checkbot/0.4 '); my ($elapsed,$t0); for my $domain (keys %domain_data) { my $d = $domain; $d = "http://$d" if $d !~ m(^https?://); $DEBUG && print STDERR "Fetching $d\n" if $DEBUG; $t0 = [gettimeofday]; my $response = $ua->get($d); $DEBUG && print STDERR $response->is_success ? "": "#ERROR# ", $response->status_line, "\n"; if ($response->is_success) { $elapsed = tv_interval ($t0, [gettimeofday]) if $response->is_success; $DEBUG && print STDERR "Fetching $d elapsed $elapsed\n"; $DEBUG && print STDERR Dumper(%domain_data); $domain_data{$domain}->[1] = $elapsed; $DEBUG && print STDERR Dumper(%domain_data); } } print $domain_data{$_}->[0], ".value ", $domain_data{$_}->[1], "\n" foreach keys %domain_data; 1;