Linux
banshee/linux-collection/linux/ssh/
clock
Offset of a Linux clock from a time server: date by SSH against NTP.
by banshee1 installation2 versions
v2Latest
Published on October 28, 2026. One naming convention across collections; documentation completed.
// The offset of a Linux clock from a time server: the time of the machine, read by SSH, compared with the
// time of the server that "Time server" names (pool.ntp.org when empty). Positive, the clock is ahead;
// negative, it is behind.
// Runs date +%s, which date of BusyBox and of GNU coreutils both know: the seconds since 1 January 1970
// UTC, with no time zone to guess. date truncates to the second: the true instant lies in the second that
// follows, and its middle is the best estimate.
// The server's time is brought back to the middle of the SSH call, connection included: the offset is
// therefore read to about one second, far below the default thresholds.
need('protocol:ssh');
need('protocol:ntp');
output('offset', 'number', array('label' => 'Offset from the time server', 'unit' => 's'));
output('drift', 'number', array('label' => 'Offset, without its sign', 'unit' => 's'));
watch.above('clock-drift', 'Clock off', 'drift', array('warning' => 5, 'critical' => 60));
$asked = time.now();
$date = ssh.run('date +%s');
$answered = time.now();
$server = ntp.time(errors=$err);
$now = time.now();
if ($err['code'] != '') {
fail('unreachable', 'Time server unreachable', $err['text']);
}
$seconds = text.trim($date['output']);
if (!text.is_number($seconds)) {
fail('misconfigured', 'date +%s did not return a number of seconds', $seconds . $date['errors']);
}
// The server's time at the middle of the SSH call: its time, less the time elapsed since, measured by the
// poller's clock.
$reference = $server - ($now - ($asked + $answered) / 2);
$offset = $seconds + 0.5 - $reference;
$drift = $offset;
if ($drift < 0) {
$drift = -$drift;
}
value('offset', number.round($offset, 1));
value('drift', number.round($drift, 1));