| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux |
| 4 | |
| 5 | package sensors |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | _ "embed" |
| 10 | "errors" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/sensors/lmsensors" |
| 14 | ) |
| 15 | |
| 16 | //go:embed "config_schema.json" |
| 17 | var configSchema string |
| 18 | |
| 19 | func init() { |
| 20 | collectorapi.Register("sensors", collectorapi.Creator{ |
| 21 | JobConfigSchema: configSchema, |
| 22 | Defaults: collectorapi.Defaults{ |
| 23 | UpdateEvery: 10, |
| 24 | Disabled: true, |
| 25 | }, |
| 26 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 27 | Config: func() any { return &Config{} }, |
| 28 | }) |
| 29 | } |
| 30 | |
| 31 | func New() *Collector { |
| 32 | return &Collector{ |
| 33 | Config: Config{}, |
| 34 | charts: &collectorapi.Charts{}, |
| 35 | seenSensors: make(map[string]bool), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | type Config struct { |
| 40 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 41 | Relabel []struct { |
| 42 | Chip string `yaml:"chip" json:"chip"` |
| 43 | Sensors []struct { |
| 44 | Name string `yaml:"name" json:"name"` |
| 45 | Label string `yaml:"label" json:"label"` |
| 46 | } `yaml:"sensors,omitempty" json:"sensors"` |
| 47 | } `yaml:"relabel,omitempty" json:"relabel"` |
| 48 | } |
| 49 | |
| 50 | type ( |
| 51 | Collector struct { |
| 52 | collectorapi.Base |
| 53 | Config `yaml:",inline" json:""` |
| 54 | |
| 55 | charts *collectorapi.Charts |
| 56 | |
| 57 | sc sysfsScanner |
| 58 | |
| 59 | seenSensors map[string]bool |
| 60 | } |
| 61 | sysfsScanner interface { |
| 62 | Scan() ([]*lmsensors.Chip, error) |
| 63 | } |
| 64 | ) |
| 65 | |
| 66 | func (c *Collector) Configuration() any { |
| 67 | return c.Config |
| 68 | } |
| 69 | |
| 70 | func (c *Collector) Init(context.Context) error { |
| 71 | sc := lmsensors.New() |
| 72 | sc.Logger = c.Logger |
| 73 | c.sc = sc |
| 74 | |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | func (c *Collector) Check(context.Context) error { |
| 79 | mx, err := c.collect() |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | if len(mx) == 0 { |
| 85 | return errors.New("no metrics collected") |
| 86 | } |
| 87 | |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func (c *Collector) Charts() *collectorapi.Charts { |
| 92 | return c.charts |
| 93 | } |
| 94 | |
| 95 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 96 | mx, err := c.collect() |
| 97 | if err != nil { |
| 98 | c.Error(err) |
| 99 | } |
| 100 | |
| 101 | if len(mx) == 0 { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | return mx |
| 106 | } |
| 107 | |
| 108 | func (c *Collector) Cleanup(context.Context) {} |