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