You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB
Go

package client
import (
"fmt"
"strconv"
"strings"
)
// Value from ohwm
type Value struct {
Hardware []Hardware
Unit Sensor
Label string
Value string
}
// Float64 gets value as float64
func (v *Value) Float64() float64 {
val := strings.Split(v.Value, " ")
if len(val) == 0 {
return 0
}
ret, err := strconv.ParseFloat(val[0], 64)
if err != nil {
return 0
}
return ret
}
// MetricName returns value as a metric name
func (v *Value) MetricName() (string, map[string]string) {
labels := map[string]string{}
segments := []string{"ohwm"}
for _, hw := range v.Hardware {
metricname, ok := hardwareToMetric[hw.Type]
if !ok {
metricname = hardwareToMetric[UnknownHardware]
}
labels["number"] = fmt.Sprintf("%d", hw.TypeIndex+1)
labels["total"] = fmt.Sprintf("%d", hw.TypeCount)
segments = append(segments, metricname)
}
sensorname, ok := sensorToMetric[v.Unit]
if !ok {
sensorname = sensorToMetric[UnknownSensor]
}
labels["name"] = sensorname
labels["label"] = v.Label
ret := strings.Join(segments, "_")
return strings.ReplaceAll(ret, "-", "_"), labels
}