Projet

Général

Profil

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

root / plugins / http / http_request_time @ 17f78427

Historique | Voir | Annoter | Télécharger (3,79 ko)

1 0ca8ce20 Stefan Osterlitz
#!/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 17f78427 Lars Kruse
8 0ca8ce20 Stefan Osterlitz
	LWP::UserAgent and Time::HiRes are required
9
10
=head1 CONFIGURATION
11
12
  [http_request_time]
13 5b56bd29 Kenyon Ralph
  env.url1  http://127.0.0.1/1
14 8b1e467b Chris Wilson
  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 b693de85 Antoine Beaupré
  env.timeout 3
20
21
Timeout is the timeout of any HTTP request. Tune to avoid a complete
22
timeout of the plugin.
23 0ca8ce20 Stefan Osterlitz
24
=head1 MAGIC MARKERS
25
26
  #%# family=auto
27
  #%# capabilities=autoconf
28
29
=head1 LICENSE
30
31
GPLv2
32
33
=cut
34
35
use strict;
36
use warnings;
37
use Munin::Plugin;
38
use Time::HiRes qw(gettimeofday tv_interval);
39
my $ret = undef;
40
41
need_multigraph();
42
43
sub clean {
44
	my $surl=shift;
45
	$surl=~s/^https?:\/\///;
46
	$surl=~s|%[\w\d]|_|g;
47
	$surl=~s|[^\w\d_]|_|g;
48
	$surl=~s|_*$||g;
49
	$surl=~s|^_*||g;
50
	return $surl;
51
};
52
53
if (! eval "require LWP::UserAgent;")
54
{
55
	$ret = "LWP::UserAgent not found";
56
        if ( ! defined $ARGV[0] ) {
57
                die $ret;
58
        }
59
}
60
61
my %URLS;
62 8b1e467b Chris Wilson
63 2ae2cd69 Antoine Beaupré
# timeout in seconds for requests
64 6994c89d Antoine Beaupré
# slightly lower than the default global timeout (5 seconds)
65
my $timeout = $ENV{'timeout'} || 3;
66 2ae2cd69 Antoine Beaupré
67 8b1e467b Chris Wilson
for (my $i = 1; $ENV{"url$i"}; $i++)
68
{
69
	my $url   = $ENV{"url$i"};
70
	my $proxy = $ENV{"url${i}_proxy"};
71
	my $name  = $ENV{"url${i}_name"}  || clean($url);
72
	my $label = $ENV{"url${i}_label"} || $url;
73 17f78427 Lars Kruse
74 8b1e467b Chris Wilson
	$URLS{$name}={
75
		url=>$url,
76
		proxy=>$proxy,
77
		label=>$label,
78 0ca8ce20 Stefan Osterlitz
		time=>'U'
79
	};
80
}
81
82
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" )
83
{
84
	if ($ret)
85
	{
86
		print "no ($ret)\n";
87
		exit 0;
88
	}
89
90 2ae2cd69 Antoine Beaupré
	my $ua = LWP::UserAgent->new(timeout => $timeout);
91 0ca8ce20 Stefan Osterlitz
92
	foreach my $url (keys %URLS) {
93
		my $response = $ua->request(HTTP::Request->new('GET',$url));
94
                if ($response->is_success) {
95
                	next;
96
                }
97
                else {
98
                        print "no (URL $url: ". $response->message .")\n";
99
                        exit 0;
100
                }
101
	}
102
	print "yes\n";
103
	exit 0;
104
}
105
106
if ( defined $ARGV[0] and $ARGV[0] eq "config" )
107
{
108
	# master graph
109
	print "multigraph http_request_time\n";
110
	print "graph_title HTTP(S) Request response times\n";
111
	print "graph_args --base 1000\n";
112
	print "graph_vlabel response time in ms\n";
113 62560fae dipohl
	print "graph_category webserver\n";
114 0ca8ce20 Stefan Osterlitz
115
	my @go;
116 8b1e467b Chris Wilson
	foreach my $name (keys %URLS) {
117
		my $url = $URLS{$name};
118
		print "$name.label $$url{'label'}\n";
119
		print "$name.info The response time of a single request\n";
120
		print "$name.min 0\n";
121
		print "$name.draw LINE1\n";
122
		push(@go, $name);
123 0ca8ce20 Stefan Osterlitz
	}
124
125
	# multigraphs
126
127 8b1e467b Chris Wilson
	foreach my $name (keys %URLS) {
128
		my $url = $URLS{$name};
129
		print "\nmultigraph http_request_time.$name\n";
130 0ca8ce20 Stefan Osterlitz
		print "graph_title $$url{'url'}\n";
131
		print "graph_args --base 1000\n";
132
		print "graph_vlabel response time in ms\n";
133 62560fae dipohl
		print "graph_category webserver\n";
134 8b1e467b Chris Wilson
		print "$name.label $$url{'label'}\n";
135
		print "$name.info The response time of a single request\n";
136
		print "$name.min 0\n";
137
		print "$name.draw LINE1\n";
138 0ca8ce20 Stefan Osterlitz
	}
139
140
	exit 0;
141
}
142
143 2ae2cd69 Antoine Beaupré
my $ua = LWP::UserAgent->new(timeout => $timeout);
144 8b1e467b Chris Wilson
foreach my $name (keys %URLS) {
145
	my $url = $URLS{$name};
146 0ca8ce20 Stefan Osterlitz
147 8b1e467b Chris Wilson
	if ($url->{proxy}) {
148
		$ua->proxy(['http', 'ftp'], $url->{proxy});
149
	}
150
	else {
151
		$ua->proxy(['http', 'ftp'], undef);
152
	}
153
154
	# warm up
155 0ca8ce20 Stefan Osterlitz
	my $response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
156 8b1e467b Chris Wilson
157
	# timed run
158
	my $t1=[gettimeofday];
159
	$response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
160 0ca8ce20 Stefan Osterlitz
	my $t2=[gettimeofday];
161 8b1e467b Chris Wilson
162 0ca8ce20 Stefan Osterlitz
	if ($response->is_success) {
163
		$$url{'time'}=sprintf("%d",tv_interval($t1,$t2)*1000);
164 17f78427 Lars Kruse
	};
165 0ca8ce20 Stefan Osterlitz
};
166
167
print("multigraph http_request_time\n");
168 8b1e467b Chris Wilson
foreach my $name (keys %URLS) {
169
	my $url = $URLS{$name};
170
	print("$name.value $$url{'time'}\n");
171 0ca8ce20 Stefan Osterlitz
}
172
173 8b1e467b Chris Wilson
foreach my $name (keys %URLS) {
174
	my $url = $URLS{$name};
175
	print("\nmultigraph http_request_time.$name\n");
176
	print("$name.value $$url{'time'}\n");
177
}
178 0ca8ce20 Stefan Osterlitz
179
# vim:syntax=perl