Collect lm-sensor metrics only when scraped (best practice for exporters)
parent
c2cf39dd43
commit
cdeb6e943c
|
@ -11,40 +11,35 @@ import (
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ncabatoff/gosensors"
|
"github.com/ncabatoff/gosensors"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fanspeed = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
fanspeedDesc = prometheus.NewDesc(
|
||||||
Namespace: "sensor",
|
"sensor_lm_fan_speed_rpm",
|
||||||
Subsystem: "lm",
|
"fan speed (rotations per minute).",
|
||||||
Name: "fan_speed_rpm",
|
[]string{"fantype", "chip", "adaptor"},
|
||||||
Help: "fan speed (rotations per minute).",
|
nil)
|
||||||
}, []string{"fantype", "chip", "adaptor"})
|
|
||||||
|
|
||||||
voltages = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
voltageDesc = prometheus.NewDesc(
|
||||||
Namespace: "sensor",
|
"sensor_lm_voltage_volts",
|
||||||
Subsystem: "lm",
|
"voltage in volts",
|
||||||
Name: "voltage_volts",
|
[]string{"intype", "chip", "adaptor"},
|
||||||
Help: "voltage in volts",
|
nil)
|
||||||
}, []string{"intype", "chip", "adaptor"})
|
|
||||||
|
|
||||||
powers = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
powerDesc = prometheus.NewDesc(
|
||||||
Namespace: "sensor",
|
"sensor_lm_power_watts",
|
||||||
Subsystem: "lm",
|
"power in watts",
|
||||||
Name: "power_watts",
|
[]string{"powertype", "chip", "adaptor"},
|
||||||
Help: "power in watts",
|
nil)
|
||||||
}, []string{"powertype", "chip", "adaptor"})
|
|
||||||
|
|
||||||
temperature = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
temperatureDesc = prometheus.NewDesc(
|
||||||
Namespace: "sensor",
|
"sensor_lm_temperature_celsius",
|
||||||
Subsystem: "lm",
|
"temperature in celsius",
|
||||||
Name: "temperature_celsius",
|
[]string{"temptype", "chip", "adaptor"},
|
||||||
Help: "temperature in celsius",
|
nil)
|
||||||
}, []string{"temptype", "chip", "adaptor"})
|
|
||||||
|
|
||||||
hddTempDesc = prometheus.NewDesc(
|
hddTempDesc = prometheus.NewDesc(
|
||||||
"sensor_hddsmart_temperature_celsius",
|
"sensor_hddsmart_temperature_celsius",
|
||||||
|
@ -53,13 +48,6 @@ var (
|
||||||
nil)
|
nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
prometheus.MustRegister(fanspeed)
|
|
||||||
prometheus.MustRegister(voltages)
|
|
||||||
prometheus.MustRegister(powers)
|
|
||||||
prometheus.MustRegister(temperature)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
listenAddress = flag.String("web.listen-address", ":9255", "Address on which to expose metrics and web interface.")
|
listenAddress = flag.String("web.listen-address", ":9255", "Address on which to expose metrics and web interface.")
|
||||||
|
@ -69,13 +57,16 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
prometheus.EnableCollectChecks(true)
|
prometheus.EnableCollectChecks(true)
|
||||||
|
|
||||||
hddcollector := NewHddCollector(*hddtempAddress)
|
hddcollector := NewHddCollector(*hddtempAddress)
|
||||||
if err := hddcollector.Init(); err != nil {
|
if err := hddcollector.Init(); err != nil {
|
||||||
log.Printf("error readding hddtemps: %v", err)
|
log.Printf("error readding hddtemps: %v", err)
|
||||||
}
|
}
|
||||||
prometheus.MustRegister(hddcollector)
|
prometheus.MustRegister(hddcollector)
|
||||||
|
|
||||||
go collectLm()
|
lmscollector := NewLmSensorsCollector()
|
||||||
|
lmscollector.Init()
|
||||||
|
prometheus.MustRegister(lmscollector)
|
||||||
|
|
||||||
http.Handle(*metricsPath, prometheus.Handler())
|
http.Handle(*metricsPath, prometheus.Handler())
|
||||||
|
|
||||||
|
@ -91,27 +82,54 @@ func main() {
|
||||||
http.ListenAndServe(*listenAddress, nil)
|
http.ListenAndServe(*listenAddress, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectLm() {
|
type (
|
||||||
gosensors.Init()
|
LmSensorsCollector struct{}
|
||||||
defer gosensors.Cleanup()
|
)
|
||||||
for {
|
|
||||||
for _, chip := range gosensors.GetDetectedChips() {
|
|
||||||
chipName := chip.String()
|
|
||||||
adaptorName := chip.AdapterName()
|
|
||||||
for _, feature := range chip.GetFeatures() {
|
|
||||||
if strings.HasPrefix(feature.Name, "fan") {
|
|
||||||
fanspeed.WithLabelValues(feature.GetLabel(), chipName, adaptorName).Set(feature.GetValue())
|
|
||||||
} else if strings.HasPrefix(feature.Name, "temp") {
|
|
||||||
temperature.WithLabelValues(feature.GetLabel(), chipName, adaptorName).Set(feature.GetValue())
|
|
||||||
} else if strings.HasPrefix(feature.Name, "in") {
|
|
||||||
voltages.WithLabelValues(feature.GetLabel(), chipName, adaptorName).Set(feature.GetValue())
|
|
||||||
} else if strings.HasPrefix(feature.Name, "power") {
|
|
||||||
powers.WithLabelValues(feature.GetLabel(), chipName, adaptorName).Set(feature.GetValue())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func NewLmSensorsCollector() *LmSensorsCollector {
|
||||||
|
return &LmSensorsCollector{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LmSensorsCollector) Init() {
|
||||||
|
gosensors.Init()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Describe implements prometheus.Collector.
|
||||||
|
func (l *LmSensorsCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
|
ch <- fanspeedDesc
|
||||||
|
ch <- powerDesc
|
||||||
|
ch <- temperatureDesc
|
||||||
|
ch <- voltageDesc
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect implements prometheus.Collector.
|
||||||
|
func (l *LmSensorsCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
|
for _, chip := range gosensors.GetDetectedChips() {
|
||||||
|
chipName := chip.String()
|
||||||
|
adaptorName := chip.AdapterName()
|
||||||
|
for _, feature := range chip.GetFeatures() {
|
||||||
|
if strings.HasPrefix(feature.Name, "fan") {
|
||||||
|
ch <- prometheus.MustNewConstMetric(fanspeedDesc,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
feature.GetValue(),
|
||||||
|
feature.GetLabel(), chipName, adaptorName)
|
||||||
|
} else if strings.HasPrefix(feature.Name, "temp") {
|
||||||
|
ch <- prometheus.MustNewConstMetric(temperatureDesc,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
feature.GetValue(),
|
||||||
|
feature.GetLabel(), chipName, adaptorName)
|
||||||
|
} else if strings.HasPrefix(feature.Name, "in") {
|
||||||
|
ch <- prometheus.MustNewConstMetric(voltageDesc,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
feature.GetValue(),
|
||||||
|
feature.GetLabel(), chipName, adaptorName)
|
||||||
|
} else if strings.HasPrefix(feature.Name, "power") {
|
||||||
|
ch <- prometheus.MustNewConstMetric(powerDesc,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
feature.GetValue(),
|
||||||
|
feature.GetLabel(), chipName, adaptorName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue