| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux |
| 4 | |
| 5 | package w1sensor |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | _ "embed" |
| 10 | "errors" |
| 11 | |
| 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("w1sensor", collectorapi.Creator{ |
| 20 | JobConfigSchema: configSchema, |
| 21 | Defaults: collectorapi.Defaults{ |
| 22 | UpdateEvery: 1, |
| 23 | }, |
| 24 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 25 | Config: func() any { return &Config{} }, |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | func New() *Collector { |
| 30 | return &Collector{ |
| 31 | Config: Config{ |
| 32 | SensorsPath: "/sys/bus/w1/devices", |
| 33 | }, |
| 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 | SensorsPath string `yaml:"sensors_path,omitempty" json:"sensors_path"` |
| 42 | } |
| 43 | |
| 44 | type Collector struct { |
| 45 | collectorapi.Base |
| 46 | Config `yaml:",inline" json:""` |
| 47 | |
| 48 | charts *collectorapi.Charts |
| 49 | |
| 50 | seenSensors map[string]bool |
| 51 | } |
| 52 | |
| 53 | func (c *Collector) Configuration() any { |
| 54 | return c.Config |
| 55 | } |
| 56 | |
| 57 | func (c *Collector) Init(context.Context) error { |
| 58 | if c.SensorsPath == "" { |
| 59 | return errors.New("config: no sensors path specified") |
| 60 | } |
| 61 | |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | func (c *Collector) Check(context.Context) error { |
| 66 | mx, err := c.collect() |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | if len(mx) == 0 { |
| 72 | return errors.New("no metrics collected") |
| 73 | } |
| 74 | |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | func (c *Collector) Charts() *collectorapi.Charts { |
| 79 | return c.charts |
| 80 | } |
| 81 | |
| 82 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 83 | mx, err := c.collect() |
| 84 | if err != nil { |
| 85 | c.Error(err) |
| 86 | } |
| 87 | |
| 88 | if len(mx) == 0 { |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | return mx |
| 93 | } |
| 94 | |
| 95 | func (c *Collector) Cleanup(context.Context) {} |