54
54
55
55
NAME = 'ntp_monitor'
56
56
57
- def ntp_monitor (ntp_hostname , offset = 500 , self_offset = 500 , diag_hostname = None , error_offset = 5000000 ):
57
+ def ntp_monitor (ntp_hostname , offset = 500 , self_offset = 500 , diag_hostname = None ,
58
+ error_offset = 5000000 , ignore_self = False ):
58
59
pub = rospy .Publisher ("/diagnostics" , DiagnosticArray , queue_size = 10 )
59
60
rospy .init_node (NAME , anonymous = True )
60
61
61
62
hostname = socket .gethostname ()
62
63
if diag_hostname is None :
63
64
diag_hostname = hostname
64
65
66
+ ntp_checks = []
65
67
stat = DiagnosticStatus ()
66
68
stat .level = 0
67
69
stat .name = "NTP offset from " + diag_hostname + " to " + ntp_hostname
68
70
stat .message = "OK"
69
71
stat .hardware_id = hostname
70
72
stat .values = []
73
+ ntp_checks .append ((stat , ntp_hostname , offset ))
74
+
75
+ if not ignore_self :
76
+ self_stat = DiagnosticStatus ()
77
+ self_stat .level = DiagnosticStatus .OK
78
+ self_stat .name = "NTP self-offset for " + diag_hostname
79
+ self_stat .message = "OK"
80
+ self_stat .hardware_id = hostname
81
+ self_stat .values = []
82
+ ntp_checks .append ((self_stat , hostname , self_offset ))
71
83
72
- self_stat = DiagnosticStatus ()
73
- self_stat .level = DiagnosticStatus .OK
74
- self_stat .name = "NTP self-offset for " + diag_hostname
75
- self_stat .message = "OK"
76
- self_stat .hardware_id = hostname
77
- self_stat .values = []
78
-
79
84
while not rospy .is_shutdown ():
80
- for st ,host ,off in [(stat ,ntp_hostname ,offset ), (self_stat , hostname ,self_offset )]:
85
+ msg = DiagnosticArray ()
86
+ for st , host , off in ntp_checks :
81
87
try :
82
88
p = Popen (["ntpdate" , "-q" , host ], stdout = PIPE , stdin = PIPE , stderr = PIPE )
83
89
res = p .wait ()
@@ -111,11 +117,9 @@ def ntp_monitor(ntp_hostname, offset=500, self_offset=500, diag_hostname = None,
111
117
KeyValue ("Offset tolerance (us) for Error" , str (error_offset )),
112
118
KeyValue ("Output" , o ),
113
119
KeyValue ("Errors" , e ) ]
120
+ msg .status .append (st )
114
121
115
-
116
- msg = DiagnosticArray ()
117
122
msg .header .stamp = rospy .get_rostime ()
118
- msg .status = [stat , self_stat ]
119
123
pub .publish (msg )
120
124
time .sleep (1 )
121
125
@@ -135,6 +139,8 @@ def ntp_monitor_main(argv=sys.argv):
135
139
help = "Computer name in diagnostics output (ex: 'c1')" ,
136
140
metavar = "DIAG_HOSTNAME" ,
137
141
action = "store" , default = None )
142
+ parser .add_option ("--ignore-self" , dest = "ignore_self" ,
143
+ help = "Ignore self NTP test" , action = "store_true" )
138
144
options , args = parser .parse_args (rospy .myargv ())
139
145
140
146
if (len (args ) != 2 ):
@@ -145,10 +151,12 @@ def ntp_monitor_main(argv=sys.argv):
145
151
offset = int (options .offset_tol )
146
152
self_offset = int (options .self_offset_tol )
147
153
error_offset = int (options .error_offset_tol )
154
+ ignore_self = options .ignore_self
148
155
except :
149
156
parser .error ("Offsets must be numbers" )
150
157
151
- ntp_monitor (args [1 ], offset , self_offset , options .diag_hostname , error_offset )
158
+ ntp_monitor (args [1 ], offset , self_offset , options .diag_hostname ,
159
+ error_offset , ignore_self )
152
160
153
161
154
162
if __name__ == "__main__" :
0 commit comments