Windows
banshee/windows-collection/windows/winrm/
clock
The offset of the clock of a Windows from a time server, by WinRM and PowerShell, and NTP
by banshee1 installation2 versions
// The offset of a Windows clock from a time server: the time of the machine, read by WinRM, 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 one PowerShell script that only reads the clock: [DateTime]::UtcNow.Ticks, the hundreds of
// nanoseconds since 1 January of the year 1 UTC, an integer, with no time zone and no culture to guess.
// Every .NET has it, that of Windows Server 2012 included.
// The server's time is brought back to the middle of the WinRM call, connection and the start of
// PowerShell included: the offset is therefore read to about a second, far below the default thresholds.
// The script is the one of windows/ssh/clock, without the powershell -Command "..." that cmd.exe needs
// there.
need('protocol:winrm');
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 = winrm.run("[DateTime]::UtcNow.Ticks");
$answered = time.now();
$server = ntp.time(errors=$err);
$now = time.now();
if ($err['code'] != '') {
fail('unreachable', 'Time server unreachable', $err['text']);
}
$ticks = text.trim($date['output']);
if (!text.is_number($ticks)) {
fail('misconfigured', 'PowerShell did not return the time of the machine', $ticks . $date['errors']);
}
// The server's time at the middle of the WinRM call: its time, less the time elapsed since, measured by the
// poller's clock.
$reference = $server - ($now - ($asked + $answered) / 2);
// 621355968000000000 ticks separate 1 January of the year 1 from 1 January 1970; ten million make a second.
$offset = ($ticks - 621355968000000000) / 10000000 - $reference;
$drift = $offset;
if ($drift < 0) {
$drift = -$drift;
}
value('offset', number.round($offset, 1));
value('drift', number.round($drift, 1));