master
go 99 lines 1.99 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package hddtemp
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("hddtemp", collectorapi.Creator{
21 JobConfigSchema: configSchema,
22 Create: func() collectorapi.CollectorV1 { return New() },
23 Config: func() any { return &Config{} },
24 })
25 }
26
27 func New() *Collector {
28 return &Collector{
29 Config: Config{
30 Address: "127.0.0.1:7634",
31 Timeout: confopt.Duration(time.Second * 1),
32 },
33 charts: &collectorapi.Charts{},
34 disks: make(map[string]bool),
35 disksTemp: make(map[string]bool),
36 }
37 }
38
39 type Config struct {
40 UpdateEvery int `yaml:"update_every" json:"update_every"`
41 Address string `yaml:"address" json:"address"`
42 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
43 Timeout confopt.Duration `yaml:"timeout" json:"timeout"`
44 }
45
46 type Collector struct {
47 collectorapi.Base
48 Config `yaml:",inline" json:""`
49
50 charts *collectorapi.Charts
51
52 conn hddtempConn
53
54 disks map[string]bool
55 disksTemp map[string]bool
56 }
57
58 func (c *Collector) Configuration() any {
59 return c.Config
60 }
61
62 func (c *Collector) Init(context.Context) error {
63 if c.Address == "" {
64 return fmt.Errorf("config: 'address' not set")
65 }
66
67 c.conn = newHddTempConn(c.Config)
68
69 return nil
70 }
71
72 func (c *Collector) Check(context.Context) error {
73 mx, err := c.collect()
74 if err != nil {
75 return err
76 }
77 if len(mx) == 0 {
78 return errors.New("no metrics collected")
79 }
80 return nil
81 }
82
83 func (c *Collector) Charts() *collectorapi.Charts {
84 return c.charts
85 }
86
87 func (c *Collector) Collect(context.Context) map[string]int64 {
88 mx, err := c.collect()
89 if err != nil {
90 c.Error(err)
91 }
92
93 if len(mx) == 0 {
94 return nil
95 }
96 return mx
97 }
98
99 func (c *Collector) Cleanup(context.Context) {}