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