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
#!/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.timeout 3
20

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

    
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

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

    
67
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

    
74
	$URLS{$name}={
75
		url=>$url,
76
		proxy=>$proxy,
77
		label=>$label,
78
		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
	my $ua = LWP::UserAgent->new(timeout => $timeout);
91

    
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
	print "graph_category webserver\n";
114

    
115
	my @go;
116
	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
	}
124

    
125
	# multigraphs
126

    
127
	foreach my $name (keys %URLS) {
128
		my $url = $URLS{$name};
129
		print "\nmultigraph http_request_time.$name\n";
130
		print "graph_title $$url{'url'}\n";
131
		print "graph_args --base 1000\n";
132
		print "graph_vlabel response time in ms\n";
133
		print "graph_category webserver\n";
134
		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
	}
139

    
140
	exit 0;
141
}
142

    
143
my $ua = LWP::UserAgent->new(timeout => $timeout);
144
foreach my $name (keys %URLS) {
145
	my $url = $URLS{$name};
146

    
147
	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
	my $response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
156

    
157
	# timed run
158
	my $t1=[gettimeofday];
159
	$response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
160
	my $t2=[gettimeofday];
161

    
162
	if ($response->is_success) {
163
		$$url{'time'}=sprintf("%d",tv_interval($t1,$t2)*1000);
164
	};
165
};
166

    
167
print("multigraph http_request_time\n");
168
foreach my $name (keys %URLS) {
169
	my $url = $URLS{$name};
170
	print("$name.value $$url{'time'}\n");
171
}
172

    
173
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

    
179
# vim:syntax=perl