148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
// fieldMapping maps a single value inside a Shelly component status to a Prometheus gauge.
|
|
type fieldMapping struct {
|
|
// Component is the Shelly component type, e.g. "cloud" or "smoke".
|
|
// Indexed components (e.g. "smoke:0") get an additional "id" label.
|
|
Component string
|
|
Indexed bool
|
|
// Path is the JSON path inside the component status, e.g. ["battery", "V"].
|
|
Path []string
|
|
Gauge *prometheus.GaugeVec
|
|
}
|
|
|
|
var (
|
|
deviceLabels = []string{"device"}
|
|
indexedLabels = []string{"device", "id"}
|
|
|
|
onlineGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_online",
|
|
Help: "Whether the device is online (1) or offline (0), as reported on the <device>/online topic.",
|
|
}, deviceLabels)
|
|
|
|
lastSeenGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_last_message_timestamp_seconds",
|
|
Help: "Unix timestamp of the last MQTT message received from the device.",
|
|
}, deviceLabels)
|
|
|
|
// mappings defines which status values get exported. Add new entries here to export more data.
|
|
mappings = []fieldMapping{
|
|
{
|
|
Component: "cloud",
|
|
Path: []string{"connected"},
|
|
Gauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_cloud_connected",
|
|
Help: "Whether the device is connected to the Shelly cloud.",
|
|
}, deviceLabels),
|
|
},
|
|
{
|
|
Component: "mqtt",
|
|
Path: []string{"connected"},
|
|
Gauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_mqtt_connected",
|
|
Help: "Whether the device is connected to the MQTT broker.",
|
|
}, deviceLabels),
|
|
},
|
|
{
|
|
Component: "smoke",
|
|
Indexed: true,
|
|
Path: []string{"alarm"},
|
|
Gauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_smoke_alarm",
|
|
Help: "Whether the smoke alarm is active.",
|
|
}, indexedLabels),
|
|
},
|
|
{
|
|
Component: "smoke",
|
|
Indexed: true,
|
|
Path: []string{"mute"},
|
|
Gauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_smoke_mute",
|
|
Help: "Whether the smoke alarm is muted.",
|
|
}, indexedLabels),
|
|
},
|
|
{
|
|
Component: "devicepower",
|
|
Indexed: true,
|
|
Path: []string{"battery", "V"},
|
|
Gauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_battery_voltage_volts",
|
|
Help: "Battery voltage in volts.",
|
|
}, indexedLabels),
|
|
},
|
|
{
|
|
Component: "devicepower",
|
|
Indexed: true,
|
|
Path: []string{"battery", "percent"},
|
|
Gauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
Name: "shelly_battery_percent",
|
|
Help: "Battery charge in percent.",
|
|
}, indexedLabels),
|
|
},
|
|
}
|
|
)
|
|
|
|
func registerMetrics(reg prometheus.Registerer) {
|
|
reg.MustRegister(onlineGauge, lastSeenGauge)
|
|
for _, m := range mappings {
|
|
reg.MustRegister(m.Gauge)
|
|
}
|
|
}
|
|
|
|
// applyComponentStatus updates all gauges mapped to the given component.
|
|
// key is the Shelly component key, e.g. "cloud" or "smoke:0". Missing fields are
|
|
// ignored, so partial updates (NotifyStatus events) only touch the values they contain.
|
|
func applyComponentStatus(device, key string, status map[string]any) {
|
|
component, id, indexed := strings.Cut(key, ":")
|
|
|
|
for _, m := range mappings {
|
|
if m.Component != component || m.Indexed != indexed {
|
|
continue
|
|
}
|
|
value, ok := lookupNumber(status, m.Path)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if m.Indexed {
|
|
m.Gauge.WithLabelValues(device, id).Set(value)
|
|
} else {
|
|
m.Gauge.WithLabelValues(device).Set(value)
|
|
}
|
|
}
|
|
}
|
|
|
|
// lookupNumber walks path inside data and converts the found value to a float.
|
|
func lookupNumber(data map[string]any, path []string) (float64, bool) {
|
|
var current any = data
|
|
for _, key := range path {
|
|
obj, ok := current.(map[string]any)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
current, ok = obj[key]
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
}
|
|
return toFloat(current)
|
|
}
|
|
|
|
func toFloat(v any) (float64, bool) {
|
|
switch val := v.(type) {
|
|
case float64:
|
|
return val, true
|
|
case bool:
|
|
if val {
|
|
return 1, true
|
|
}
|
|
return 0, true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|