4545class NTPMonitor (Node ):
4646 """A diagnostic task that monitors the NTP offset of the system clock."""
4747
48- def __init__ (self , ntp_hostname , offset = 500 , self_offset = 500 ,
48+ def __init__ (self , ntp_hostname , ntp_port , offset = 500 , self_offset = 500 ,
4949 diag_hostname = None , error_offset = 5000000 ,
5050 do_self_test = True ):
5151 """Initialize the NTPMonitor."""
@@ -54,6 +54,7 @@ def __init__(self, ntp_hostname, offset=500, self_offset=500,
5454 frequency = self .get_parameter ('frequency' ).get_parameter_value ().double_value
5555
5656 self .ntp_hostname = ntp_hostname
57+ self .ntp_port = ntp_port
5758 self .offset = offset
5859 self .self_offset = self_offset
5960 self .diag_hostname = diag_hostname
@@ -67,7 +68,8 @@ def __init__(self, ntp_hostname, offset=500, self_offset=500,
6768 self .stat = DIAG .DiagnosticStatus ()
6869 self .stat .level = DIAG .DiagnosticStatus .OK
6970 self .stat .name = 'NTP offset from ' + \
70- self .diag_hostname + ' to ' + self .ntp_hostname
71+ self .diag_hostname + ' to ' + self .ntp_hostname + \
72+ ':' + str (self .ntp_port )
7173 self .stat .message = 'OK'
7274 self .stat .hardware_id = self .hostname
7375 self .stat .values = []
@@ -128,7 +130,10 @@ def add_kv(stat_values, key, value):
128130 ntp_client = ntplib .NTPClient ()
129131 response = None
130132 try :
131- response = ntp_client .request (self .ntp_hostname , version = 3 )
133+ response = ntp_client .request (
134+ self .ntp_hostname ,
135+ port = self .ntp_port ,
136+ version = 3 )
132137 except ntplib .NTPException as e :
133138 self .get_logger ().error (f'NTP Error: { e } ' )
134139 st .level = DIAG .DiagnosticStatus .ERROR
@@ -164,6 +169,9 @@ def ntp_monitor_main(argv=sys.argv[1:]):
164169 parser .add_argument ('--ntp_hostname' ,
165170 action = 'store' , default = '0.pool.ntp.org' ,
166171 type = str )
172+ parser .add_argument ('--ntp_port' ,
173+ action = 'store' , default = 123 ,
174+ type = int )
167175 parser .add_argument ('--offset-tolerance' , dest = 'offset_tol' ,
168176 action = 'store' , default = 500 ,
169177 help = 'Offset from NTP host [us]' , metavar = 'OFFSET-TOL' ,
@@ -192,7 +200,8 @@ def ntp_monitor_main(argv=sys.argv[1:]):
192200 assert offset < error_offset , \
193201 'Offset tolerance must be less than error offset tolerance'
194202
195- ntp_monitor = NTPMonitor (args .ntp_hostname , offset , self_offset ,
203+ ntp_monitor = NTPMonitor (args .ntp_hostname , args .ntp_port ,
204+ offset , self_offset ,
196205 args .diag_hostname , error_offset ,
197206 args .do_self_test )
198207
0 commit comments