blib/lib/MYDan/Util/NTPSync.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 86 CC 8 SCAR 29.4
CC
8
Cov
0%
new
42.0
run
12.0
BEGIN
2.0
CRAP 18.9
1 package MYDan::Util::NTPSync;
2  
3 =head1 NAME
4  
5 MYDan::Util::NTPSync
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Util::NTPSync;
10  
11 =cut
12  
13 use strict;
14 use warnings;
15  
16 use YAML::XS;
17 use Sys::Hostname;
18 use Carp;
19 use MYDan::Util::Alias;
20  
21 our $NTPDATE;
22  
23 BEGIN{
24 my $alias = MYDan::Util::Alias->new();
25 $NTPDATE = $alias->alias( 'ntpdate' ) || 'ntpdate';
26 };
27  
28 sub new
29 {
30 my ( $class, %self ) = @_;
31 confess "no ntpsync conf" unless exists $self{conf};
32 my $conf = eval{ YAML::XS::LoadFile( $self{conf} ) };
33 confess "$self{conf}: $@\n" if $@;
34 confess "conf is not hash" if ref $conf ne 'HASH';
35  
36 my $hostname = Sys::Hostname::hostname;
37 map { push @{ $self{ntp} }, @{ $conf->{$_} } if $hostname =~ /$_/ }
38 sort { length($b) <=> length($a) } keys %$conf;
39 confess "no match ntpserver" unless exists $self{ntp};
40  
41 bless \%self, ref $class || $class;
42 }
43  
44 sub run
45 {
46 my $self = shift;
47  
48 my ( @ntp, $ok ) = @{ $self->{ntp} };
49 do
50 {
51 system '/etc/init.d/ntpd stop > /dev/null 2>&1';
52  
53 for my $ntp (@ntp)
54 {
55 map {
56 unless ( system "$NTPDATE $ntp" ) { $ok = 1; last; }
57 } 1 .. $self->{try};
58 warn "Failed to sync with $ntp\n";
59 }
60  
61 warn sprintf "sync %s.\n", $ok ? 'ok' : 'fail';
62 } while $self->{daemon} && sleep $self->{interval};
63  
64 }
65  
66 1;