| 1 | package Monitoring::Sneck::Checks::ntp; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use IO::Select (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Monitoring::Sneck::Checks::ntp - checks the local clock against a NTP server | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Seconds from 1900, where NTP time starts, to 1970. | ||||||||||||||||||||||||||||
| 23 | 3 | our $NTP_EPOCH = 2208988800; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | clock|perl:ntp -H pool.ntp.org | ||||||||||||||||||||||||||||
| 28 | clock_tight|perl:ntp -H 192.0.2.123 -w 0.5 -c 1 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Asks a NTP server for the time over UDP and works out how far the local | ||||||||||||||||||||||||||||
| 33 | clock is off from it, the same as check_ntp_time. The offset, either | ||||||||||||||||||||||||||||
| 34 | way, is checked against the ranges. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | No reply, a server that says it is not synced, being stratum 16 or | ||||||||||||||||||||||||||||
| 37 | having its leap indicator set to 3, or a kiss of death, such as RATE, | ||||||||||||||||||||||||||||
| 38 | are CRITICAL. A reply that is not for what was sent is UNKNOWN. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | - -H, --hostname :: The NTP server. Required. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | - -p, --port :: The port. | ||||||||||||||||||||||||||||
| 45 | Default :: 123 | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | - -w, --warning :: Range of seconds for the offset. | ||||||||||||||||||||||||||||
| 48 | Default :: 60 | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | - -c, --critical :: Range of seconds for the offset. | ||||||||||||||||||||||||||||
| 51 | Default :: 120 | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 10 | ▶ | sub option_spec { return ( 'hostname|H=s', 'port|p=i', 'warning|w=s', 'critical|c=s' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 10 | ▶ | sub defaults { return { port => 123, warning => '60', critical => '120' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 10 | ▶ | sub required { return ('hostname') } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 10 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 10 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 10 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | return ( '--hostname "' . $options->{hostname} . '" is not a IP or hostname' ) | ||||||||||||||||||||||||||||
| 67 | 10 | ▶ | if defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
MC/DC0%0/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"}
not $self->is_host($options->{"hostname"})
Truth table33%1/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 10 | return (); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 METHODS | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 check | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Does the check. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 9 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 9 | require IO::Socket::IP; | |||||||||||||||||||||||||||
| 84 | 9 | my $where = $self->opt('hostname') . ':' . $self->opt('port'); | |||||||||||||||||||||||||||
| 85 | 9 | my $socket = IO::Socket::IP->new( | |||||||||||||||||||||||||||
| 86 | PeerHost => $self->opt('hostname'), | ||||||||||||||||||||||||||||
| 87 | PeerPort => $self->opt('port'), | ||||||||||||||||||||||||||||
| 88 | Proto => 'udp', | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | 9 | ▶ | if ( !defined($socket) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return $self->finish( $self->CRITICAL, $where . ', ' . ( $@ || $! ) ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$@ || $!
MC/DC0%0/2
$@ || $!
$@
$!
Truth table0%0/3
A || B
A= $@B= $!
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # leap indicator 0, version 4, mode 3 for a client, and when it was sent as the transmit time | ||||||||||||||||||||||||||||
| 95 | 9 | my $sent = Time::HiRes::time; | |||||||||||||||||||||||||||
| 96 | 9 | my $stamp = _to_ntp($sent); | |||||||||||||||||||||||||||
| 97 | 9 | my $request = pack( 'C', 0x23 ) . ( "\0" x 39 ) . $stamp; | |||||||||||||||||||||||||||
| 98 | 9 | send( $socket, $request, 0 ); | |||||||||||||||||||||||||||
| 99 | 9 | my $reply; | |||||||||||||||||||||||||||
| 100 | 9 | ▶ | if ( IO::Select->new($socket)->can_read( $self->timeout ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 8 | recv( $socket, $reply, 1024, 0 ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 9 | my $received = Time::HiRes::time; | |||||||||||||||||||||||||||
| 104 | 9 | close($socket); | |||||||||||||||||||||||||||
| 105 | 9 | ▶ | if ( !defined($reply) || length($reply) < 48 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $reply or length $reply < 48
MC/DC50%1/2
not defined $reply or length $reply < 48
not defined $reply
length $reply < 48
Truth table66%2/3
not(A) or B
A= not defined $replyB= length $reply < 48
| |||||||||||||||||||||||||||||
| 106 | 1 | return $self->finish( $self->CRITICAL, 'no reply from ' . $where ); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 8 | my ( $flags, $stratum ) = unpack( 'C C', $reply ); | |||||||||||||||||||||||||||
| 110 | 8 | my $leap = $flags >> 6; | |||||||||||||||||||||||||||
| 111 | 8 | my $mode = $flags & 0x07; | |||||||||||||||||||||||||||
| 112 | 8 | ▶ | if ( $mode != 4 || substr( $reply, 24, 8 ) ne $stamp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mode != 4 or substr($reply, 24, 8) ne $stamp
MC/DC100%2/2
$mode != 4 or substr($reply, 24, 8) ne $stamp
$mode != 4
substr($reply, 24, 8) ne $stamp
Truth table100%3/3
A or B
A= $mode != 4B= substr($reply, 24, 8) ne $stamp
| |||||||||||||||||||||||||||||
| 113 | 2 | return $self->finish( $self->UNKNOWN, $where . ' sent a reply that is not for what was sent' ); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 6 | ▶ | if ( $stratum == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | my $code = substr( $reply, 12, 4 ) =~ s/[^\x20-\x7e]//gr; | |||||||||||||||||||||||||||
| 117 | 1 | return $self->finish( $self->CRITICAL, $where . ' sent a kiss of death, ' . $code ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 5 | ▶ | if ( $stratum >= 16 || $leap == 3 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$stratum >= 16 or $leap == 3
MC/DC50%1/2
$stratum >= 16 or $leap == 3
$stratum >= 16
$leap == 3
Truth table66%2/3
A or B
A= $stratum >= 16B= $leap == 3
| |||||||||||||||||||||||||||||
| 120 | 1 | return $self->finish( $self->CRITICAL, $where . ' is not synced, stratum ' . $stratum ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # offset is ((T2 - T1) + (T3 - T4)) / 2 and delay (T4 - T1) - (T3 - T2) | ||||||||||||||||||||||||||||
| 124 | 4 | my $server_received = _from_ntp( substr( $reply, 32, 8 ) ); | |||||||||||||||||||||||||||
| 125 | 4 | my $server_sent = _from_ntp( substr( $reply, 40, 8 ) ); | |||||||||||||||||||||||||||
| 126 | 4 | my $offset = sprintf( '%.6f', ( ( $server_received - $sent ) + ( $server_sent - $received ) ) / 2 ); | |||||||||||||||||||||||||||
| 127 | 4 | my $delay = sprintf( '%.6f', ( $received - $sent ) - ( $server_sent - $server_received ) ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | $self->perfdata( | |||||||||||||||||||||||||||
| 130 | label => 'offset', | ||||||||||||||||||||||||||||
| 131 | value => $offset, | ||||||||||||||||||||||||||||
| 132 | uom => 's', | ||||||||||||||||||||||||||||
| 133 | warning => $self->opt('warning'), | ||||||||||||||||||||||||||||
| 134 | critical => $self->opt('critical') | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | 4 | $self->perfdata( label => 'delay', value => $delay, uom => 's', min => 0 ); | |||||||||||||||||||||||||||
| 137 | 4 | $self->perfdata( label => 'stratum', value => $stratum, min => 0, max => 16 ); | |||||||||||||||||||||||||||
| 138 | 4 | my $code = $self->threshold_status( abs($offset), $self->opt('warning'), $self->opt('critical') ); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 4 | return $self->finish( $code, 'offset ' . $offset . ' seconds from ' . $where . ', stratum ' . $stratum ); | |||||||||||||||||||||||||||
| 141 | } ## end sub check | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Turns a time since epoch into a NTP time stamp. | ||||||||||||||||||||||||||||
| 144 | # | ||||||||||||||||||||||||||||
| 145 | # Args... | ||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||
| 147 | # - time :: Seconds since epoch, with a fraction. | ||||||||||||||||||||||||||||
| 148 | # | ||||||||||||||||||||||||||||
| 149 | # Returns the 8 bytes of the time stamp, seconds since 1900 and a 32 bit | ||||||||||||||||||||||||||||
| 150 | # fraction, both big endian. | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | # Example... | ||||||||||||||||||||||||||||
| 153 | # | ||||||||||||||||||||||||||||
| 154 | # _to_ntp(1793530999.5); | ||||||||||||||||||||||||||||
| 155 | # # pack( 'N N', 4002519799, 2147483648 ) | ||||||||||||||||||||||||||||
| 156 | 9 | ▶ | sub _to_ntp { | ||||||||||||||||||||||||||
|
Subroutine
_to_ntp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | 9 | my ($time) = @_; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 9 | my $seconds = int($time); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 9 | return pack( 'N N', $seconds + $NTP_EPOCH, int( ( $time - $seconds ) * 4294967296 ) ); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Turns a NTP time stamp into a time since epoch. | ||||||||||||||||||||||||||||
| 165 | # | ||||||||||||||||||||||||||||
| 166 | # Args... | ||||||||||||||||||||||||||||
| 167 | # | ||||||||||||||||||||||||||||
| 168 | # - stamp :: The 8 bytes of the time stamp. | ||||||||||||||||||||||||||||
| 169 | # | ||||||||||||||||||||||||||||
| 170 | # Returns seconds since epoch, with a fraction. | ||||||||||||||||||||||||||||
| 171 | # | ||||||||||||||||||||||||||||
| 172 | # Example... | ||||||||||||||||||||||||||||
| 173 | # | ||||||||||||||||||||||||||||
| 174 | # _from_ntp( pack( 'N N', 4002519799, 2147483648 ) ); | ||||||||||||||||||||||||||||
| 175 | # # 1793530999.5 | ||||||||||||||||||||||||||||
| 176 | 8 | ▶ | sub _from_ntp { | ||||||||||||||||||||||||||
|
Subroutine
_from_ntp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 177 | 8 | my ($stamp) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 8 | my ( $seconds, $fraction ) = unpack( 'N N', $stamp ); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 8 | return $seconds - $NTP_EPOCH + $fraction / 4294967296; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3 | 1; | |||||||||||||||||||||||||||