summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/sensors/charts.go
blob: 20df057c88230acbb0ab6a474fe00814aa0c718d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: GPL-3.0-or-later

package sensors

import (
	"fmt"
	"strings"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
)

const (
	prioSensorTemperature = module.Priority + iota
	prioSensorVoltage
	prioSensorCurrent
	prioSensorPower
	prioSensorFan
	prioSensorEnergy
	prioSensorHumidity
)

var sensorTemperatureChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_temperature",
	Title:    "Sensor temperature",
	Units:    "Celsius",
	Fam:      "temperature",
	Ctx:      "sensors.sensor_temperature",
	Type:     module.Line,
	Priority: prioSensorTemperature,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "temperature", Div: precision},
	},
}

var sensorVoltageChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_voltage",
	Title:    "Sensor voltage",
	Units:    "Volts",
	Fam:      "voltage",
	Ctx:      "sensors.sensor_voltage",
	Type:     module.Line,
	Priority: prioSensorVoltage,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "voltage", Div: precision},
	},
}

var sensorCurrentChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_current",
	Title:    "Sensor current",
	Units:    "Amperes",
	Fam:      "current",
	Ctx:      "sensors.sensor_current",
	Type:     module.Line,
	Priority: prioSensorCurrent,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "current", Div: precision},
	},
}

var sensorPowerChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_power",
	Title:    "Sensor power",
	Units:    "Watts",
	Fam:      "power",
	Ctx:      "sensors.sensor_power",
	Type:     module.Line,
	Priority: prioSensorPower,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "power", Div: precision},
	},
}

var sensorFanChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_fan",
	Title:    "Sensor fan speed",
	Units:    "RPM",
	Fam:      "fan",
	Ctx:      "sensors.sensor_fan_speed",
	Type:     module.Line,
	Priority: prioSensorFan,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "fan", Div: precision},
	},
}

var sensorEnergyChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_energy",
	Title:    "Sensor energy",
	Units:    "Joules",
	Fam:      "energy",
	Ctx:      "sensors.sensor_energy",
	Type:     module.Line,
	Priority: prioSensorEnergy,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "energy", Div: precision},
	},
}

var sensorHumidityChartTmpl = module.Chart{
	ID:       "sensor_chip_%s_feature_%s_subfeature_%s_humidity",
	Title:    "Sensor humidity",
	Units:    "percent",
	Fam:      "humidity",
	Ctx:      "sensors.sensor_humidity",
	Type:     module.Area,
	Priority: prioSensorHumidity,
	Dims: module.Dims{
		{ID: "sensor_chip_%s_feature_%s_subfeature_%s", Name: "humidity", Div: precision},
	},
}

func (s *Sensors) addSensorChart(sn sensorStats) {
	var chart *module.Chart

	switch sensorType(sn) {
	case sensorTypeTemp:
		chart = sensorTemperatureChartTmpl.Copy()
	case sensorTypeVoltage:
		chart = sensorVoltageChartTmpl.Copy()
	case sensorTypePower:
		chart = sensorPowerChartTmpl.Copy()
	case sensorTypeHumidity:
		chart = sensorHumidityChartTmpl.Copy()
	case sensorTypeFan:
		chart = sensorFanChartTmpl.Copy()
	case sensorTypeCurrent:
		chart = sensorCurrentChartTmpl.Copy()
	case sensorTypeEnergy:
		chart = sensorEnergyChartTmpl.Copy()
	default:
		return
	}

	chip, feat, subfeat := snakeCase(sn.chip), snakeCase(sn.feature), snakeCase(sn.subfeature)

	chart.ID = fmt.Sprintf(chart.ID, chip, feat, subfeat)
	chart.Labels = []module.Label{
		{Key: "chip", Value: sn.chip},
		{Key: "feature", Value: sn.feature},
	}
	for _, dim := range chart.Dims {
		dim.ID = fmt.Sprintf(dim.ID, chip, feat, subfeat)
	}

	if err := s.Charts().Add(chart); err != nil {
		s.Warning(err)
	}
}

func (s *Sensors) removeSensorChart(px string) {
	for _, chart := range *s.Charts() {
		if strings.HasPrefix(chart.ID, px) {
			chart.MarkRemove()
			chart.MarkNotCreated()
			return
		}
	}
}