Leser: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#!/usr/bin/perl # # Plugin to show loadtime of domains and/or URLs per hour # # Contributed by GwenDragon <info@gwendragon.de> # (c) 2010 by GwenDragon # 2010-6-15 GwenDragon <info@gwendragon.de> # # 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 <<CONFIG; graph_title HTTP loadtime of a domain graph_args --base 1000 -l 0 graph_vlabel Load time in seconds graph_category network graph_info This graph shows load time of domain/URL in seconds CONFIG for my $domain (keys %domain_data) { my $label = $domain_data{$domain}->[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;
$ua->get('http://127.0.0.1'); # force creation of internal objects for speedup
1
2
3
4
[0]> strace -e open perl -MLWP -e '$ua=LWP::UserAgent->new(); ' 2>&1 | wc -l
59
[0]> strace -e open perl -MLWP -e '$ua=LWP::UserAgent->new(); $ua->get("http://localhost/")' 2>&1 | wc -l
106
$DEBUG && print STDERR "Fetching $d\n" if $DEBUG;
1
2
3
for my $domain (keys %domain_data) {
my $d = $domain;
$d = "http://$d" if $d !~ m(^https?://);
1
2
3
4
for my $domain (keys %domain_data) {
$ua->get('http://localhost/');
my $d = $domain;
$d = "http://$d" if $d !~ m(^https?://);