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 <github@growse.com>
This commit is contained in:
Andrew Rowson 2022-07-27 14:41:59 +01:00 committed by GitHub
parent 8a688e8903
commit aa1adc7b11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -35,6 +35,7 @@ const (
var ( var (
ntpServer = kingpin.Flag("collector.ntp.server", "NTP server to use for ntp collector").Default("127.0.0.1").String() ntpServer = kingpin.Flag("collector.ntp.server", "NTP server to use for ntp collector").Default("127.0.0.1").String()
ntpServerPort = kingpin.Flag("collector.ntp.server-port", "UDP port number to connect to on NTP server").Default("123").Int()
ntpProtocolVersion = kingpin.Flag("collector.ntp.protocol-version", "NTP protocol version").Default("4").Int() ntpProtocolVersion = kingpin.Flag("collector.ntp.protocol-version", "NTP protocol version").Default("4").Int()
ntpServerIsLocal = kingpin.Flag("collector.ntp.server-is-local", "Certify that collector.ntp.server address is not a public ntp server").Default("false").Bool() ntpServerIsLocal = kingpin.Flag("collector.ntp.server-is-local", "Certify that collector.ntp.server address is not a public ntp server").Default("false").Bool()
ntpIPTTL = kingpin.Flag("collector.ntp.ip-ttl", "IP TTL to use while sending NTP query").Default("1").Int() 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) {
return nil, fmt.Errorf("offset tolerance must be non-negative") return nil, fmt.Errorf("offset tolerance must be non-negative")
} }
if *ntpServerPort < 1 || *ntpServerPort > 65535 {
return nil, fmt.Errorf("invalid NTP port number %d; must be between 1 and 65535 inclusive", *ntpServerPort)
}
return &ntpCollector{ return &ntpCollector{
stratum: typedDesc{prometheus.NewDesc( stratum: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, ntpSubsystem, "stratum"), prometheus.BuildFQName(namespace, ntpSubsystem, "stratum"),
@ -124,6 +129,7 @@ func (c *ntpCollector) Update(ch chan<- prometheus.Metric) error {
Version: *ntpProtocolVersion, Version: *ntpProtocolVersion,
TTL: *ntpIPTTL, TTL: *ntpIPTTL,
Timeout: time.Second, // default `ntpdate` timeout Timeout: time.Second, // default `ntpdate` timeout
Port: *ntpServerPort,
}) })
if err != nil { if err != nil {
return fmt.Errorf("couldn't get SNTP reply: %w", err) return fmt.Errorf("couldn't get SNTP reply: %w", err)