Script to detect time drift


 
Thread Tools Search this Thread
Operating Systems HP-UX Script to detect time drift
# 1  
Old 05-10-2018
Script to detect time drift

Hello there,

I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms.

I have been googling a lot but thought to post it in this forum to get a suggestion on the best way to do this.

Can we read in the ntp.drift file that is created on the server(OS is HP-UX) which script will run and use the value in the file to calculate the time drift or is there a better approach?

Thanks in-advance for you suggestions.
# 2  
Old 05-10-2018
You have an internal company NTP server? You are trying to test it's accuracy, is that correct? Do you have access to that internal NTP server to run stuff on it? Or can you only check it from a client machine?
# 3  
Old 05-11-2018
Thanks for your response hicksd8.

My responses are below

You have an internal company NTP server? Yes, we have internal NTP server

You are trying to test it's accuracy, is that correct? Yes. correct

Do you have access to that internal NTP server to run stuff on it? Or can you only check it from a client machine? We have access to server to run stuff on it.
# 4  
Old 05-11-2018
You probably won't be surprised to learn that I haven't actually done, or tried this, myself, but researching it I think that you can query an external NTP server from your internal NTP server WITHOUT actually updating its clock, and then compare the two for difference.

On your internal NTP server on which you want to monitor drift, set up the NTP config file with a single server entry to an external NTP/PTP server and include the server option 'noselect'.

When the NTP query runs it will not update the clock but the offset can be viewed using

Code:
nptq -pn

or

Code:
nptq -c rl

You don't say what Unix/Linux you are running (EDIT: Sorry you posted to the HP-UX forum) and therefore I cannot say what NTP commands are implemented on it. You will have to test the function manually first and then get one of the scripting experts on this forum to help you if you need it. You will, of course, also need to include a function to update the clock for real when it gets too far adrift.

Last edited by hicksd8; 05-11-2018 at 11:27 AM..
This User Gave Thanks to hicksd8 For This Post:
# 5  
Old 05-14-2018
You would have to do this on the client side. If you have a problem with your NTP time server, then treat that as the client and get it to align to another trusted clock, be that a radio-clock or internet address.

You can also use ntpdate -d refer-server to get a time difference from the reference server (which must be offering the NTP service, of course)

if the clock drift is too far, then you would need to step the clock on the local server to match something like this:-
  • Check the offset with - ntpdate -d ref-server
  • Stop the local NTP service in the normal way
  • Step the clock into sync - ntpdate ref-server
  • Start the local service in the normal way
  • Check the offset with - ntpdate -d ref-server

You should see debug information when you use the -d flag and the last line gives you the agreed offset from the reference server or servers (just a space separated list)


If you can't have the NTP client running all the time because your application doesn't like the clock going backwards even by tiny fractions of a second, you would probably need to schedule and idle minute to step the clock each day. You can run ntpdate -d ref-server at any time and just use the last line to show you the current offset from the trusted clock.



I hope that this helps,
Robin
# 6  
Old 05-14-2018
Thanks rbattle1 for your response.

The user wants us to create a script that runs on the UNIX HP-UX server every 10 min and check for any time drifts >+/- 50 ms. If the time drift is >+/- 50 ms then the script should trigger an email to the user.

I am not quite sure the next steps of the user once they are aware of time drifts.

After talking to couple of DBAs & googling, I understood that if the NTP service runs every 10 minutes on the server, it creates ntp.drift file every time NTP service runs. And ntp.drift contains the offset information and I can use this to calculate time in milli seconds and calculate the time drift. Is this assumption correct?

Please advise. I just want to make sure I am doing the right thing.

Thanks very much in advance.
# 7  
Old 05-14-2018
ntpdate requires root rights.
Better is ntpq as hicksd8 wants to propose (but got mispelled).
The following ksh script does it
Code:
#!/bin/ksh
PATH=/bin:/usr/bin:/usr/sbin:/sbin
offset=$(
ntpq -c rl | sed -n '
s/.*phase=\([-0-9.]*\).*/\1/p
t
s/.*offset=\([-0-9.]*\).*/\1/p
'
)
[[ $offset -lt 0 ]] && offset=$((-offset))
if [[ $offset -gt 50 ]]
then
  echo "$offset is greater than 50 msec"
fi

NB ksh88 rounds floating point numbers to integers, while ksh93 has full floating point precision.
These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to detect url in use in a script?

Hello, I have a small script and it runs from web application in below format: pipe:///path_to_myscript.sh url1 url2 url3 myscript.sh: #!/bin/bash count=0 while do count=$((count+1)) exec 3>&1 ((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \ while read LINE; do echo... (9 Replies)
Discussion started by: baris35
9 Replies

2. UNIX for Beginners Questions & Answers

Script to detect time drift on server

Hello there, I am not an expert in networking related stuff but I got a requirement to create UNIX script to query our Company's internal time source via NTP for time drift detect and report it when > +/- 50ms. I have been googling a lot but thought to post it in this forum to get a... (1 Reply)
Discussion started by: Green_Star
1 Replies

3. UNIX for Advanced & Expert Users

A script to detect system type

Hi forum, So I am trying to determine the OS type with the following script: #!/usr/bin/sh OStype1=`uname -s` Sunos1=SunOs if then echo "This system is Linux" exit 0 elif then echo "This system is SunOs" exit 0 elif (1 Reply)
Discussion started by: dampio
1 Replies

4. Shell Programming and Scripting

Help with detect with regex and move script

Hi all, I am needing some help with a script that will search for a video file by known extensions and then do a pattern search (I'm guessing via regex) and then based on a match of one type of another move the file to an assigned directory. I would like to do this with either a shell script... (7 Replies)
Discussion started by: Simplify
7 Replies

5. Shell Programming and Scripting

How to detect Hanged process in shell script?

I have to check daily 20 processes each day. The names are like Network1 Network2 Network3 ....... Network20. There is built in utility for doing this. Following is the command to check a single network process. check_process_status 1 If we want to check the status of Network2 then the... (6 Replies)
Discussion started by: Nakul_sh
6 Replies

6. Shell Programming and Scripting

Detect if script starts from queue

Dear community, what I'm try to do is deny users to run a script without parameters from command bash, but the same script should run without parameters only from crontab. Example runs by crontab:*/5 * * * * /tmp/script.sh Here the normal execution starts every 5 minutes Example #1 runs by... (16 Replies)
Discussion started by: Lord Spectre
16 Replies

7. Shell Programming and Scripting

AWK script to detect webpages from file

Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: www.google.com www.facebook.com www.google.com the output should be: www.google.com x2... (2 Replies)
Discussion started by: ROFL
2 Replies

8. Shell Programming and Scripting

AWK script to detect webpages from file

Hi guys I'm very new to unix and I have to create an awk script that detects webpage addresses from a file/webpage and outputs how many times each webpage was detected.e.g. if my file was: (Note: The symbol " was added to stop them being created into links) "www.google.com"... (1 Reply)
Discussion started by: ROFL
1 Replies

9. UNIX for Dummies Questions & Answers

how to detect my script is already running

I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job. In my script can i have logic to say if this script is already running from the previous cron, then exit. How do i go about doing that. If you describe the... (11 Replies)
Discussion started by: rmulchandani
11 Replies
Login or Register to Ask a Question