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