MMM/Host.pm

filter: Statementstmt 51.0% 48 / 94 Branchbran 17.5% 7 / 40 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 66.6% 14 / 21 Podpod 100% 13 / 13 totaltotal 43.7% 84 / 192 CC 29 SCAR 28.5
CC
29
Cov
38%
get_hostiploc
40.9
get_geo
30.0
get_dnsloc
30.0
CRAP 17.3
« MMM/Report.pm ↑ Summary MMM/Mirror.pm »
1 package MMM::Host;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 MMM::Host
13  
14 =head1 DESCRIPTION
15  
16 An object to retain host information
17  
18 =head1 METHODS
19  
20 =head2 new
21  
22 Create a MMM::Host object from information found in hash passed
23 as arguments.
24  
25 my $mirror MMM::Mirror->new( host => 'host.domain' );
26  
27 =cut
28  
29 12 ▶sub _rev {
Subroutine _rev: called CC=1 SCAR=0.0
30 12 strftime( '%Y%m%d%H%M%S', gmtime(time) );
31 }
32  
33 13 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
34 13 my ( $class, %infos ) = @_;
35 13 ▶ $infos{hostname} or return;
Branch: 1 branch
BranchTrueFalseTotal
$infos{"hostname"} or return 12 1 13
36 12 $infos{hostname} = lc( $infos{hostname} );
37 12 ▶ $infos{revision} ||= _rev();
Condition33%1/3
$infos{"revision"} ||= _rev()
l!l&&r!l&&!r
0120
MC/DC0%0/2
$infos{"revision"} ||= _rev()
$infos{"revision"} _rev()
Truth table33%1/3
A ||= B
A= $infos{"revision"}B= _rev()
ABresult
1X1
011
000
38 12 ▶ if ( $infos{geolocation} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infos{"geolocation"}) 5 7 12
39 ( $infos{longitude}, $infos{latitude} ) =
40 5 $infos{geolocation} =~ /([\d\.]+),([\d\.]+)/;
41 }
42 12 bless( {%infos}, $class );
43 }
44  
45 =head2 hostname
46  
47 Return the hostname of the host
48  
49 =cut
50  
51 1 ▶sub hostname {
Subroutine hostname: called CC=1 SCAR=0.0
Pod documented
52 1 my ($self) = @_;
53 1 $self->{hostname};
54 }
55  
56 =head2 revision
57  
58 Return the revision of the entry. The revision is an id to identify if an
59 entry is newer than another for same mirror.
60  
61 =cut
62  
63 2 ▶sub revision {
Subroutine revision: called CC=1 SCAR=0.0
Pod documented
64 2 my ($self) = @_;
65 2 $self->{revision};
66 }
67  
68 =head2 refresh_revision
69  
70 Reset revision to current timestamp
71  
72 =cut
73  
74 0 ▶sub refresh_revision {
Subroutine refresh_revision: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($self) = @_;
76 0 $self->{revision} = _rev();
77 }
78  
79 =head2 geo
80  
81 Return the latitude and the longitude for this host
82  
83 =cut
84  
85 14 ▶sub geo {
Subroutine geo: called CC=1 SCAR=0.0
Pod documented
86 14 return ( $_[0]->{latitude}, $_[0]->{longitude} );
87 }
88  
89 =head2 get_geo
90  
91 Try to use various method to find latitude and longitude
92 and return them
93  
94 =cut
95  
96 0 ▶sub get_geo {
Subroutine get_geo: not called CC=5 SCAR=34.0
Pod documented
97 0 ▶ if ( !$_[0]->{get_geo_done} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]{"get_geo_done"}) 0 0 0
98 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]{"latitude"} and defined $_[0]{"longitude"}) 0 0 0
Condition0%0/3
defined $_[0]{"latitude"} and defined $_[0]{"longitude"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0]{"latitude"} and defined $_[0]{"longitude"}
defined $_[0]{"latitude"} defined $_[0]{"longitude"}
Truth table0%0/3
A and B
A= defined $_[0]{"latitude"}B= defined $_[0]{"longitude"}
ABresult
0X0
100
111
99 !( defined( $_[0]->{latitude} ) && defined( $_[0]->{longitude} ) ) )
100 {
101 0 ▶ $_[0]->get_dnsloc || $_[0]->get_hostiploc;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->get_dnsloc or $_[0]->get_hostiploc 0 0 0
102 }
103 0 $_[0]->{get_geo_done} = 1;
104 }
105 0 return $_[0]->geo;
106 }
107  
108 =head2 get_hostiploc
109  
110 Get and set information from hostip.info website
111  
112 =cut
113  
114 0 ▶sub get_hostiploc {
Subroutine get_hostiploc: not called CC=7 SCAR=37.1
Pod documented
115 0 my ($self) = @_;
116  
117 0 ▶ my ( $name, $aliases, $addrtype, $length, $paddr, @addrs ) =
Branch: 1 branch
BranchTrueFalseTotal
() = gethostbyname $self->hostname or return 0 0 0
118 gethostbyname( $self->hostname )
119 or return;
120 0 my $addr = join( '.', unpack( 'C4', $paddr ) );
121 6 ▶ use WWW::HostipInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 0 my $hostip = new WWW::HostipInfo;
123 0 ▶ my $info = $hostip->get_info($addr) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $hostip->get_info($addr) or return 0 0 0
124 0 ▶ $self->{latitude} = $info->latitude
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->latitude 0 0 0
125 if ( defined( $info->latitude ) );
126 0 ▶ $self->{longitude} = $info->longitude
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->longitude 0 0 0
127 if ( defined( $info->longitude ) );
128 0 ▶ $self->{country} = $info->country_name
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->country_name 0 0 0
129 if ( defined( $info->country_name ) );
130 0 ▶ $self->{city} = $info->city if ( defined( $info->city ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->city 0 0 0
131 0 $self->refresh_revision;
132  
133 0 1;
134 }
135  
136 =head2 get_dnsloc
137  
138 Try to find geolocalisation from DNS LOC record
139  
140 =cut
141  
142 0 ▶sub get_dnsloc {
Subroutine get_dnsloc: not called CC=5 SCAR=34.0
Pod documented
143 0 my ($self) = @_;
144 0 return;
145 0 my @partname = split( /\./, $self->hostname );
146 0 my $dnsq = Net::DNS::Resolver->new();
147 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
shift @partname and do { do { my $answer = $dnsq->query(join(".", @partname), "LOC") or return; foreach my $ans ($answer->answer) { if ($ans->type eq "LOC") { ($self->{"latitude"}, $self->{"longitude"}) = $ans->latlon; $self->refresh_revision; return 1; } ; } } } 0 0 0
148 0 ▶ my $answer = $dnsq->query( join( '.', @partname ), 'LOC' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $answer = $dnsq->query(join(".", @partname), "LOC") or return 0 0 0
149 0 foreach my $ans ( $answer->answer ) {
150 0 ▶ if ( $ans->type eq 'LOC' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ans->type eq "LOC") 0 0 0
151 0 ( $self->{latitude}, $self->{longitude} ) = $ans->latlon();
152 0 $self->refresh_revision;
153 0 return 1;
154 }
155 }
156 } while ( shift(@partname) );
157  
158 0 return;
159 }
160  
161 =head2 set_geo($latitude, $longitude)
162  
163 Set the geolocalisation for this host
164  
165 =cut
166  
167 0 ▶sub set_geo {
Subroutine set_geo: not called CC=1 SCAR=6.9
Pod documented
168 0 my ( $self, $lat, $lon ) = @_;
169 0 ( $self->{latitude}, $self->{longitude} ) = ( $lat, $lon );
170 }
171  
172 =head2 distance( $host )
173  
174 Calcule the distance (angle in degrees) to another host
175  
176 =cut
177  
178 3 ▶sub distance {
Subroutine distance: called CC=2 SCAR=7.0
Pod documented
179 3 my ( $self, $host ) = @_;
180 3 ▶ grep { !defined($_) } ($self->geo, $host->geo) and return;
Branch: 1 branch
BranchTrueFalseTotal
grep {not defined $_;} $self->geo, $host->geo and return 0 3 3
181 3 my ( $lat1, $lon1 ) = map { deg2rad($_) } $self->geo;
182 3 my ( $lat2, $lon2 ) = map { deg2rad($_) } $host->geo;
183 3 rad2deg(
184 acos(
185 sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) *
186 cos( $lon1 - $lon2 )
187 )
188 );
189 }
190  
191 =head2 same_host($host)
192  
193 Compare two host entry and return true if they identify the same
194 computer
195  
196 =cut
197  
198 0 ▶sub same_host {
Subroutine same_host: not called CC=2 SCAR=17.9
Pod documented
199 0 my ( $self, $host ) = @_;
200 0 ▶ if ( $self->hostname eq $host->hostname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hostname eq $host->hostname) 0 0 0
201 0 return 1;
202 }
203 0 return;
204 }
205  
206 =head2 sync_host($host)
207  
208 Get unknown values from $host if defined.
209  
210 =cut
211  
212 1 ▶sub sync_host {
Subroutine sync_host: called CC=6 SCAR=23.5
Pod documented
213 1 my ( $self, $host ) = @_;
214 1 foreach (qw(city continent country latitude longiture)) {
215 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{$_} or defined $host->{$_} and $host->revision > $self->revision) 5 0 5
Condition16%1/6
not defined $self->{$_} or defined $host->{$_} and $host->revision > $self->revision
l!l&&r!l&&!r
500
defined $host->{$_} and $host->revision > $self->revision
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $self->{$_} or defined $host->{$_} and $host->revision > $self->revision
not defined $self->{$_} defined $host->{$_} $host->revision > $self->revision
Truth table25%1/4
not(A) or B and C
A= not defined $self->{$_}B= defined $host->{$_}C= $host->revision > $self->revision
ABCresult
1XX1
0111
00X0
0100
216 ( !defined( $self->{$_} ) )
217 || ( defined( $host->{$_} )
218 && $host->revision > $self->revision )
219 )
220 {
221 5 $self->{$_} = $host->{$_};
222 }
223 }
224  
225 1 ▶ if ( $host->revision > $self->revision ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host->revision > $self->revision) 0 1 1
226 0 $self->{revision} = $host->{revision};
227 }
228 }
229  
230 =head2 xml_output
231  
232 Return a xml string describing this mirror.
233  
234 See also <MMM::MirrorList::xml_output>
235  
236 =cut
237  
238 0 ▶sub xml_output {
Subroutine xml_output: not called CC=5 SCAR=34.0
Pod documented
239 0 my ($self) = @_;
240 0 my $xml = "\t\t<host>\n";
241  
242 0 foreach (qw(hostname continent country city revision)) {
243 0 ▶ if ( $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$_}) 0 0 0
244 0 $xml .= sprintf( "\t\t\t<%s>%s</%s>\n", $_, $self->{$_}, $_ );
245 }
246 }
247  
248 0 ▶ if ( defined( $self->{latitude} ) && defined( $self->{longitude} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"latitude"} and defined $self->{"longitude"}) 0 0 0
Condition0%0/3
defined $self->{"latitude"} and defined $self->{"longitude"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"latitude"} and defined $self->{"longitude"}
defined $self->{"latitude"} defined $self->{"longitude"}
Truth table0%0/3
A and B
A= defined $self->{"latitude"}B= defined $self->{"longitude"}
ABresult
0X0
100
111
249 0 $xml .=
250 "\t\t\t<geolocation>$self->{longitude},$self->{latitude}</geolocation>\n";
251 }
252  
253 0 $xml .= "\t\t</host>\n";
254  
255 0 $xml;
256 }
257  
258 6 1;
259  
260 =head1 AUTHOR
261  
262 Olivier Thauvin <nanardon@nanardon.zarb.org>
263  
264 =head1 COPYRIGHT AND LICENSE
265  
266 Copyright (C) 2006 Olivier Thauvin
267  
268 This program is free software; you can redistribute it and/or
269 modify it under the terms of the GNU General Public License
270 as published by the Free Software Foundation; either version 2
271 of the License, or (at your option) any later version.
272  
273 This program is distributed in the hope that it will be useful,
274 but WITHOUT ANY WARRANTY; without even the implied warranty of
275 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
276 GNU General Public License for more details.
277  
278 You should have received a copy of the GNU General Public License
279 along with this program; if not, write to the Free Software
280 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
281  
282 =cut
283  
« MMM/Report.pm ↑ Summary MMM/Mirror.pm »