| 1 | package MYDan::Collector::Stat::Port; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use POSIX; | ||
| 7 | |||
| 8 | use IO::Select; | ||
| 9 | use IO::Socket::INET; | ||
| 10 | |||
| 11 | #use Net::SSL; | ||
| 12 | use Data::Dumper; | ||
| 13 | use MYDan::Collector::Util; | ||
| 14 | |||
| 15 | #retry:3:time:3:input:hello:tcp:22 | ||
| 16 | |||
| 17 | my %option = ( 'time' => 5, retry => 1, host => '127.0.0.1' ); | ||
| 18 | my %iface; | ||
| 19 | BEGIN{ | ||
| 20 | my $tmp; | ||
| 21 | map{ | ||
| 22 | $tmp = $1 if $_ =~ /^(\S+)/; | ||
| 23 | $iface{$tmp} = $1 if $tmp && $_ =~ /\baddr:(\d+\.\d+\.\d+\.\d+)\b/; | ||
| 24 | |||
| 25 | }MYDan::Collector::Util::qx( 'ifconfig' ); | ||
| 26 | }; | ||
| 27 | |||
| 28 | sub co | ||
| 29 | { | ||
| 30 | my ( $this, @port, %port ) = shift; | ||
| 31 | |||
| 32 | map{ $port{$1} = 1 if $_ =~ /^\{PORT\}\{([^}]+)\}/ }@_; | ||
| 33 | |||
| 34 | push @port, [ 'PORT', 'status', 'output' ]; | ||
| 35 | |||
| 36 | for ( keys %port ) | ||
| 37 | { | ||
| 38 | my ( %opt, $status, $output ) = trans( $_ ); | ||
| 39 | |||
| 40 | for( 1 .. $opt{retry} ) | ||
| 41 | { | ||
| 42 | if( defined $opt{tcp} ) | ||
| 43 | { | ||
| 44 | ( $status, $output ) = tcp( %opt ); | ||
| 45 | } | ||
| 46 | else | ||
| 47 | { | ||
| 48 | $output = ''; | ||
| 49 | $status = MYDan::Collector::Util::system( "nc -u -z -w $opt{time} $opt{host} $opt{udp} 1>/dev/null 2>&1" ) | ||
| 50 | ? 'down' : 'alive'; | ||
| 51 | } | ||
| 52 | |||
| 53 | last if $status eq 'alive'; | ||
| 54 | } | ||
| 55 | |||
| 56 | push @port, [ $_, $status, $output ]; | ||
| 57 | } | ||
| 58 | |||
| 59 | return \@port; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub trans | ||
| 63 | { | ||
| 64 | my ( $url, %opt ) = shift; | ||
| 65 | |||
| 66 | if( $url =~ /tcp|udp/ ) | ||
| 67 | { %opt = split /:/, $url;} | ||
| 68 | else { $opt{tcp} = $url; } | ||
| 69 | |||
| 70 | map{ $opt{$_} = $option{$_} unless $opt{$_}; }keys %option; | ||
| 71 | |||
| 72 | $opt{host} = $iface{$opt{host}} if $iface{$opt{host}}; | ||
| 73 | return %opt; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub tcp | ||
| 77 | { | ||
| 78 | my %param = @_; | ||
| 79 | |||
| 80 | my ( $time, $input, $host, $tcp ) = @param{ qw( time input host tcp ) }; | ||
| 81 | |||
| 82 | my $sock = IO::Socket::INET->new( | ||
| 83 | Blocking => 0, Timeout => $time, | ||
| 84 | Proto => 'tcp', Type => SOCK_STREAM, | ||
| 85 | PeerAddr => "$host:$tcp", | ||
| 86 | ); | ||
| 87 | |||
| 88 | return ( "socket: $!", '' ) unless $sock; | ||
| 89 | |||
| 90 | my $output = ''; | ||
| 91 | $sock->send( $input ) if defined $input; | ||
| 92 | $sock->shutdown( 1 ); | ||
| 93 | my $select = IO::Select->new(); | ||
| 94 | $select->add( $sock ); | ||
| 95 | |||
| 96 | while(1) | ||
| 97 | { | ||
| 98 | my @ready = $select->can_read( $time ); | ||
| 99 | unless( scalar @ready ) | ||
| 100 | { | ||
| 101 | eval { $sock->shutdown( 2 ) }; | ||
| 102 | return ( "alive", $output ); | ||
| 103 | } | ||
| 104 | |||
| 105 | my $fh = shift @ready; | ||
| 106 | my $tmp = <$fh>; | ||
| 107 | if( $tmp ) { $output .= $tmp; } | ||
| 108 | else | ||
| 109 | { | ||
| 110 | $select->remove( $fh ); | ||
| 111 | close( $fh ); | ||
| 112 | last; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | eval { $sock->shutdown( 2 ) }; | ||
| 117 | |||
| 118 | return ( 'alive', $output ); | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; |