Skip to main content
Ruby

Using Ruby to Calculate NTP Poll Intervals

By March 17, 2017September 12th, 2022No Comments

Using Ruby to Calculate NTP Poll IntervalsHaving practical examples on using any programming language is always useful. I am very much from a system administration background, meaning that my examples will also be geared towards sysadmin tasks. It is no different with today’s example and Ruby recipe where we show using Ruby to calculate NTP Poll Intervals. In reality, we are using the Power Of operators in Ruby to calculate the antilogs of the NTP ( Time Server ) poll intervals.

NTP Time Server

The NTP or Network Time Protocol Server is used keep a server or desktops internal clock synchronised to the network. In this way, we can ensure that we have accurate time on servers and devices on the network. This is important even on desktop systems where time variation may mean they can’t access HTTPS sights where their time conflicts with the certificate start or end date.

Configuration of The Server

In configuring the server you set a time source for the server to synchronise with. Where they get the time from. You may also set a Minumum Poll interval and a Maximum Poll Interval. The following extract is from a time configuration file:

server 172.16.0.3 minpoll 8 maxpoll 16 prefer

The minimum poll interval is the starting point for synchronisation and specifies the smallest time interval between asking the server for the correct time.It is shown here as the setting minpoll 8. The maximum poll interval specifies the largest amount of time that may pass between synchronisation. Here it is shown as maxpoll 16.


The values set against these settings are the area of confusing. The value shown is LOG2(Seconds), yes, I told you it was confusing. To be able to read this we need to be able to inverse the log or read the antilog of this setting. This will show the value in seconds for the poll intervals. Thankfully, it is not that difficult, 2 to the power of 8 and 2 to the power of 16 will return the values that we need.

Using Ruby to Calculate NTP Poll Intervals

Using the IRB console we can easily calculate these. The Power Of operator is **.

 irb
irb(main):001:0> 2 ** 8
=> 256


irb(main):002:0> 2 ** 16
=> 65536
irb(main):003:0>

We can see that the minimum poll interval is set to 256 seconds, just short of 5 minutes. The maximum poll interval is set to 65,536 seconds which is about 18 hours or so. Rather than just use Ruby as a calculator we can do much more. In later versions on NTP the values that can be set range from 4 to 17. If we want to print these out then we can easily achieve this using loops and ranges in Ruby.

Printing Ranges in Ruby

We will work with a Ruby file so it is easier to correct the script if we have typos. Your finished script should be similar to this.

#!/usr/bin/ruby
#Used to print valid minimum and maximum poll values for NTP
#The values used in the conf file are in LOG2(seconds) and range from 4-17.
#To see their actual versions we need to antilog or power of the setting
for x in (4..17)
        s = 2 ** x
        printf "%2d = %6d seconds\n",x,s
end

The for loop is used to iterate through a list of values. The list is created through the Ruby range operator .. .The range 4..17 will list all integer values between and including 4 to 17.

The first line inside the loop sets a local variable which we call s, for seconds. This is gained from the result of the calculation of 2 ** x. Or 2 to the power of the current value of x in the iteration loop. This will start on 4 and make its incrementally through to 17. So on the first loop iteration x will equal 4, the next time it will equal 5 and so on through to 17.

The second line in the loop uses printf. This allows some formatting of the output so we can align values on the screen. We allow for two numbers for the NTP value and up to 6 numbers for the values in seconds.  The script’s output should show as this:

 4 =     16 seconds
 5 =     32 seconds
 6 =     64 seconds
 7 =    128 seconds
 8 =    256 seconds
 9 =    512 seconds
10 =   1024 seconds
11 =   2048 seconds
12 =   4096 seconds
13 =   8192 seconds
14 =  16384 seconds
15 =  32768 seconds
16 =  65536 seconds
17 = 131072 seconds

The video will step you through the script.