-
Notifications
You must be signed in to change notification settings - Fork 3
/
drift-log.py
executable file
·191 lines (135 loc) · 4.23 KB
/
drift-log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
# drift-log.py
# (C) Copyright IBM 2005
# by John Stultz johnstul@us.ibm.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import commands
import os
import sys
import string
import time
def ntpdate_offset(ntpdate_cmd, server):
#get actual offset
(stat, cmd) = commands.getstatusoutput(ntpdate_cmd + ' -uq ' + server)
if (stat != 0):
print "Bad ntpdate output:", cmd
sys.exit(-1)
line = string.split(cmd)
return string.atof(line[-2])
def ntpdate_set(ntpdate_cmd, server):
(stat, cmd) = commands.getstatusoutput(ntpdate_cmd + ' -ub ' + server)
line = string.split(cmd)
if len(line) != 8:
print "Time was set."
else:
print "Time could not be set."
def ntpdc_sysinfo_server(ntpdc_cmd):
(stat, cmd) = commands.getstatusoutput(ntpdc_cmd + ' -c sysinfo')
line = string.split(cmd)
server = ""
if len(line) != 4:
server = line[2]
return server
def ntpdc_kerninfo_offset(ntpdc_cmd):
#get ntpd's view of the world
(stat, cmd) = commands.getstatusoutput(ntpdc_cmd + ' -c kerninfo')
line = string.split(cmd)
offset = 0
if len(line) > 18:
offset = line[2]
return offset
def ntpdc_kerninfo_freq(ntpdc_cmd):
#get ntpd's view of the world
(stat, cmd) = commands.getstatusoutput(ntpdc_cmd + ' -c kerninfo')
line = string.split(cmd)
drift = 0
if len(line) > 18:
drift = line[6]
return drift
def ntpdc_kerninfo_status(ntpdc_cmd):
#get ntpd's view of the world
(stat, cmd) = commands.getstatusoutput(ntpdc_cmd + ' -c kerninfo')
line = string.split(cmd)
status = ""
if len(line) > 18:
status = line[17] + " " + line[18]
return status
def ntpdc_peers_offset(ntpdc_cmd):
#get ntpd's view of the world
(stat, cmd) = commands.getstatusoutput(ntpdc_cmd + ' -c peers')
line = string.split(cmd)
offset = line[-2]
return offset
def find_cmd(possibilities):
for path in possibilities:
if os.path.exists(path):
return path
print "Cannot fine command: ", possibilities
sys.exit(1)
sleep_time_default = 60
set_time = 0
server = ""
sleep_time = 0
loops = -1
#parse args
for arg in sys.argv[1:]:
if arg == "-s":
set_time = 1
elif server == "":
server = arg
elif sleep_time == 0:
sleep_time = string.atoi(arg)
elif loops == -1:
loops = string.atoi(arg)
#Find utility paths
ntpdate_possibilities = ["/usr/sbin/ntpdate", "/usr/bin/ntpdate"]
ntpdc_possibilities = ["/usr/sbin/ntpdc", "/usr/bin/ntpdc"]
ntpdate_cmd = find_cmd(ntpdate_possibilities)
ntpdc_cmd = find_cmd(ntpdc_possibilities)
#get current ntpd server name
ntpdc_server = ntpdc_sysinfo_server(ntpdc_cmd)
if server == "":
server = ntpdc_server
if sleep_time == 0:
sleep_time = sleep_time_default
(stat, host) = commands.getstatusoutput('hostname')
print "Host: ", host, "Server:", server, "every", sleep_time, "secs", loops , "times"
#check specified server is the same as ntpdc server
ip1 = string.split(commands.getoutput('host ' + server))
ip2 = string.split(commands.getoutput('host ' + ntpdc_server))
if ip1[-1] != ip2[-1]:
print "Warning: specified server", server,
print "is not the current NTP server", ntpdc_server
# set the time
if set_time == 1:
ntpdate_settime(ntpdate_cmd, server)
#start logging
print "key: date, secs, actual offset, kernel offset, ntp offset, ntp drift, ntp status"
print "================================================================================"
i = 0
while 1:
#get time right now
now_time = time.time()
datestr = time.strftime("%d %b %Y %H:%M:%S", time.localtime(now_time))
print datestr,",", now_time, ",",
print ntpdate_offset(ntpdate_cmd, server), ",",
print ntpdc_kerninfo_offset(ntpdc_cmd), ",",
print ntpdc_peers_offset(ntpdc_cmd), ",",
print ntpdc_kerninfo_freq(ntpdc_cmd), ",",
print ntpdc_kerninfo_status(ntpdc_cmd)
sys.stdout.flush()
i = i + 1
if (loops == -1):
i = 0
elif (i > loops):
break
time.sleep(sleep_time)