class SyncWrap::TimeChecker

Component for checking and comparing remote host times. This can be used to validate proper time keeping (e.g. via ntpd or similar) on multiple remote hosts by comparing with local time. Its also illustrative of syncwrap threading and synchronized output formatting.

Usage

First you should add this component to some or all hosts:

role( :all, TimeChecker.new )

Then you run it via the command line:

syncwrap TimeChecker.check

You can also experiment with -v and -t flags if desired.

Output is in the following form per host:

host-name : start-delta <- HH:MM:SS.NNNNNNZ -> return-delta

Where:

If both local and remote hosts have well synchronized clocks, then both these differences should be positive, but small negative values for return-delta are common, in the range of the clock skew. Negative values are formatted in red when output is colorized.

Host component dependencies: none

Constants

FORMAT

Public Instance Methods

check() click to toggle source
# File lib/syncwrap/components/time_checker.rb, line 68
def check
  lnow1 = Time.now.utc
  rc, out = capture( 'date --rfc-3339=ns' )
  if rc == 0
    lnow2 = Time.now.utc
    rnow = Time.parse( out ).utc
    d1 =         rnow - lnow1
    d2 = lnow2 - rnow
    formatter.sync do
      c = formatter.io
      c << FORMAT % [ host.name,
                      d1 < 0.0 ? red? : green?, d1, clear?,
                      rnow.strftime( '%H:%M:%S.%6NZ' ),
                      d2 < 0.0 ? red? : green?, d2, clear? ]
      c.flush
    end
  end
end