diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 11:19:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-07-24 09:53:24 +0000 |
commit | b5f8ee61a7f7e9bd291dd26b0585d03eb686c941 (patch) | |
tree | d4d31289c39fc00da064a825df13a0b98ce95b10 /src/go/collectors/go.d.plugin/modules/ntpd/client.go | |
parent | Adding upstream version 1.44.3. (diff) | |
download | netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.tar.xz netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.zip |
Adding upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/ntpd/client.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/ntpd/client.go b/src/go/collectors/go.d.plugin/modules/ntpd/client.go new file mode 100644 index 000000000..8e111cd76 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/ntpd/client.go @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package ntpd + +import ( + "net" + "time" + + "github.com/facebook/time/ntp/control" +) + +func newNTPClient(c Config) (ntpConn, error) { + conn, err := net.DialTimeout("udp", c.Address, c.Timeout.Duration()) + if err != nil { + return nil, err + } + + client := &ntpClient{ + conn: conn, + timeout: c.Timeout.Duration(), + client: &control.NTPClient{Connection: conn}, + } + + return client, nil +} + +type ntpClient struct { + conn net.Conn + timeout time.Duration + client *control.NTPClient +} + +func (c *ntpClient) systemInfo() (map[string]string, error) { + return c.peerInfo(0) +} + +func (c *ntpClient) peerInfo(id uint16) (map[string]string, error) { + msg := &control.NTPControlMsgHead{ + VnMode: control.MakeVnMode(2, control.Mode), + REMOp: control.OpReadVariables, + AssociationID: id, + } + + if err := c.conn.SetDeadline(time.Now().Add(c.timeout)); err != nil { + return nil, err + } + + resp, err := c.client.Communicate(msg) + if err != nil { + return nil, err + } + + return resp.GetAssociationInfo() +} + +func (c *ntpClient) peerIDs() ([]uint16, error) { + msg := &control.NTPControlMsgHead{ + VnMode: control.MakeVnMode(2, control.Mode), + REMOp: control.OpReadStatus, + } + + if err := c.conn.SetDeadline(time.Now().Add(c.timeout)); err != nil { + return nil, err + } + + resp, err := c.client.Communicate(msg) + if err != nil { + return nil, err + } + + peers, err := resp.GetAssociations() + if err != nil { + return nil, err + } + + var ids []uint16 + for id := range peers { + ids = append(ids, id) + } + + return ids, nil +} + +func (c *ntpClient) close() { + if c.conn != nil { + _ = c.conn.Close() + c.conn = nil + } +} |