1 #!/usr/bin/python 2 3 # Time Drift Script 4 # Periodically checks and displays time drift 5 # by john stultz (jstultz (at] us.ibm.com) 6 7 # Copyright (C) 2003-2006 IBM 8 # 9 # This program is free software; you can redistribute it and/or 10 # modify it under the terms of the GNU General Public License as 11 # published by the Free Software Foundation; either version 2 of the 12 # License, or (at your option) any later version. 13 # 14 # This program is distributed in the hope that it will be useful, but 15 # WITHOUT ANY WARRANTY; without even the implied warranty of 16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 # General Public License for more details. 18 # 19 # You should have received a copy of the GNU General Public License 20 # along with this program; if not, write to the Free Software 21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 22 # 02111-1307, USA. 23 24 25 # Usage: drift-test.py [-s] [ntp_server [sleep_time]] 26 27 import commands 28 import sys 29 import string 30 import time 31 32 server_default = "bvrgsa.ibm.com" 33 sleep_time_default = 60 34 35 server = "" 36 sleep_time = 0 37 set_time = 0 38 39 #parse args 40 for arg in sys.argv[1:]: 41 if arg == "-s": 42 set_time = 1 43 elif server == "": 44 server = arg 45 elif sleep_time == 0: 46 sleep_time = string.atoi(arg) 47 48 if server == "": 49 server = server_default 50 if sleep_time == 0: 51 sleep_time = sleep_time_default 52 53 #set time 54 if (set_time == 1): 55 cmd = commands.getoutput('/usr/sbin/ntpdate -ub ' + server) 56 57 cmd = commands.getoutput('/usr/sbin/ntpdate -uq ' + server) 58 line = string.split(cmd) 59 60 #parse original offset 61 start_offset = string.atof(line[-2]); 62 #parse original time 63 start_time = time.localtime(time.time()) 64 datestr = time.strftime("%d %b %Y %H:%M:%S", start_time) 65 66 time.sleep(1) 67 while 1: 68 cmd = commands.getoutput('/usr/sbin/ntpdate -uq ' + server) 69 line = string.split(cmd) 70 71 #parse offset 72 now_offset = string.atof(line[-2]); 73 74 #parse time 75 now_time = time.localtime(time.time()) 76 datestr = time.strftime("%d %b %Y %H:%M:%S", now_time) 77 78 # calculate drift 79 delta_time = time.mktime(now_time) - time.mktime(start_time) 80 delta_offset = now_offset - start_offset 81 drift = delta_offset / delta_time * 1000000 82 83 #print output 84 print time.strftime("%d %b %H:%M:%S",now_time), 85 print " offset:", now_offset , 86 print " drift:", drift ,"ppm" 87 sys.stdout.flush() 88 89 #sleep 90 time.sleep(sleep_time) 91