Thread LWP: Unterschiedliche Ablaufzeiten bei $ua->get (13 answers)
Opened by GwenDragon at 2010-06-15 15:29

GwenDragon
 2010-06-15 15:29
#138378 #138378
User since
2005-01-17
14785 Artikel
Admin1
[Homepage]
user image
Ich programmiere derzeit ein Plugin für Munin.

Code (perl): (dl )
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;


Was ich als problematisch bemerke, ist die Tatsache, dass das erste get immer länger dauert.


[root@server1 ~]# export urls="gwendragon.de localhost";export domains=;./loadtime
gwendragon_de.value 0.049852
localhost.value 0.003877
[root@server1 ~]# export urls="localhost";export domains=;./loadtime
localhost.value 0.044676
[root@server1 ~]#


Als Debug:
[root@server1 ~]# export urls="localhost";export domains=;./loadtime debug
$VAR1 = 'localhost';
$VAR2 = [
'localhost',
-1
];
Fetching http://localhost/
200 OK
Fetching http://localhost/ elapsed 0.045046
$VAR1 = 'localhost';
$VAR2 = [
'localhost',
-1
];
$VAR1 = 'localhost';
$VAR2 = [
'localhost',
'0.045046'
];
localhost.value 0.045046
[root@server1 ~]# export urls="gwendragon.de localhost";export domains=;./loadtime debug
$VAR1 = 'gwendragon.de';
$VAR2 = [
'gwendragon_de',
-1
];
$VAR3 = 'localhost';
$VAR4 = [
'localhost',
-1
];
Fetching http://gwendragon.de/
200 OK
Fetching http://gwendragon.de/ elapsed 0.04829
$VAR1 = 'gwendragon.de';
$VAR2 = [
'gwendragon_de',
-1
];
$VAR3 = 'localhost';
$VAR4 = [
'localhost',
-1
];
$VAR1 = 'gwendragon.de';
$VAR2 = [
'gwendragon_de',
'0.04829'
];
$VAR3 = 'localhost';
$VAR4 = [
'localhost',
-1
];
Fetching http://localhost/
200 OK
Fetching http://localhost/ elapsed 0.003195
$VAR1 = 'gwendragon.de';
$VAR2 = [
'gwendragon_de',
'0.04829'
];
$VAR3 = 'localhost';
$VAR4 = [
'localhost',
-1
];
$VAR1 = 'gwendragon.de';
$VAR2 = [
'gwendragon_de',
'0.04829'
];
$VAR3 = 'localhost';
$VAR4 = [
'localhost',
'0.003195'
];
gwendragon_de.value 0.04829
localhost.value 0.003195
[root@server1 ~]#


Hat jemand Ahnung woher die unterschiedlichen Zeiten beim Fetch kommen?
Und wie das abgestellt werden könnte?

View full thread LWP: Unterschiedliche Ablaufzeiten bei $ua->get