master
go 102 lines 2.03 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dovecot
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "time"
10
11 "github.com/netdata/netdata/go/plugins/pkg/confopt"
12 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
13 )
14
15 //go:embed "config_schema.json"
16 var configSchema string
17
18 func init() {
19 collectorapi.Register("dovecot", collectorapi.Creator{
20 JobConfigSchema: configSchema,
21 Create: func() collectorapi.CollectorV1 { return New() },
22 Config: func() any { return &Config{} },
23 })
24 }
25
26 func New() *Collector {
27 return &Collector{
28 Config: Config{
29 Address: "127.0.0.1:24242",
30 Timeout: confopt.Duration(time.Second * 1),
31 },
32 newConn: newDovecotConn,
33 charts: charts.Copy(),
34 }
35 }
36
37 type Config struct {
38 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
39 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
40 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
41 Address string `yaml:"address" json:"address"`
42 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
43 }
44
45 type Collector struct {
46 collectorapi.Base
47 Config `yaml:",inline" json:""`
48
49 charts *collectorapi.Charts
50
51 newConn func(Config) dovecotConn
52 conn dovecotConn
53 }
54
55 func (c *Collector) Configuration() any {
56 return c.Config
57 }
58
59 func (c *Collector) Init(context.Context) error {
60 if c.Address == "" {
61 return errors.New("config: 'address' not set")
62 }
63
64 return nil
65 }
66
67 func (c *Collector) Check(context.Context) error {
68 mx, err := c.collect()
69 if err != nil {
70 return err
71 }
72
73 if len(mx) == 0 {
74 return errors.New("no metrics collected")
75 }
76
77 return nil
78 }
79
80 func (c *Collector) Charts() *collectorapi.Charts {
81 return c.charts
82 }
83
84 func (c *Collector) Collect(context.Context) map[string]int64 {
85 mx, err := c.collect()
86 if err != nil {
87 c.Error(err)
88 }
89
90 if len(mx) == 0 {
91 return nil
92 }
93
94 return mx
95 }
96
97 func (c *Collector) Cleanup(context.Context) {
98 if c.conn != nil {
99 c.conn.disconnect()
100 c.conn = nil
101 }
102 }