blib/lib/MYDan/Collector/Stat/Http.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 112 CC 11 SCAR 28.2
CC
11
Cov
0%
co
30.0
trans
30.0
BEGIN
12.0
CRAP 16.8
1 package MYDan::Collector::Stat::Http;
2  
3 use strict;
4 use warnings;
5 use Carp;
6 use POSIX;
7  
8 use LWP::UserAgent;
9 use Encode;
10  
11 use Data::Dumper;
12 use MYDan::Collector::Util;
13  
14 #retry:3:time:3:host:lvscheck.xitong.mydan.net:proxy:http=127.0.0.1=9999:http://localhost:8080
15 #http://iface:eth0:8080
16  
17 my %iface;
18 BEGIN{
19 my $tmp;
20 map{
21 $tmp = $1 if $_ =~ /^(\S+)/;
22 $iface{$tmp} = $1 if $tmp && $_ =~ /\baddr:(\d+\.\d+\.\d+\.\d+)\b/;
23 }MYDan::Collector::Util::qx( 'ifconfig' );
24 };
25  
26 my %option = ( 'time' => 5, retry => 1 );
27  
28 sub co
29 {
30 my ( $this, @http, @stat, %http ) = shift;
31  
32 map{ $http{$1} = 1 if $_ =~ /^\{HTTP\}\{([^}]+)\}/ }@_;
33  
34 push @http, [ 'HTTP', 'code', 'is_success', 'status_line', 'cont' ];
35  
36 for ( keys %http )
37 {
38 my %opt = trans( $_ );
39  
40 my ( $url, $retry, $res ) = map{ delete $opt{$_} }qw( url retry );
41  
42 my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } );
43 $ua->proxy( [ 'http', 'https'], "$1://$2:$3" )
44 if $opt{proxy} && $opt{proxy} =~ /^(\w+)=([^:]+)=(\d+)/;
45  
46 $ua->agent('Mozilla/9 [en] (Centos; Linux)');
47
48 $ua->timeout( $opt{time} );
49  
50 $ua->default_header ( 'Cache-control' => 'no-cache', 'Pragma' => 'no-cache', %opt );
51  
52 for( 1 .. $retry )
53 {
54 $res = $ua->get( $url );
55 last if $res->is_success;
56 }
57 my $cont = decode( 'utf8', $res->is_success ? $res->content : '' );
58 push @http, [ $_, $res->code, $res->is_success, $res->status_line, $cont ];
59  
60 }
61  
62 return \@http;
63 }
64  
65 sub trans
66 {
67 my ( $url, %opt ) = shift;
68  
69 if( $url =~ /^(.*)http(.+)/ )
70 {
71 %opt = split /:/, $1;
72 $opt{url} = "http$2";
73 }
74 else { $opt{url} = "http://$url" }
75  
76 $opt{url} =~ s/iface:(\w+)/$iface{$1}/
77 if $opt{url} =~ /http\w*:\/\/iface:(\w+)\b/ && $iface{$1};
78  
79 map{ $opt{$_} = $option{$_} unless $opt{$_} }keys %option;
80  
81 $opt{Host} = $opt{host} if $opt{host};
82 return %opt;
83 }
84  
85 1;