Skip to content

Commit aa1adc7

Browse files
authored
Allow user to select port on NTP server to query (#2270)
* Allow user to select port on NTP server to query Some people (me!) run NTP servers on non-privileged ports. The `github.com/beevik/ntp` package allows overriding the port, so this change just adds a flag `collector.ntp.server-port` (defaults to 123) and then passes that value through to the query via the `QueryOptions`. Signed-off-by: Andrew Rowson <[email protected]>
1 parent 8a688e8 commit aa1adc7

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

collector/ntp.go

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535

3636
var (
3737
ntpServer = kingpin.Flag("collector.ntp.server", "NTP server to use for ntp collector").Default("127.0.0.1").String()
38+
ntpServerPort = kingpin.Flag("collector.ntp.server-port", "UDP port number to connect to on NTP server").Default("123").Int()
3839
ntpProtocolVersion = kingpin.Flag("collector.ntp.protocol-version", "NTP protocol version").Default("4").Int()
3940
ntpServerIsLocal = kingpin.Flag("collector.ntp.server-is-local", "Certify that collector.ntp.server address is not a public ntp server").Default("false").Bool()
4041
ntpIPTTL = kingpin.Flag("collector.ntp.ip-ttl", "IP TTL to use while sending NTP query").Default("1").Int()
@@ -74,6 +75,10 @@ func NewNtpCollector(logger log.Logger) (Collector, error) {
7475
return nil, fmt.Errorf("offset tolerance must be non-negative")
7576
}
7677

78+
if *ntpServerPort < 1 || *ntpServerPort > 65535 {
79+
return nil, fmt.Errorf("invalid NTP port number %d; must be between 1 and 65535 inclusive", *ntpServerPort)
80+
}
81+
7782
return &ntpCollector{
7883
stratum: typedDesc{prometheus.NewDesc(
7984
prometheus.BuildFQName(namespace, ntpSubsystem, "stratum"),
@@ -124,6 +129,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error {
124129
Version: *ntpProtocolVersion,
125130
TTL: *ntpIPTTL,
126131
Timeout: time.Second, // default `ntpdate` timeout
132+
Port: *ntpServerPort,
127133
})
128134
if err != nil {
129135
return fmt.Errorf("couldn't get SNTP reply: %w", err)

0 commit comments

Comments
 (0)