Projet

Général

Profil

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

root / plugins / http / http_request_time @ 942bda31

Historique | Voir | Annoter | Télécharger (3,03 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.url   http://127.0.0.1/1 http://127.0.0.1/2 http://127.0.0.1/3
14

    
15
=head1 MAGIC MARKERS
16

    
17
  #%# family=auto
18
  #%# capabilities=autoconf
19

    
20
=head1 LICENSE
21

    
22
GPLv2
23

    
24
=cut
25

    
26
use strict;
27
use warnings;
28
use Munin::Plugin;
29
use Time::HiRes qw(gettimeofday tv_interval);
30
my $ret = undef;
31

    
32
need_multigraph();
33

    
34
sub clean {
35
	my $surl=shift;
36
	$surl=~s/^https?:\/\///;
37
	$surl=~s|%[\w\d]|_|g;
38
	$surl=~s|[^\w\d_]|_|g;
39
	$surl=~s|_*$||g;
40
	$surl=~s|^_*||g;
41
	return $surl;
42
};
43

    
44

    
45
if (! eval "require LWP::UserAgent;")
46
{
47
	$ret = "LWP::UserAgent not found";
48
        if ( ! defined $ARGV[0] ) {
49
                die $ret;
50
        }
51
}
52

    
53
my $URL = $ENV{'url'}?$ENV{'url'}:"http://127.0.0.1/";
54
my %URLS;
55
foreach $_ (split(/ /,$URL)){
56
	$URLS{$_}={
57
		url=>$_,
58
		surl=>clean($_),
59
		time=>'U'
60
	};
61
}
62

    
63
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" )
64
{
65
	if ($ret)
66
	{
67
		print "no ($ret)\n";
68
		exit 0;
69
	}
70

    
71
	my $ua = LWP::UserAgent->new(timeout => 30);
72

    
73
	foreach my $url (keys %URLS) {
74
		my $response = $ua->request(HTTP::Request->new('GET',$url));
75
                if ($response->is_success) {
76
                	next;
77
                }
78
                else {
79
                        print "no (URL $url: ". $response->message .")\n";
80
                        exit 0;
81
                }
82
	}
83
	print "yes\n";
84
	exit 0;
85
}
86

    
87
if ( defined $ARGV[0] and $ARGV[0] eq "config" )
88
{
89
	# master graph
90
	print "multigraph http_request_time\n";
91
	print "graph_title HTTP(S) Request response times\n";
92
	print "graph_args --base 1000\n";
93
	print "graph_vlabel response time in ms\n";
94
	print "graph_category other\n";
95

    
96
	my @go;
97
	foreach my $url (values %URLS) {
98
		print "$$url{'surl'}.label $$url{'url'}\n";
99
		print "$$url{'surl'}.info The response time of a single request\n";
100
		print "$$url{'surl'}.min 0\n";
101
		print "$$url{'surl'}.draw LINE1\n";
102
		push(@go,$$url{'surl'});
103
	}
104

    
105
	# multigraphs
106

    
107
	foreach my $url (values %URLS) {
108
		print "\nmultigraph http_request_time.$$url{'surl'}\n";
109
		print "graph_title $$url{'url'}\n";
110
		print "graph_args --base 1000\n";
111
		print "graph_vlabel response time in ms\n";
112
		print "graph_category other\n";
113
		print "$$url{'surl'}.label $$url{'url'}\n";
114
		print "$$url{'surl'}.info The response time of a single request\n";
115
		print "$$url{'surl'}.min 0\n";
116
		print "$$url{'surl'}.draw LINE1\n";
117
	}
118

    
119
	exit 0;
120
}
121

    
122
my $ua = LWP::UserAgent->new(timeout => 15);
123

    
124
foreach my $url (values %URLS) {
125
	my $t1=[gettimeofday];
126
	my $response = $ua->request(HTTP::Request->new('GET',$$url{'url'}));
127
	my $t2=[gettimeofday];
128
	if ($response->is_success) {
129
		$$url{'time'}=sprintf("%d",tv_interval($t1,$t2)*1000);
130
	};		
131
};
132

    
133
print("multigraph http_request_time\n");
134
foreach my $url (values %URLS) {
135
	print("$$url{'surl'}.value $$url{'time'}\n");
136
}
137
foreach my $url (values %URLS) {
138
	print("\nmultigraph http_request_time.$$url{'surl'}\n");
139
	print("$$url{'surl'}.value $$url{'time'}\n");
140
}
141

    
142

    
143
# vim:syntax=perl