Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / http / http_request_time @ 54c95632

Historique | Voir | Annoter | Télécharger (4,01 ko)

1
#!/usr/bin/perl
2

    
3
=head1 INSTALLATION
4

    
5
	This plugin does http requests to specified URLs and takes the response time.
6
	Use it to monitor remote sites.
7

    
8
	LWP::UserAgent and Time::HiRes are required
9

    
10
=head1 CONFIGURATION
11

    
12
  [http_request_time]
13
  env.url1  http://127.0.0.1/1
14
  env.url2  http://127.0.0.1/2
15
  env.url3  http://www.example.com
16
  env.url3_name  some_munin_internal_name
17
  env.url3_label Some random page on our website
18
  env.url3_proxy http://firewall:3128
19
  env.url3_agent Mozilla/5.0
20
  env.timeout 3
21

    
22
Timeout is the timeout of any HTTP request. Tune to avoid a complete
23
timeout of the plugin.
24

    
25
=head1 MAGIC MARKERS
26

    
27
  #%# family=auto
28
  #%# capabilities=autoconf
29

    
30
=head1 LICENSE
31

    
32
GPLv2
33

    
34
=cut
35

    
36
use strict;
37
use warnings;
38
use Munin::Plugin;
39
use Time::HiRes qw(gettimeofday tv_interval);
40
my $ret = undef;
41

    
42
need_multigraph();
43

    
44
sub clean {
45
	my $surl=shift;
46
	$surl=~s/^https?:\/\///;
47
	$surl=~s|%[\w\d]|_|g;
48
	$surl=~s|[^\w\d_]|_|g;
49
	$surl=~s|_*$||g;
50
	$surl=~s|^_*||g;
51
	return $surl;
52
};
53

    
54
if (! eval "require LWP::UserAgent;")
55
{
56
	$ret = "LWP::UserAgent not found";
57
        if ( ! defined $ARGV[0] ) {
58
                die $ret;
59
        }
60
}
61

    
62
my %URLS;
63

    
64
# timeout in seconds for requests
65
# slightly lower than the default global timeout (5 seconds)
66
my $timeout = $ENV{'timeout'} || 3;
67

    
68
for (my $i = 1; $ENV{"url$i"}; $i++)
69
{
70
	my $url   = $ENV{"url$i"};
71
	my $proxy = $ENV{"url${i}_proxy"};
72
	my $name  = $ENV{"url${i}_name"}  || clean($url);
73
	my $label = $ENV{"url${i}_label"} || $url;
74
	my $agent = $ENV{"url${i}_agent"};
75

    
76
	$URLS{$name}={
77
		url=>$url,
78
		proxy=>$proxy,
79
		label=>$label,
80
		agent=>$agent,
81
		time=>'U'
82
	};
83
}
84

    
85
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" )
86
{
87
	if ($ret)
88
	{
89
		print "no ($ret)\n";
90
		exit 0;
91
	}
92

    
93
	my $ua = LWP::UserAgent->new(timeout => $timeout);
94

    
95
	foreach my $url (keys %URLS) {
96
		my $response = $ua->request(HTTP::Request->new('GET',$URLS{$url}{'url'}));
97
                if ($response->is_success) {
98
                	next;
99
                }
100
                else {
101
                        print "no (URL $url: ". $response->message .")\n";
102
                        exit 0;
103
                }
104
	}
105
	print "yes\n";
106
	exit 0;
107
}
108

    
109
if ( defined $ARGV[0] and $ARGV[0] eq "config" )
110
{
111
	# master graph
112
	print "multigraph http_request_time\n";
113
	print "graph_title HTTP(S) Request response times\n";
114
	print "graph_args --base 1000\n";
115
	print "graph_vlabel response time in ms\n";
116
	print "graph_category webserver\n";
117

    
118
	my @go;
119
	foreach my $name (keys %URLS) {
120
		my $url = $URLS{$name};
121
		print "$name.label $$url{'label'}\n";
122
		print "$name.info The response time of a single request\n";
123
		print "$name.min 0\n";
124
		print "$name.draw LINE1\n";
125
		push(@go, $name);
126
	}
127

    
128
	# multigraphs
129

    
130
	foreach my $name (keys %URLS) {
131
		my $url = $URLS{$name};
132
		print "\nmultigraph http_request_time.$name\n";
133
		print "graph_title $$url{'url'}\n";
134
		print "graph_args --base 1000\n";
135
		print "graph_vlabel response time in ms\n";
136
		print "graph_category webserver\n";
137
		print "$name.label $$url{'label'}\n";
138
		print "$name.info The response time of a single request\n";
139
		print "$name.min 0\n";
140
		print "$name.draw LINE1\n";
141
	}
142

    
143
	exit 0;
144
}
145

    
146
my $ua = LWP::UserAgent->new(timeout => $timeout);
147
my $defaultAgent = $ua->agent;
148
foreach my $name (keys %URLS) {
149
	my $url = $URLS{$name};
150

    
151
	if ($url->{agent}) {
152
		$ua->agent($url->{agent});
153
	} else {
154
		$ua->agent($defaultAgent);
155
	}
156
	if ($url->{proxy}) {
157
		$ua->proxy(['http', 'ftp'], $url->{proxy});
158
	}
159
	else {
160
		$ua->proxy(['http', 'ftp'], undef);
161
	}
162

    
163
	# warm up
164
	my $response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
165

    
166
	# timed run
167
	my $t1=[gettimeofday];
168
	$response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
169
	my $t2=[gettimeofday];
170

    
171
	if ($response->is_success) {
172
		$$url{'time'}=sprintf("%d",tv_interval($t1,$t2)*1000);
173
	};
174
};
175

    
176
print("multigraph http_request_time\n");
177
foreach my $name (keys %URLS) {
178
	my $url = $URLS{$name};
179
	print("$name.value $$url{'time'}\n");
180
}
181

    
182
foreach my $name (keys %URLS) {
183
	my $url = $URLS{$name};
184
	print("\nmultigraph http_request_time.$name\n");
185
	print("$name.value $$url{'time'}\n");
186
}
187

    
188
# vim:syntax=perl