banshee is under active development: collectors will keep changing, and a version you publish will likely need adjustments. For stable use, wait for version 1.0.0.

Windows

banshee/windows-collection/windows/ssh/

clock

The offset of the clock of a Windows from a time server, by SSH and PowerShell, and NTP

  • windows
  • ssh
  • powershell
  • clock
  • ntp
  • time

by banshee1 installation2 versions

Keen 1.0.0 · 40 lines
// The offset of a Windows 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 one PowerShell command 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 SSH call, connection and the start of PowerShell
// included: the offset is therefore read to about a 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("powershell -NoProfile -NonInteractive -Command \"[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 SSH 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));