| 1 | package MYDan::Util::WhoIs; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use MYDan::Node; | ||
| 6 | use MYDan::Util::Hosts; | ||
| 7 | use MYDan::Util::OptConf; | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use MYDan::Util::WhoIs; | ||
| 12 | my $w = MYDan::Util::WhoIs->new() | ||
| 13 | |||
| 14 | $w->whois( 'hostname.abc' ); | ||
| 15 | $w->search( 'abc' ); | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ( $class, %self ) = shift; | ||
| 22 | |||
| 23 | $self{hosts} = MYDan::Util::Hosts->new(); | ||
| 24 | $self{cache} = MYDan::Node::DBI::Cache->new( | ||
| 25 | MYDan::Util::OptConf->load()->{range}{cache} ); | ||
| 26 | |||
| 27 | bless \%self, ref $class || $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub whois | ||
| 31 | { | ||
| 32 | my ( $this, $name ) = @_; | ||
| 33 | return unless $name; | ||
| 34 | |||
| 35 | my ( $cache, $hosts ) = @$this{qw( cache hosts )}; | ||
| 36 | |||
| 37 | my %d = $hosts->dump(); | ||
| 38 | |||
| 39 | my ( %i, %h ) = ( $name => 1 ); | ||
| 40 | |||
| 41 | while( 1 ) | ||
| 42 | { | ||
| 43 | my $c = keys %i; | ||
| 44 | for my $name ( grep{ $_ !~ /^\d+\.\d+\.\d+\.\d+$/ }keys %i ) | ||
| 45 | { | ||
| 46 | $name =~ s/^[nw]{1}\d*-//; | ||
| 47 | map{ $i{$_} = 1 if $_ eq $name || $_ =~ /^[nw]{1}\d*-$name$/ }$hosts->hosts(); | ||
| 48 | } | ||
| 49 | |||
| 50 | while( my @d = each %d ) | ||
| 51 | { | ||
| 52 | if( grep{ $_ eq $d[0] || $_ eq $d[1] }keys %i ) | ||
| 53 | { | ||
| 54 | map{$i{$_} = 1}@d; | ||
| 55 | $h{$d[0]} = $d[1]; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | last if $c == keys %i; | ||
| 60 | } | ||
| 61 | |||
| 62 | return ( cluster => [ $cache->select( '*', node => [ 1 => keys %i ] ) ], hosts => \%h ); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub search | ||
| 66 | { | ||
| 67 | my ( $this, $name ) = @_; | ||
| 68 | return unless $name; | ||
| 69 | |||
| 70 | my ( $cache, $hosts ) = @$this{qw( cache hosts )}; | ||
| 71 | |||
| 72 | my ( %d, %h ) = $hosts->dump(); | ||
| 73 | while( my @d = each %d ) | ||
| 74 | { | ||
| 75 | $h{$d[0]} = $d[1] if grep{ $_ =~ /$name/ }@d; | ||
| 76 | } | ||
| 77 | |||
| 78 | return ( cluster => [ grep{$_->[2] =~ /$name/ }$cache->select( '*' ) ], hosts => \%h ); | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; |