| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | //go:build linux |
| 4 | |
| 5 | package w1sensor |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | prioTemperature = collectorapi.Priority + iota |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | sensorChartTmpl = collectorapi.Chart{ |
| 20 | ID: "w1sensor_%s_temperature", |
| 21 | Title: "1-Wire Temperature Sensor", |
| 22 | Units: "Celsius", |
| 23 | Fam: "Temperature", |
| 24 | Ctx: "w1sensor.temperature", |
| 25 | Type: collectorapi.Line, |
| 26 | Priority: prioTemperature, |
| 27 | Dims: collectorapi.Dims{ |
| 28 | {ID: "w1sensor_%s_temperature", Div: precision}, |
| 29 | }, |
| 30 | } |
| 31 | ) |
| 32 | |
| 33 | func (c *Collector) addSensorChart(id string) { |
| 34 | chart := sensorChartTmpl.Copy() |
| 35 | |
| 36 | chart.ID = fmt.Sprintf(chart.ID, id) |
| 37 | chart.Labels = []collectorapi.Label{ |
| 38 | {Key: "sensor_id", Value: id}, |
| 39 | } |
| 40 | |
| 41 | for _, dim := range chart.Dims { |
| 42 | dim.ID = fmt.Sprintf(dim.ID, id) |
| 43 | } |
| 44 | |
| 45 | if err := c.Charts().Add(chart); err != nil { |
| 46 | c.Warning(err) |
| 47 | } |
| 48 | |
| 49 | } |
| 50 | |
| 51 | func (c *Collector) removeSensorChart(id string) { |
| 52 | px := fmt.Sprintf("w1sensor_%s", id) |
| 53 | for _, chart := range *c.Charts() { |
| 54 | if strings.HasPrefix(chart.ID, px) { |
| 55 | chart.MarkRemove() |
| 56 | chart.MarkNotCreated() |
| 57 | } |
| 58 | } |
| 59 | } |