feat(go.d/sensors): add a config option to update/add sensor label value (#18707)
Ilya Mashchenko committed
Oct 7, 2024 at 17:18 UTC
4c09d264e6f15f63076734d114a924c070e1a933
6 files changed
+180
-2
src/go/plugin/go.d/modules/sensors/charts.go
+30
@@ -6,6 +6,7 @@ import (
6
"fmt"
7
"strings"
8
9
+ "github.com/netdata/netdata/go/plugins/pkg/matcher"
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
11
"github.com/netdata/netdata/go/plugins/plugin/go.d/modules/sensors/lmsensors"
12
)
@@ -485,6 +486,10 @@ func (s *Sensors) addCharts(charts *module.Charts, chipUniqueName, chipSysDevice
486
return
487
}
488
489
+ if lbl := s.relabel(chipUniqueName, snName); lbl != "" {
490
+ snLabel = lbl
491
+ }
492
+
493
for _, chart := range *charts {
494
chart.ID = fmt.Sprintf(chart.ID, chipUniqueName, snName)
495
chart.ID = cleanChartId(chart.ID)
@@ -516,6 +521,31 @@ func (s *Sensors) removeSensorChart(px string) {
521
}
522
}
523
524
+func (s *Sensors) relabel(chipUniqueName, snName string) string {
525
+ for _, rv := range s.Relabel {
526
+ if rv.Chip == "" {
527
+ return ""
528
+ }
529
+
530
+ mr, err := matcher.NewSimplePatternsMatcher(rv.Chip)
531
+ if err != nil {
532
+ s.Debugf("failed to create simple pattern matcher from '%s': %v", rv.Chip, err)
533
+ return ""
534
+ }
535
+
536
+ if !mr.MatchString(chipUniqueName) {
537
+ return ""
538
+ }
539
+
540
+ for _, sv := range rv.Sensors {
541
+ if sv.Name == snName {
542
+ return sv.Label
543
+ }
544
+ }
545
+ }
546
+ return ""
547
+}
548
+
549
func cleanChartId(id string) string {
550
r := strings.NewReplacer(" ", "_", ".", "_")
551
return strings.ToLower(r.Replace(id))
src/go/plugin/go.d/modules/sensors/config_schema.json
+94
@@ -10,6 +10,68 @@
10
"type": "integer",
11
"minimum": 1,
12
"default": 10
13
+ },
14
+ "relabel": {
15
+ "title": "Update Labels",
16
+ "description": " This configuration can be used to update existing sensor labels or add labels to sensors that don't have them.",
17
+ "type": [
18
+ "array",
19
+ "null"
20
+ ],
21
+ "items": {
22
+ "title": "",
23
+ "description": "",
24
+ "type": [
25
+ "object",
26
+ "null"
27
+ ],
28
+ "properties": {
29
+ "chip": {
30
+ "title": "Chip",
31
+ "description": "[Pattern](https://github.com/netdata/netdata/tree/master/src/go/pkg/matcher#simple-patterns-matcher) to match the `chip_id` label.",
32
+ "type": "string"
33
+ },
34
+ "sensors": {
35
+ "title": "Relabel",
36
+ "description": "A list of sensors to be relabeled for the specified chip. The sensor name will be matched against the `sensor` label value.",
37
+ "type": [
38
+ "array",
39
+ "null"
40
+ ],
41
+ "items": {
42
+ "title": "Sensor",
43
+ "description": "",
44
+ "type": [
45
+ "object",
46
+ "null"
47
+ ],
48
+ "properties": {
49
+ "name": {
50
+ "title": "Name",
51
+ "description": "",
52
+ "type": "string"
53
+ },
54
+ "label": {
55
+ "title": "New label",
56
+ "description": "",
57
+ "type": "string"
58
+ }
59
+ },
60
+ "required": [
61
+ "name",
62
+ "label"
63
+ ]
64
+ },
65
+ "minItems": 1,
66
+ "uniqueItems": true
67
+ }
68
+ },
69
+ "required": [
70
+ "chip",
71
+ "sensors"
72
+ ]
73
+ },
74
+ "uniqueItems": true
75
}
76
},
77
"required": [
@@ -22,6 +84,38 @@
84
"uiSchema": {
85
"uiOptions": {
86
"fullPage": true
87
+ },
88
+ "relabel": {
89
+ "items": {
90
+ "chip": {
91
+ "ui:placeholder": "it8688-*"
92
+ },
93
+ "sensors": {
94
+ "ui:listFlavour": "list",
95
+ "items": {
96
+ "name": {
97
+ "ui:placeholder": "For example, temp1, in1, or voltage1."
98
+ }
99
+ }
100
+ }
101
+ }
102
+ },
103
+ "ui:flavour": "tabs",
104
+ "ui:options": {
105
+ "tabs": [
106
+ {
107
+ "title": "Base",
108
+ "fields": [
109
+ "update_every"
110
+ ]
111
+ },
112
+ {
113
+ "title": "Relabel Sensors",
114
+ "fields": [
115
+ "relabel"
116
+ ]
117
+ }
118
+ ]
119
}
120
}
121
}
src/go/plugin/go.d/modules/sensors/metadata.yaml
+32
-1
@@ -73,6 +73,26 @@ modules:
73
description: Data collection frequency.
74
default_value: 10
75
required: false
76
+ - name: relabel
77
+ description: A list used to update existing sensor labels or add labels to sensors that don't have them.
78
+ default_value: "[]"
79
+ required: false
80
+ - name: relabel[].chip
81
+ description: "[Pattern](/src/libnetdata/simple_pattern/README.md#simple-patterns) to match the `chip_id` label value."
82
+ default_value: ""
83
+ required: false
84
+ - name: relabel[].sensors
85
+ description: A list of sensors to be relabeled for the specified chip.
86
+ default_value: "[]"
87
+ required: false
88
+ - name: relabel[].sensors[].name
89
+ description: The exact sensor name (e.g., `'temp1'`, `'in1'`, `'voltage1'`).
90
+ default_value: ""
91
+ required: false
92
+ - name: relabel[].sensors[].label
93
+ description: The new label value for the sensor.
94
+ default_value: ""
95
+ required: false
96
examples:
97
folding:
98
title: Config
@@ -84,6 +104,18 @@ modules:
104
jobs:
105
- name: sensors
106
update_every: 5 # Collect sensors statistics every 5 seconds
107
+ - name: Renaming labels
108
+ description: Allows you to override/add labels.
109
+ config: |
110
+ jobs:
111
+ - name: sensors
112
+ relabel:
113
+ - chip: as99127f-*
114
+ sensors:
115
+ - name: temp1
116
+ label: Mobo Temp
117
+ - name: temp2
118
+ label: CPU0 Temp
119
troubleshooting:
120
problems:
121
list: []
@@ -106,7 +138,6 @@ modules:
138
description: The name of the specific sensor within the chip device. This provides a direct identifier for the individual measurement point.
139
- name: label
140
description: A label provided by the kernel driver to indicate the intended use or purpose of the sensor.
109
-
141
metrics:
142
- name: sensors.chip_sensor_temperature
143
description: Sensor Temperature
src/go/plugin/go.d/modules/sensors/sensors.go
+7
@@ -34,6 +34,13 @@ func New() *Sensors {
34
35
type Config struct {
36
UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
37
+ Relabel []struct {
38
+ Chip string `yaml:"chip" json:"chip"`
39
+ Sensors []struct {
40
+ Name string `yaml:"name" json:"name"`
41
+ Label string `yaml:"label" json:"label"`
42
+ } `yaml:"sensors,omitempty" json:"sensors"`
43
+ } `yaml:"relabel,omitempty" json:"relabel"`
44
}
45
46
type (
src/go/plugin/go.d/modules/sensors/testdata/config.json
+12
-1
@@ -1,3 +1,14 @@
1
{
2
- "update_every": 123
2
+ "update_every": 123,
3
+ "relabel": [
4
+ {
5
+ "chip": "ok",
6
+ "sensors": [
7
+ {
8
+ "name": "ok",
9
+ "label": "ok"
10
+ }
11
+ ]
12
+ }
13
+ ]
14
}
src/go/plugin/go.d/modules/sensors/testdata/config.yaml
+5
@@ -1 +1,6 @@
1
update_every: 123
2
+relabel:
3
+ - chip: "ok"
4
+ sensors:
5
+ - name: "ok"
6
+ label: "ok"