diff options
Diffstat (limited to 'src/go/collectors/go.d.plugin/modules/ntpd/client.go')
-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 0000000000..5164c80e8b --- /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 + } +} |