master
go 51 lines 906 Bytes
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dovecot
4
5 import (
6 "bytes"
7
8 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket"
9 )
10
11 type dovecotConn interface {
12 connect() error
13 disconnect()
14 queryExportGlobal() ([]byte, error)
15 }
16
17 func newDovecotConn(conf Config) dovecotConn {
18 return &dovecotClient{conn: socket.New(socket.Config{
19 Address: conf.Address,
20 Timeout: conf.Timeout.Duration(),
21 })}
22 }
23
24 type dovecotClient struct {
25 conn socket.Client
26 }
27
28 func (c *dovecotClient) connect() error {
29 return c.conn.Connect()
30 }
31
32 func (c *dovecotClient) disconnect() {
33 _ = c.conn.Disconnect()
34 }
35
36 func (c *dovecotClient) queryExportGlobal() ([]byte, error) {
37 var b bytes.Buffer
38 var n int
39
40 if err := c.conn.Command("EXPORT\tglobal\n", func(bs []byte) (bool, error) {
41 b.Write(bs)
42 b.WriteByte('\n')
43
44 n++
45 return n < 2, nil
46 }); err != nil {
47 return nil, err
48 }
49
50 return b.Bytes(), nil
51 }