USEFUL perl SCRIPT: NTP verifier
USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 17:44:08 von Ignoramus5390
Over the years, I have had many problems with NTPd stopping to work
without any notice. That always upset me. I finally wrote a perl
script that checks times among various servers and computes a time
offset for each of them. It can either report that, or it can also set
exit code based on whether all these offsets are below a user
specified limit.
To run it, say query-ntp host1 host2 etc
A good practice is to specify localhost as host1, pool.ntp.org as
host2, and whatever other machines you are checking, would follow
after that.
i
############################################################ ##########
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict; use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
);
my @servers = @ARGV;
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#----------------------------------------------------------- -
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0; # $h{'Originate Timestamp'};
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
#print "T4=$T4\n";
my $d = ($T4 - $T1) - ($T2 - $T3);
my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$d, offset=$t\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$d, offset=$t, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $t );
}
}
}
exit 1 if $bad;
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 18:21:40 von John Bokma
Ignoramus5390 wrote:
> use strict; use warnings;
You might want each on a line of its own.
>
> use Data::Dumper;
> use Getopt::Long;
> use Net::NTP;
> use Time::HiRes qw( time );
>
> my $detail = undef;
> my $limit = undef;
undef is the default value, I wouldn't assign it explicitly.
> GetOptions(
> "detail!" => \$detail,
> "limit=f" => \$limit,
> );
>
> my @servers = @ARGV;
you might want to do some checking here, and report a usage message.
Also, I recommend to support at least -h / --help.
> foreach my $server (@servers) {
> my $t0 = time;
> my %h = get_ntp_response( $server );
>
>
> #Timestamp Name ID When Generated
> #----------------------------------------------------------- -
> #Originate Timestamp T1 time request sent by client
> #Receive Timestamp T2 time request received by server
> #Transmit Timestamp T3 time reply sent by server
> #Destination Timestamp T4 time reply received by client
If you need to explain what IDs mean it clearly shows that the names of
your variables are not chosen well.
> #
> #The roundtrip delay d and local clock offset t are defined as
> #
> #d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
>
> my $T1 = $t0; # $h{'Originate Timestamp'};
^^^^ remove commented out cruft
> my $T2 = $h{'Receive Timestamp'};
> my $T3 = $h{'Transmit Timestamp'};
>
> my $T4 = time; # From Time::HiRes! Accurate to usec!
>
> #print "T4=$T4\n";
don't leave cruft in like that. If you need debugging, use a --debug
option, and use something like:
$debug and print ....
> my $d = ($T4 - $T1) - ($T2 - $T3);
my $roundtrip_delay =
> my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $local_clock_offset = ...
> my $duration = $T4-$t0;
> my $delta = $T2-$T1 - $duration/2;
>
> if( $limit ) {
> if( $delta > $limit || -$delta < -$limit) {
> print "Server $server OFF by $delta seconds!!!\n";
> $bad = 1;
>
> } else {
> print "Server $server OK, delta is $delta seconds!!!\n";
> }
>
> } else {
Note that the else here makes that $detail and $limit are mutual
exclusive. Sounds odd to me. But if that's what you want, instead of
setting $bad to 1, and do a test at the end, you could just have used:
die "Server .... OFF ... \n";
note that die also sets conveniently the exit value...
> if( $detail ) {
> print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$d,
> offset=$t\nelapsed=$duration, delta=$delta\n\n";
> } else {
> #print "$server delay=$d, offset=$t, delta=$delta,
> duration=$duration\n"; print sprintf( "%-30s %.5f\n", $server,
> $t );
> }
> }
> }
>
> exit 1 if $bad;
--
John Bokma http://johnbokma.com/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 18:21:40 von John Bokma
Ignoramus5390 wrote:
> use strict; use warnings;
You might want each on a line of its own.
>
> use Data::Dumper;
> use Getopt::Long;
> use Net::NTP;
> use Time::HiRes qw( time );
>
> my $detail = undef;
> my $limit = undef;
undef is the default value, I wouldn't assign it explicitly.
> GetOptions(
> "detail!" => \$detail,
> "limit=f" => \$limit,
> );
>
> my @servers = @ARGV;
you might want to do some checking here, and report a usage message.
Also, I recommend to support at least -h / --help.
> foreach my $server (@servers) {
> my $t0 = time;
> my %h = get_ntp_response( $server );
>
>
> #Timestamp Name ID When Generated
> #----------------------------------------------------------- -
> #Originate Timestamp T1 time request sent by client
> #Receive Timestamp T2 time request received by server
> #Transmit Timestamp T3 time reply sent by server
> #Destination Timestamp T4 time reply received by client
If you need to explain what IDs mean it clearly shows that the names of
your variables are not chosen well.
> #
> #The roundtrip delay d and local clock offset t are defined as
> #
> #d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
>
> my $T1 = $t0; # $h{'Originate Timestamp'};
^^^^ remove commented out cruft
> my $T2 = $h{'Receive Timestamp'};
> my $T3 = $h{'Transmit Timestamp'};
>
> my $T4 = time; # From Time::HiRes! Accurate to usec!
>
> #print "T4=$T4\n";
don't leave cruft in like that. If you need debugging, use a --debug
option, and use something like:
$debug and print ....
> my $d = ($T4 - $T1) - ($T2 - $T3);
my $roundtrip_delay =
> my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $local_clock_offset = ...
> my $duration = $T4-$t0;
> my $delta = $T2-$T1 - $duration/2;
>
> if( $limit ) {
> if( $delta > $limit || -$delta < -$limit) {
> print "Server $server OFF by $delta seconds!!!\n";
> $bad = 1;
>
> } else {
> print "Server $server OK, delta is $delta seconds!!!\n";
> }
>
> } else {
Note that the else here makes that $detail and $limit are mutual
exclusive. Sounds odd to me. But if that's what you want, instead of
setting $bad to 1, and do a test at the end, you could just have used:
die "Server .... OFF ... \n";
note that die also sets conveniently the exit value...
> if( $detail ) {
> print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$d,
> offset=$t\nelapsed=$duration, delta=$delta\n\n";
> } else {
> #print "$server delay=$d, offset=$t, delta=$delta,
> duration=$duration\n"; print sprintf( "%-30s %.5f\n", $server,
> $t );
> }
> }
> }
>
> exit 1 if $bad;
--
John Bokma http://johnbokma.com/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 18:45:06 von Ignoramus5390
On 2008-01-14, John Bokma wrote:
> Ignoramus5390 wrote:
>
>
>> use strict; use warnings;
>
> You might want each on a line of its own.
ok
>>
>> use Data::Dumper;
>> use Getopt::Long;
>> use Net::NTP;
>> use Time::HiRes qw( time );
>>
>> my $detail = undef;
>> my $limit = undef;
>
> undef is the default value, I wouldn't assign it explicitly.
I like this style. Makes it very explicit.
>> GetOptions(
>> "detail!" => \$detail,
>> "limit=f" => \$limit,
>> );
>>
>> my @servers = @ARGV;
>
> you might want to do some checking here, and report a usage message.
done
> Also, I recommend to support at least -h / --help.
Is there some standard way of doing it?
>> foreach my $server (@servers) {
>> my $t0 = time;
>> my %h = get_ntp_response( $server );
>>
>>
>> #Timestamp Name ID When Generated
>> #----------------------------------------------------------- -
>> #Originate Timestamp T1 time request sent by client
>> #Receive Timestamp T2 time request received by server
>> #Transmit Timestamp T3 time reply sent by server
>> #Destination Timestamp T4 time reply received by client
>
> If you need to explain what IDs mean it clearly shows that the names of
> your variables are not chosen well.
They come from NTP specification.
>
>> #
>> #The roundtrip delay d and local clock offset t are defined as
>> #
>> #d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
>>
>> my $T1 = $t0; # $h{'Originate Timestamp'};
>
> ^^^^ remove commented out cruft
>
>
>> my $T2 = $h{'Receive Timestamp'};
>> my $T3 = $h{'Transmit Timestamp'};
>>
>> my $T4 = time; # From Time::HiRes! Accurate to usec!
>>
>> #print "T4=$T4\n";
>
> don't leave cruft in like that. If you need debugging, use a --debug
> option, and use something like:
>
> $debug and print ....
ok
>> my $d = ($T4 - $T1) - ($T2 - $T3);
>
> my $roundtrip_delay =
>
>> my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
>
> my $local_clock_offset = ...
changed
>> my $duration = $T4-$t0;
>> my $delta = $T2-$T1 - $duration/2;
>>
>> if( $limit ) {
>> if( $delta > $limit || -$delta < -$limit) {
>> print "Server $server OFF by $delta seconds!!!\n";
>> $bad = 1;
>>
>> } else {
>> print "Server $server OK, delta is $delta seconds!!!\n";
>> }
>>
>> } else {
>
> Note that the else here makes that $detail and $limit are mutual
> exclusive. Sounds odd to me. But if that's what you want, instead of
> setting $bad to 1, and do a test at the end, you could just have used:
>
> die "Server .... OFF ... \n";
I want all bad servers reported, hence delayed dying.
> note that die also sets conveniently the exit value...
Yes, but I want to be a little clear since I want to go to the end.
############################################################ ##########
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
);
die "detail and limit are mutually exclusive" if $detail and $limit;
my @servers = @ARGV;
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#----------------------------------------------------------- -
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0;
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
my $local_clock_offset = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay, offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$roundtrip_delay, offset=$local_clock_offset, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
}
}
}
exit 1 if $bad;
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 18:45:06 von Ignoramus5390
On 2008-01-14, John Bokma wrote:
> Ignoramus5390 wrote:
>
>
>> use strict; use warnings;
>
> You might want each on a line of its own.
ok
>>
>> use Data::Dumper;
>> use Getopt::Long;
>> use Net::NTP;
>> use Time::HiRes qw( time );
>>
>> my $detail = undef;
>> my $limit = undef;
>
> undef is the default value, I wouldn't assign it explicitly.
I like this style. Makes it very explicit.
>> GetOptions(
>> "detail!" => \$detail,
>> "limit=f" => \$limit,
>> );
>>
>> my @servers = @ARGV;
>
> you might want to do some checking here, and report a usage message.
done
> Also, I recommend to support at least -h / --help.
Is there some standard way of doing it?
>> foreach my $server (@servers) {
>> my $t0 = time;
>> my %h = get_ntp_response( $server );
>>
>>
>> #Timestamp Name ID When Generated
>> #----------------------------------------------------------- -
>> #Originate Timestamp T1 time request sent by client
>> #Receive Timestamp T2 time request received by server
>> #Transmit Timestamp T3 time reply sent by server
>> #Destination Timestamp T4 time reply received by client
>
> If you need to explain what IDs mean it clearly shows that the names of
> your variables are not chosen well.
They come from NTP specification.
>
>> #
>> #The roundtrip delay d and local clock offset t are defined as
>> #
>> #d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
>>
>> my $T1 = $t0; # $h{'Originate Timestamp'};
>
> ^^^^ remove commented out cruft
>
>
>> my $T2 = $h{'Receive Timestamp'};
>> my $T3 = $h{'Transmit Timestamp'};
>>
>> my $T4 = time; # From Time::HiRes! Accurate to usec!
>>
>> #print "T4=$T4\n";
>
> don't leave cruft in like that. If you need debugging, use a --debug
> option, and use something like:
>
> $debug and print ....
ok
>> my $d = ($T4 - $T1) - ($T2 - $T3);
>
> my $roundtrip_delay =
>
>> my $t = (($T2 - $T1) + ($T3 - $T4)) / 2;
>
> my $local_clock_offset = ...
changed
>> my $duration = $T4-$t0;
>> my $delta = $T2-$T1 - $duration/2;
>>
>> if( $limit ) {
>> if( $delta > $limit || -$delta < -$limit) {
>> print "Server $server OFF by $delta seconds!!!\n";
>> $bad = 1;
>>
>> } else {
>> print "Server $server OK, delta is $delta seconds!!!\n";
>> }
>>
>> } else {
>
> Note that the else here makes that $detail and $limit are mutual
> exclusive. Sounds odd to me. But if that's what you want, instead of
> setting $bad to 1, and do a test at the end, you could just have used:
>
> die "Server .... OFF ... \n";
I want all bad servers reported, hence delayed dying.
> note that die also sets conveniently the exit value...
Yes, but I want to be a little clear since I want to go to the end.
############################################################ ##########
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
);
die "detail and limit are mutually exclusive" if $detail and $limit;
my @servers = @ARGV;
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#----------------------------------------------------------- -
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0;
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
my $local_clock_offset = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay, offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$roundtrip_delay, offset=$local_clock_offset, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
}
}
}
exit 1 if $bad;
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:10:25 von John Bokma
Ignoramus5390 wrote:
> On 2008-01-14, John Bokma wrote:
[..]
>> undef is the default value, I wouldn't assign it explicitly.
>
> I like this style. Makes it very explicit.
IMO Perl programmers should know that the default is undef, and hence no
need to assign undef. To confuse matters a bit more (IMO), you set $bad
to undef, yet you assign later on 1 to it (in case of an error), and at
the end of the script you test if $bad .... I try to avoid testing for
undef that way, and I try to avoid to use 1/undef as a true/false. In
short, I would've used something like:
my $server_limit_out_of_range = 0;
:
:
if ....
$server_limit_out_of_range = 1;
:
:
$server_limit_out_of_range and exit 1;
ditto for $detail (which is a flag).
As for limit, I probably would pick a sensible default instead of undef.
>> Also, I recommend to support at least -h / --help.
>
> Is there some standard way of doing it?
Perl Best Practices (recommended) recommends:
--usage - one line
--help - --usage line followed by one-line
summary of each option (in my experience
the one-line is to strict)
--version
--man - complete documentation of the program
>> If you need to explain what IDs mean it clearly shows that the names
>> of your variables are not chosen well.
>
> They come from NTP specification.
OK, clear. That's something you've read to write this, but someone who
reads your program does not always have to be familiar with this
documentation, nor has to read it first IMO.
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
v.s.
my $roundtrip_delay = ( $destination_ts - $originate_ts )
- ( $receive_ts - $transmit_ts );
which is more readable?
>> Note that the else here makes that $detail and $limit are mutual
>> exclusive. Sounds odd to me. But if that's what you want, instead of
>> setting $bad to 1, and do a test at the end, you could just have
>> used:
>>
>> die "Server .... OFF ... \n";
>
> I want all bad servers reported, hence delayed dying.
My bad, I missed the additional }
In that case I probably would have used (warnings go to STDERR):
warn "Server ... \n";
next;
}
to keep it functionally the same. OTOH, I can't see why $limit and
$detail have to be mutually exclusive.
> if( $limit ) {
> if( $delta > $limit || -$delta < -$limit) {
if ( abs( $delta ) > $limit ) {
^ note the extra space, /if/ is not a function
--
John
http://johnbokma.com/mexit/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:10:25 von John Bokma
Ignoramus5390 wrote:
> On 2008-01-14, John Bokma wrote:
[..]
>> undef is the default value, I wouldn't assign it explicitly.
>
> I like this style. Makes it very explicit.
IMO Perl programmers should know that the default is undef, and hence no
need to assign undef. To confuse matters a bit more (IMO), you set $bad
to undef, yet you assign later on 1 to it (in case of an error), and at
the end of the script you test if $bad .... I try to avoid testing for
undef that way, and I try to avoid to use 1/undef as a true/false. In
short, I would've used something like:
my $server_limit_out_of_range = 0;
:
:
if ....
$server_limit_out_of_range = 1;
:
:
$server_limit_out_of_range and exit 1;
ditto for $detail (which is a flag).
As for limit, I probably would pick a sensible default instead of undef.
>> Also, I recommend to support at least -h / --help.
>
> Is there some standard way of doing it?
Perl Best Practices (recommended) recommends:
--usage - one line
--help - --usage line followed by one-line
summary of each option (in my experience
the one-line is to strict)
--version
--man - complete documentation of the program
>> If you need to explain what IDs mean it clearly shows that the names
>> of your variables are not chosen well.
>
> They come from NTP specification.
OK, clear. That's something you've read to write this, but someone who
reads your program does not always have to be familiar with this
documentation, nor has to read it first IMO.
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
v.s.
my $roundtrip_delay = ( $destination_ts - $originate_ts )
- ( $receive_ts - $transmit_ts );
which is more readable?
>> Note that the else here makes that $detail and $limit are mutual
>> exclusive. Sounds odd to me. But if that's what you want, instead of
>> setting $bad to 1, and do a test at the end, you could just have
>> used:
>>
>> die "Server .... OFF ... \n";
>
> I want all bad servers reported, hence delayed dying.
My bad, I missed the additional }
In that case I probably would have used (warnings go to STDERR):
warn "Server ... \n";
next;
}
to keep it functionally the same. OTOH, I can't see why $limit and
$detail have to be mutually exclusive.
> if( $limit ) {
> if( $delta > $limit || -$delta < -$limit) {
if ( abs( $delta ) > $limit ) {
^ note the extra space, /if/ is not a function
--
John
http://johnbokma.com/mexit/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:36:37 von Toby A Inkster
Ignoramus5390 wrote:
> my @servers = @ARGV;
die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
unless ($#servers > 1);
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 15 days, 5:47.]
NetSol Cybersquatting
http://tobyinkster.co.uk/blog/2008/01/10/netsol-cybersquatti ng/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:36:37 von Toby A Inkster
Ignoramus5390 wrote:
> my @servers = @ARGV;
die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
unless ($#servers > 1);
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 15 days, 5:47.]
NetSol Cybersquatting
http://tobyinkster.co.uk/blog/2008/01/10/netsol-cybersquatti ng/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:52:19 von Dan Espen
Ignoramus5390 writes:
> Over the years, I have had many problems with NTPd stopping to work
> without any notice. That always upset me.
Me too, but I took a different approach.
In root's crontab:
35 * * * * /root/scripts/ntp-check.sh
Content of script:
x=`/etc/rc.d/init.d/ntpd status`
case $x in
*stopped*|*dead*)
status=`/etc/rc.d/init.d/ntpd start 2>&1`
printf "had to start ntpd on `uname -n`, state <$x>\
\nrestart got status\n $status" | mail -s cron root
;;
esac
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:52:19 von Dan Espen
Ignoramus5390 writes:
> Over the years, I have had many problems with NTPd stopping to work
> without any notice. That always upset me.
Me too, but I took a different approach.
In root's crontab:
35 * * * * /root/scripts/ntp-check.sh
Content of script:
x=`/etc/rc.d/init.d/ntpd status`
case $x in
*stopped*|*dead*)
status=`/etc/rc.d/init.d/ntpd start 2>&1`
printf "had to start ntpd on `uname -n`, state <$x>\
\nrestart got status\n $status" | mail -s cron root
;;
esac
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:57:24 von Ignoramus5390
On 2008-01-14, Dan Espen wrote:
> Ignoramus5390 writes:
>
>> Over the years, I have had many problems with NTPd stopping to work
>> without any notice. That always upset me.
>
> Me too, but I took a different approach.
> In root's crontab:
>
> 35 * * * * /root/scripts/ntp-check.sh
>
> Content of script:
>
> x=`/etc/rc.d/init.d/ntpd status`
> case $x in
> *stopped*|*dead*)
> status=`/etc/rc.d/init.d/ntpd start 2>&1`
> printf "had to start ntpd on `uname -n`, state <$x>\
> \nrestart got status\n $status" | mail -s cron root
> ;;
> esac
Yeah, and what happens when it is alive, but does not talk to any
timeservers?
i
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 19:57:24 von Ignoramus5390
On 2008-01-14, Dan Espen wrote:
> Ignoramus5390 writes:
>
>> Over the years, I have had many problems with NTPd stopping to work
>> without any notice. That always upset me.
>
> Me too, but I took a different approach.
> In root's crontab:
>
> 35 * * * * /root/scripts/ntp-check.sh
>
> Content of script:
>
> x=`/etc/rc.d/init.d/ntpd status`
> case $x in
> *stopped*|*dead*)
> status=`/etc/rc.d/init.d/ntpd start 2>&1`
> printf "had to start ntpd on `uname -n`, state <$x>\
> \nrestart got status\n $status" | mail -s cron root
> ;;
> esac
Yeah, and what happens when it is alive, but does not talk to any
timeservers?
i
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:03:23 von Ignoramus5390
On 2008-01-14, Toby A Inkster wrote:
> Ignoramus5390 wrote:
>
>> my @servers = @ARGV;
>
> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
> unless ($#servers > 1);
>
Thanks. I changed it to:
die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
unless (@servers);
Thank you
i
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:03:23 von Ignoramus5390
On 2008-01-14, Toby A Inkster wrote:
> Ignoramus5390 wrote:
>
>> my @servers = @ARGV;
>
> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
> unless ($#servers > 1);
>
Thanks. I changed it to:
die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
unless (@servers);
Thank you
i
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:12:31 von John Bokma
Toby A Inkster wrote:
> Ignoramus5390 wrote:
>
>> my @servers = @ARGV;
>
> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
> unless ($#servers > 1);
@servers > 2 or die "......";
(which I read as a precondition for the code that follows: there must be
more than 2 servers, otherwise ... )
It's a bit unclear to me why at least 3 hosts must be given (contradicting
the usage line):
items scalar @servers >2 $#servers > 1
0 0 f -1 f
1 1 f 0 f
2 2 f 1 f
3 3 t 2 t
(additional note:
there is no need for () after the unless (i.e. when used as a
statement modifier)
)
--
John
Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1. html
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:12:31 von John Bokma
Toby A Inkster wrote:
> Ignoramus5390 wrote:
>
>> my @servers = @ARGV;
>
> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
> unless ($#servers > 1);
@servers > 2 or die "......";
(which I read as a precondition for the code that follows: there must be
more than 2 servers, otherwise ... )
It's a bit unclear to me why at least 3 hosts must be given (contradicting
the usage line):
items scalar @servers >2 $#servers > 1
0 0 f -1 f
1 1 f 0 f
2 2 f 1 f
3 3 t 2 t
(additional note:
there is no need for () after the unless (i.e. when used as a
statement modifier)
)
--
John
Arachnids near Coyolillo - part 1
http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1. html
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:26:37 von Ignoramus5390
On 2008-01-14, John Bokma wrote:
> Toby A Inkster wrote:
>
>> Ignoramus5390 wrote:
>>
>>> my @servers = @ARGV;
>>
>> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
>> unless ($#servers > 1);
>
>
> @servers > 2 or die "......";
>
> (which I read as a precondition for the code that follows: there must be
> more than 2 servers, otherwise ... )
>
I am confused about all this. One server supplied on command line is
enough to do useful tests. Just one, not two or three.
ie
query-ntp --limit 1 www.mydomain.com
would test that the clock of mydomain.com is in sync with localhost.
or
query-ntp pool.ntp.org
would compare localhost with atomic clock.
I do not see why there is a need for requiring the user to supply more
than one server.
i
> It's a bit unclear to me why at least 3 hosts must be given (contradicting
> the usage line):
>
> items scalar @servers >2 $#servers > 1
> 0 0 f -1 f
> 1 1 f 0 f
> 2 2 f 1 f
> 3 3 t 2 t
>
>
>
> (additional note:
>
> there is no need for () after the unless (i.e. when used as a
> statement modifier)
> )
>
>
>
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:26:37 von Ignoramus5390
On 2008-01-14, John Bokma wrote:
> Toby A Inkster wrote:
>
>> Ignoramus5390 wrote:
>>
>>> my @servers = @ARGV;
>>
>> die "Usage: query-ntp [--detail] [--limit=N] host1 host2 [host3 ...]\n"
>> unless ($#servers > 1);
>
>
> @servers > 2 or die "......";
>
> (which I read as a precondition for the code that follows: there must be
> more than 2 servers, otherwise ... )
>
I am confused about all this. One server supplied on command line is
enough to do useful tests. Just one, not two or three.
ie
query-ntp --limit 1 www.mydomain.com
would test that the clock of mydomain.com is in sync with localhost.
or
query-ntp pool.ntp.org
would compare localhost with atomic clock.
I do not see why there is a need for requiring the user to supply more
than one server.
i
> It's a bit unclear to me why at least 3 hosts must be given (contradicting
> the usage line):
>
> items scalar @servers >2 $#servers > 1
> 0 0 f -1 f
> 1 1 f 0 f
> 2 2 f 1 f
> 3 3 t 2 t
>
>
>
> (additional note:
>
> there is no need for () after the unless (i.e. when used as a
> statement modifier)
> )
>
>
>
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:31:42 von John Bokma
Ignoramus5390 wrote:
> I am confused about all this. One server supplied on command line is
> enough to do useful tests. Just one, not two or three.
Me too, I would use something like:
@servers
or die "usage: ... query-ntp [--detail] [--limit=N] host [host ... ]
moreover, if you're supporting --usage, you might want to put the message
in a sub:
@servers or show_usage_and_die();
:
sub show_usage {
die ....
}
--
John Bokma http://johnbokma.com/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:31:42 von John Bokma
Ignoramus5390 wrote:
> I am confused about all this. One server supplied on command line is
> enough to do useful tests. Just one, not two or three.
Me too, I would use something like:
@servers
or die "usage: ... query-ntp [--detail] [--limit=N] host [host ... ]
moreover, if you're supporting --usage, you might want to put the message
in a sub:
@servers or show_usage_and_die();
:
sub show_usage {
die ....
}
--
John Bokma http://johnbokma.com/
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:41:25 von Ignoramus5390
On 2008-01-14, John Bokma wrote:
> Ignoramus5390 wrote:
>
>> I am confused about all this. One server supplied on command line is
>> enough to do useful tests. Just one, not two or three.
>
> Me too, I would use something like:
>
> @servers
> or die "usage: ... query-ntp [--detail] [--limit=N] host [host ... ]
>
> moreover, if you're supporting --usage, you might want to put the message
> in a sub:
>
>
> @servers or show_usage_and_die();
>
>:
>
> sub show_usage {
>
> die ....
> }
>
>
here's my new version, incorporating usage. It will be my new pattern
to follow:
############################################################ ##########
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
my $help = undef;
sub usage {
my ($msg) = @_;
print "ERROR: $msg\n\n";
print STDERR "Usage: $0 [--detail] [--limit=N] host {host}\n";
exit ($msg ? 1 : 0);
}
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
"help!" => \$help,
);
my @servers = @ARGV;
usage if $help;
usage "detail and limit are mutually exclusive" if $detail and $limit;
usage "You need to supply one or more servers" unless (@servers);
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#----------------------------------------------------------- -
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0;
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
my $local_clock_offset = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay, offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$roundtrip_delay, offset=$local_clock_offset, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
}
}
}
exit 1 if $bad;
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:41:25 von Ignoramus5390
On 2008-01-14, John Bokma wrote:
> Ignoramus5390 wrote:
>
>> I am confused about all this. One server supplied on command line is
>> enough to do useful tests. Just one, not two or three.
>
> Me too, I would use something like:
>
> @servers
> or die "usage: ... query-ntp [--detail] [--limit=N] host [host ... ]
>
> moreover, if you're supporting --usage, you might want to put the message
> in a sub:
>
>
> @servers or show_usage_and_die();
>
>:
>
> sub show_usage {
>
> die ....
> }
>
>
here's my new version, incorporating usage. It will be my new pattern
to follow:
############################################################ ##########
#!/usr/bin/perl
# See Also:
# http://www.webreference.com/programming/perl/ntp/
# http://www.webreference.com/programming/perl/ntp/2.html
#
# Copyright(C) Igor Chudov, 2008.
# Released under GNU Public License version 3.
#
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Net::NTP;
use Time::HiRes qw( time );
my $detail = undef;
my $limit = undef;
my $help = undef;
sub usage {
my ($msg) = @_;
print "ERROR: $msg\n\n";
print STDERR "Usage: $0 [--detail] [--limit=N] host {host}\n";
exit ($msg ? 1 : 0);
}
GetOptions(
"detail!" => \$detail,
"limit=f" => \$limit,
"help!" => \$help,
);
my @servers = @ARGV;
usage if $help;
usage "detail and limit are mutually exclusive" if $detail and $limit;
usage "You need to supply one or more servers" unless (@servers);
my $bad = undef;
foreach my $server (@servers) {
my $t0 = time;
my %h = get_ntp_response( $server );
#Timestamp Name ID When Generated
#----------------------------------------------------------- -
#Originate Timestamp T1 time request sent by client
#Receive Timestamp T2 time request received by server
#Transmit Timestamp T3 time reply sent by server
#Destination Timestamp T4 time reply received by client
#
#The roundtrip delay d and local clock offset t are defined as
#
#d = (T4 - T1) - (T2 - T3) t = ((T2 - T1) + (T3 - T4)) / 2
my $T1 = $t0;
my $T2 = $h{'Receive Timestamp'};
my $T3 = $h{'Transmit Timestamp'};
my $T4 = time; # From Time::HiRes! Accurate to usec!
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
my $local_clock_offset = (($T2 - $T1) + ($T3 - $T4)) / 2;
my $duration = $T4-$t0;
my $delta = $T2-$T1 - $duration/2;
if( $limit ) {
if( $delta > $limit || -$delta < -$limit) {
print "Server $server OFF by $delta seconds!!!\n";
$bad = 1;
} else {
print "Server $server OK, delta is $delta seconds!!!\n";
}
} else {
if( $detail ) {
print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay, offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
} else {
#print "$server delay=$roundtrip_delay, offset=$local_clock_offset, delta=$delta, duration=$duration\n";
print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
}
}
}
exit 1 if $bad;
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:44:36 von Dan Espen
Ignoramus5390 writes:
> On 2008-01-14, Dan Espen wrote:
>> Ignoramus5390 writes:
>>
>>> Over the years, I have had many problems with NTPd stopping to work
>>> without any notice. That always upset me.
>>
>> Me too, but I took a different approach.
>> In root's crontab:
>>
>> 35 * * * * /root/scripts/ntp-check.sh
>>
>> Content of script:
>>
>> x=`/etc/rc.d/init.d/ntpd status`
>> case $x in
>> *stopped*|*dead*)
>> status=`/etc/rc.d/init.d/ntpd start 2>&1`
>> printf "had to start ntpd on `uname -n`, state <$x>\
>> \nrestart got status\n $status" | mail -s cron root
>> ;;
>> esac
>
> Yeah, and what happens when it is alive, but does not talk to any
> timeservers?
The only time I ever saw it exit,
it lost communications to the time server.
Reading the man page, I think that's what it is supposed to do.
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 20:44:36 von Dan Espen
Ignoramus5390 writes:
> On 2008-01-14, Dan Espen wrote:
>> Ignoramus5390 writes:
>>
>>> Over the years, I have had many problems with NTPd stopping to work
>>> without any notice. That always upset me.
>>
>> Me too, but I took a different approach.
>> In root's crontab:
>>
>> 35 * * * * /root/scripts/ntp-check.sh
>>
>> Content of script:
>>
>> x=`/etc/rc.d/init.d/ntpd status`
>> case $x in
>> *stopped*|*dead*)
>> status=`/etc/rc.d/init.d/ntpd start 2>&1`
>> printf "had to start ntpd on `uname -n`, state <$x>\
>> \nrestart got status\n $status" | mail -s cron root
>> ;;
>> esac
>
> Yeah, and what happens when it is alive, but does not talk to any
> timeservers?
The only time I ever saw it exit,
it lost communications to the time server.
Reading the man page, I think that's what it is supposed to do.
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 21:29:06 von Martien Verbruggen
On 14 Jan 2008 18:10:25 GMT,
John Bokma wrote:
> Ignoramus5390 wrote:
>
>> On 2008-01-14, John Bokma wrote:
>>> If you need to explain what IDs mean it clearly shows that the names
>>> of your variables are not chosen well.
>>
>> They come from NTP specification.
>
> OK, clear. That's something you've read to write this, but someone who
> reads your program does not always have to be familiar with this
> documentation, nor has to read it first IMO.
>
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
>
> v.s.
>
> my $roundtrip_delay = ( $destination_ts - $originate_ts )
> - ( $receive_ts - $transmit_ts );
>
> which is more readable?
To me the first one is more readable. While long variable names are good
in many ways, sometimes they hinder the easy parsing of expressions,
because too much wrap and distance gets in the way.
For me it's much easier to see what the expression does int he first
instance than in the second, even if the precise meaning of each
variable is not that clear.
Especially, also, given that these short variables are 'standard'
terminology, I see no problem with them.
Martien
--
|
Martien Verbruggen | I think there is a world market for maybe
| five computers. -- Thomas Watson, chairman of
| IBM, 1943
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 21:29:06 von Martien Verbruggen
On 14 Jan 2008 18:10:25 GMT,
John Bokma wrote:
> Ignoramus5390 wrote:
>
>> On 2008-01-14, John Bokma wrote:
>>> If you need to explain what IDs mean it clearly shows that the names
>>> of your variables are not chosen well.
>>
>> They come from NTP specification.
>
> OK, clear. That's something you've read to write this, but someone who
> reads your program does not always have to be familiar with this
> documentation, nor has to read it first IMO.
>
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
>
> v.s.
>
> my $roundtrip_delay = ( $destination_ts - $originate_ts )
> - ( $receive_ts - $transmit_ts );
>
> which is more readable?
To me the first one is more readable. While long variable names are good
in many ways, sometimes they hinder the easy parsing of expressions,
because too much wrap and distance gets in the way.
For me it's much easier to see what the expression does int he first
instance than in the second, even if the precise meaning of each
variable is not that clear.
Especially, also, given that these short variables are 'standard'
terminology, I see no problem with them.
Martien
--
|
Martien Verbruggen | I think there is a world market for maybe
| five computers. -- Thomas Watson, chairman of
| IBM, 1943
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 23:57:49 von Michele Dondi
On 14 Jan 2008 18:10:25 GMT, John Bokma wrote:
>>> undef is the default value, I wouldn't assign it explicitly.
>>
>> I like this style. Makes it very explicit.
>
>IMO Perl programmers should know that the default is undef, and hence no
>need to assign undef. To confuse matters a bit more (IMO), you set $bad
FWIW, I wholeheartedly second that.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: USEFUL perl SCRIPT: NTP verifier
am 14.01.2008 23:57:49 von Michele Dondi
On 14 Jan 2008 18:10:25 GMT, John Bokma wrote:
>>> undef is the default value, I wouldn't assign it explicitly.
>>
>> I like this style. Makes it very explicit.
>
>IMO Perl programmers should know that the default is undef, and hence no
>need to assign undef. To confuse matters a bit more (IMO), you set $bad
FWIW, I wholeheartedly second that.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:22:02 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:22:02 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:23:42 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:23:42 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:27:58 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:27:58 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:43:50 von Jim Gibson
In article , Gary L.
Burnore wrote:
> On Mon, 14 Jan 2008 13:41:25 -0600, Ignoramus5390
> wrote:
>
> >here's my new version, incorporating usage. It will be my new pattern
> >to follow:
> >########################################################### ###########
>
[some code snipped]
> >
> >my $detail = undef;
> >my $limit = undef;
> >my $help = undef;
> >
[more code snipped]
> > if( $detail ) {
> > print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay,
> > offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
> > } else {
> > #print "$server delay=$roundtrip_delay, offset=$local_clock_offset,
> > delta=$delta, duration=$duration\n";
> > print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
> > }
> > }
> >}
> >
> >exit 1 if $bad;
>
> ... frankly, missing spaces
> between = symbols is most annoying.
There are spaces around each '=' in every assignment statement. The
only '=' without surrounding spaces are in print statements, which
appear to be debug statements. I do the same thing in my code --
snuggle up the preceding and succeeding characters in a print statement
-- to see if there is any whitespace in the variables I am printing.
Readability of the output is not the issue; it is done for accuracy of
results.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:43:50 von Jim Gibson
In article , Gary L.
Burnore wrote:
> On Mon, 14 Jan 2008 13:41:25 -0600, Ignoramus5390
> wrote:
>
> >here's my new version, incorporating usage. It will be my new pattern
> >to follow:
> >########################################################### ###########
>
[some code snipped]
> >
> >my $detail = undef;
> >my $limit = undef;
> >my $help = undef;
> >
[more code snipped]
> > if( $detail ) {
> > print Dumper( \%h ) . "\nt0=$t0, T4=$T4\ndelay=$roundtrip_delay,
> > offset=$local_clock_offset\nelapsed=$duration, delta=$delta\n\n";
> > } else {
> > #print "$server delay=$roundtrip_delay, offset=$local_clock_offset,
> > delta=$delta, duration=$duration\n";
> > print sprintf( "%-30s %.5f\n", $server, $local_clock_offset );
> > }
> > }
> >}
> >
> >exit 1 if $bad;
>
> ... frankly, missing spaces
> between = symbols is most annoying.
There are spaces around each '=' in every assignment statement. The
only '=' without surrounding spaces are in print statements, which
appear to be debug statements. I do the same thing in my code --
snuggle up the preceding and succeeding characters in a print statement
-- to see if there is any whitespace in the variables I am printing.
Readability of the output is not the issue; it is done for accuracy of
results.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:51:49 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 00:51:49 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 01:06:16 von John Bokma
Jim Gibson wrote:
> In article , Gary L.
> Burnore wrote:
>> > #print "$server delay=$roundtrip_delay,
>> > offset=$local_clock_offset, delta=$delta,
>> > duration=$duration\n"; print sprintf( "%-30s %.5f\n", $server,
>> ... frankly, missing spaces
>> between = symbols is most annoying.
>
> There are spaces around each '=' in every assignment statement. The
> only '=' without surrounding spaces are in print statements, which
> appear to be debug statements. I do the same thing in my code --
> snuggle up the preceding and succeeding characters in a print
> statement -- to see if there is any whitespace in the variables I am
> printing. Readability of the output is not the issue; it is done for
> accuracy of results.
I use either [] (mostly for debugging) or ''. Both have the added
advantage that one can see a spurious newline, e.g.
duration = [10
]
In this case, where numbers are printed, I would use extra spaces but
neither [] nor ''.
anyway, as always YMMV :-)
--
John
http://johnbokma.com/mexit/
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 01:06:16 von John Bokma
Jim Gibson wrote:
> In article , Gary L.
> Burnore wrote:
>> > #print "$server delay=$roundtrip_delay,
>> > offset=$local_clock_offset, delta=$delta,
>> > duration=$duration\n"; print sprintf( "%-30s %.5f\n", $server,
>> ... frankly, missing spaces
>> between = symbols is most annoying.
>
> There are spaces around each '=' in every assignment statement. The
> only '=' without surrounding spaces are in print statements, which
> appear to be debug statements. I do the same thing in my code --
> snuggle up the preceding and succeeding characters in a print
> statement -- to see if there is any whitespace in the variables I am
> printing. Readability of the output is not the issue; it is done for
> accuracy of results.
I use either [] (mostly for debugging) or ''. Both have the added
advantage that one can see a spurious newline, e.g.
duration = [10
]
In this case, where numbers are printed, I would use extra spaces but
neither [] nor ''.
anyway, as always YMMV :-)
--
John
http://johnbokma.com/mexit/
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 01:37:40 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 01:37:40 von unknown
Post removed (X-No-Archive: yes)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 02:10:54 von Ignoramus5390
On 2008-01-14, Jim Gibson wrote:
> There are spaces around each '=' in every assignment statement. The
> only '=' without surrounding spaces are in print statements, which
> appear to be debug statements. I do the same thing in my code --
> snuggle up the preceding and succeeding characters in a print statement
> Readability of the output is not the issue; it is done for accuracy of
> results.
>
I also do it for ease of computer parsing. Name=value pairs are very
easy top parse correctly.
i
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 02:10:54 von Ignoramus5390
On 2008-01-14, Jim Gibson wrote:
> There are spaces around each '=' in every assignment statement. The
> only '=' without surrounding spaces are in print statements, which
> appear to be debug statements. I do the same thing in my code --
> snuggle up the preceding and succeeding characters in a print statement
> Readability of the output is not the issue; it is done for accuracy of
> results.
>
I also do it for ease of computer parsing. Name=value pairs are very
easy top parse correctly.
i
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 18:39:28 von hjp-usenet2
["Followup-To:" header set to comp.lang.perl.misc.]
On 2008-01-14 18:10, John Bokma wrote:
> Ignoramus5390 wrote:
>> On 2008-01-14, John Bokma wrote:
>>> undef is the default value, I wouldn't assign it explicitly.
>>
>> I like this style. Makes it very explicit.
>
> IMO Perl programmers should know that the default is undef, and hence no
> need to assign undef.
Explicitely assigning undef does tell the reader something, though:
It means that from this point on the variable *needs* to have this value
- it isn't just undef because nobody assigned anything to it yet, it is
undef because the programmer wants it to be undef.
>>> Also, I recommend to support at least -h / --help.
>>
>> Is there some standard way of doing it?
>
> Perl Best Practices (recommended) recommends:
>
> --usage - one line
> --help - --usage line followed by one-line
> summary of each option (in my experience
> the one-line is to strict)
> --version
> --man - complete documentation of the program
The Pod::Usage module makes implementing that very simple.
>>> If you need to explain what IDs mean it clearly shows that the names
>>> of your variables are not chosen well.
>>
>> They come from NTP specification.
>
> OK, clear. That's something you've read to write this, but someone who
> reads your program does not always have to be familiar with this
> documentation, nor has to read it first IMO.
The whole comment is copied verbatim from the specs. That's something I
do, too.
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
>
> v.s.
>
> my $roundtrip_delay = ( $destination_ts - $originate_ts )
> - ( $receive_ts - $transmit_ts );
>
> which is more readable?
The first, by far. $T1 to $T4 form a logical sequence. It is completely
clear what they are. I have no idea what the second version means:
Originated by whom? Received and transmitted by whom? And why is
"destination" a noun when all others are verbs? Does that mean something
special?
(Oh, and BTW, the formula is wrong: It must be
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
RFC 2030 contains the same error - its corrected in 4330)
hp
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 18:39:28 von hjp-usenet2
["Followup-To:" header set to comp.lang.perl.misc.]
On 2008-01-14 18:10, John Bokma wrote:
> Ignoramus5390 wrote:
>> On 2008-01-14, John Bokma wrote:
>>> undef is the default value, I wouldn't assign it explicitly.
>>
>> I like this style. Makes it very explicit.
>
> IMO Perl programmers should know that the default is undef, and hence no
> need to assign undef.
Explicitely assigning undef does tell the reader something, though:
It means that from this point on the variable *needs* to have this value
- it isn't just undef because nobody assigned anything to it yet, it is
undef because the programmer wants it to be undef.
>>> Also, I recommend to support at least -h / --help.
>>
>> Is there some standard way of doing it?
>
> Perl Best Practices (recommended) recommends:
>
> --usage - one line
> --help - --usage line followed by one-line
> summary of each option (in my experience
> the one-line is to strict)
> --version
> --man - complete documentation of the program
The Pod::Usage module makes implementing that very simple.
>>> If you need to explain what IDs mean it clearly shows that the names
>>> of your variables are not chosen well.
>>
>> They come from NTP specification.
>
> OK, clear. That's something you've read to write this, but someone who
> reads your program does not always have to be familiar with this
> documentation, nor has to read it first IMO.
The whole comment is copied verbatim from the specs. That's something I
do, too.
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
>
> v.s.
>
> my $roundtrip_delay = ( $destination_ts - $originate_ts )
> - ( $receive_ts - $transmit_ts );
>
> which is more readable?
The first, by far. $T1 to $T4 form a logical sequence. It is completely
clear what they are. I have no idea what the second version means:
Originated by whom? Received and transmitted by whom? And why is
"destination" a noun when all others are verbs? Does that mean something
special?
(Oh, and BTW, the formula is wrong: It must be
my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
RFC 2030 contains the same error - its corrected in 4330)
hp
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 18:59:25 von Ignoramus25819
On 2008-01-15, Peter J. Holzer wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
> On 2008-01-14 18:10, John Bokma wrote:
>> Ignoramus5390 wrote:
>>> On 2008-01-14, John Bokma wrote:
>>>> undef is the default value, I wouldn't assign it explicitly.
>>>
>>> I like this style. Makes it very explicit.
>>
>> IMO Perl programmers should know that the default is undef, and hence no
>> need to assign undef.
>
> Explicitely assigning undef does tell the reader something, though:
> It means that from this point on the variable *needs* to have this value
> - it isn't just undef because nobody assigned anything to it yet, it is
> undef because the programmer wants it to be undef.
Yep. This also means "no, I did not forget to assign its value".
i
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 19:55:57 von John Bokma
"Peter J. Holzer" wrote:
> (Oh, and BTW, the formula is wrong: It must be
>
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
^ ^
Heh, I rest my case >:-D
--
John Bokma http://johnbokma.com/
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 20:21:52 von John Bokma
Ignoramus25819 wrote:
[ my $bad = undef;
> Yep. This also means "no, I did not forget to assign its value".
Like I already wrote YMMV, but IMO using mixed "boolean" states (undef/1)
is not done, and hence I would have avoided the whole issue by using:
my $bad = 0;
(and probably would have picked a better variable name as well: what is
bad?)
--
John
http://johnbokma.com/mexit/
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 20:57:37 von Kees Nuyt
On Tue, 15 Jan 2008 18:39:28 +0100, "Peter J. Holzer"
wrote:
>Oh, and BTW, the formula is wrong: It must be
>
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
So: my $roundtrip_delay = ($T4 - $T1) - 0;
my $roundtrip_delay = ($T4 - $T1)
?
Interesting.
--
( Kees
)
c[_] Humans believe they are devils pretending to be
angels when, in fact, the reverse is true. (#323)
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 21:51:31 von Michele Dondi
On Tue, 15 Jan 2008 18:39:28 +0100, "Peter J. Holzer"
wrote:
>(Oh, and BTW, the formula is wrong: It must be
>
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
I didn't read all the rest, but $T2 - $T2 is generally spelt... zero!
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 22:39:00 von hjp-usenet2
On 2008-01-15 18:55, John Bokma wrote:
> "Peter J. Holzer" wrote:
>
>> (Oh, and BTW, the formula is wrong: It must be
>>
>> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
> ^ ^
>
> Heh, I rest my case >:-D
>
I blame my keyboard.
Should have been
my $roundtrip_delay = ($T4 - $T1) - ($T3 - $T2);
of course.
But even the version above reduced the error by 50 % :-).
hp
Re: USEFUL perl SCRIPT: NTP verifier
am 15.01.2008 23:25:02 von melsonr
In article ,
"Peter J. Holzer" writes:
> ["Followup-To:" header set to comp.lang.perl.misc.]
> On 2008-01-14 18:10, John Bokma wrote:
>> Ignoramus5390 wrote:
>>> On 2008-01-14, John Bokma wrote:
>>>> undef is the default value, I wouldn't assign it explicitly.
>>>
>>> I like this style. Makes it very explicit.
>>
>> IMO Perl programmers should know that the default is undef, and hence no
>> need to assign undef.
>
> Explicitely assigning undef does tell the reader something, though:
> It means that from this point on the variable *needs* to have this value
> - it isn't just undef because nobody assigned anything to it yet, it is
> undef because the programmer wants it to be undef.
>
>
>>>> Also, I recommend to support at least -h / --help.
>>>
>>> Is there some standard way of doing it?
>>
>> Perl Best Practices (recommended) recommends:
>>
>> --usage - one line
>> --help - --usage line followed by one-line
>> summary of each option (in my experience
>> the one-line is to strict)
>> --version
>> --man - complete documentation of the program
>
> The Pod::Usage module makes implementing that very simple.
>
>
>>>> If you need to explain what IDs mean it clearly shows that the names
>>>> of your variables are not chosen well.
>>>
>>> They come from NTP specification.
>>
>> OK, clear. That's something you've read to write this, but someone who
>> reads your program does not always have to be familiar with this
>> documentation, nor has to read it first IMO.
>
> The whole comment is copied verbatim from the specs. That's something I
> do, too.
>
>
>> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T3);
>>
>> v.s.
>>
>> my $roundtrip_delay = ( $destination_ts - $originate_ts )
>> - ( $receive_ts - $transmit_ts );
>>
>> which is more readable?
>
> The first, by far. $T1 to $T4 form a logical sequence. It is completely
> clear what they are. I have no idea what the second version means:
> Originated by whom? Received and transmitted by whom? And why is
> "destination" a noun when all others are verbs? Does that mean something
> special?
>
> (Oh, and BTW, the formula is wrong: It must be
>
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
>
> RFC 2030 contains the same error - its corrected in 4330)
>
> hp
>
And, yet, it's a PERL mantra that TMTOWTDI (There's More
Than One Way To Do It). I think the question to ask is
whether the script/program does what it's intended to do,
not whether its style meets somebody else's idea of what
looks good or has better variable names or ...
--
Robert G. Melson | Rio Grande MicroSolutions | El Paso, Texas
-----
Thinking is the hardest work there is, which is the probable
reason so few engage in it. -- Henry Ford
Re: USEFUL perl SCRIPT: NTP verifier
am 16.01.2008 00:39:14 von John Bokma
melsonr@aragorn.rgmhome.net (Robert Melson) wrote:
> And, yet, it's a PERL mantra
You're in the wrong group, this one isn't about PERL.
> that TMTOWTDI (There's More
> Than One Way To Do It). I think the question to ask is
> whether the script/program does what it's intended to do,
> not whether its style meets somebody else's idea of what
> looks good or has better variable names or ...
Which is, like style, just an opinion. Or do you want to say that /your/
opinion is more important?
--
John
http://johnbokma.com/mexit/
Re: USEFUL perl SCRIPT: NTP verifier
am 16.01.2008 00:50:34 von Tad J McClellan
Robert Melson wrote:
> And, yet, it's a PERL mantra that TMTOWTDI
^^^^
Whoever created that PERL thingy stole the Perl mantra!
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: USEFUL perl SCRIPT: NTP verifier
am 16.01.2008 10:54:20 von Toby A Inkster
Peter J. Holzer wrote:
> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
But $T2 - $T2 = 0!
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 16 days, 21:06.]
Gnocchi all'Amatriciana al Forno
http://tobyinkster.co.uk/blog/2008/01/15/gnocchi-allamatrici ana/
Re: USEFUL perl SCRIPT: NTP verifier
am 16.01.2008 14:34:01 von Ted Zlatanov
On Wed, 16 Jan 2008 09:54:20 +0000 Toby A Inkster wrote:
TAI> Peter J. Holzer wrote:
>> my $roundtrip_delay = ($T4 - $T1) - ($T2 - $T2);
TAI> But $T2 - $T2 = 0!
Not if you pick sufficiently large values of $T2 :)
(seriously, there are at least a few places this is not true in Perl,
e.g. tied scalars or IEEE NaN/Infinity math)
Ted