Skip to content

Commit

Permalink
add myuplink_system_device_point_enum
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Nov 14, 2023
1 parent 4533011 commit 7530357
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 29 deletions.
56 changes: 56 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"github.com/prometheus/client_golang/prometheus"
)

type (
MyUplinkMetrics struct {
system *prometheus.GaugeVec
systemDevice *prometheus.GaugeVec
systemDevicePoint *prometheus.GaugeVec
systemDevicePointEnum *prometheus.GaugeVec
}
)

func NewMyUplinkMetrics(registry *prometheus.Registry) *MyUplinkMetrics {
metrics := &MyUplinkMetrics{}

metrics.system = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_info",
Help: "myUplink system information",
},
[]string{"systemID", "systemName", "country"},
)
registry.MustRegister(metrics.system)

metrics.systemDevice = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_device_info",
Help: "myUplink system device information",
},
[]string{"systemID", "deviceID", "deviceName", "serialNumber", "connectionState", "firmwareVersion"},
)
registry.MustRegister(metrics.systemDevice)

metrics.systemDevicePoint = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_device_point",
Help: "myUplink device metric point",
},
[]string{"systemID", "deviceID", "category", "parameterID", "parameterName", "parameterUnit"},
)
registry.MustRegister(metrics.systemDevicePoint)

metrics.systemDevicePointEnum = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_device_point_enum",
Help: "myUplink device metric point enum value",
},
[]string{"systemID", "deviceID", "category", "parameterID", "parameterName", "parameterUnit", "valueText"},
)
registry.MustRegister(metrics.systemDevicePointEnum)

return metrics
}
51 changes: 22 additions & 29 deletions probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,7 @@ func myuplinkProbe(w http.ResponseWriter, r *http.Request) {
}
}

metricSystem := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_info",
Help: "",
},
[]string{"systemID", "systemName", "country"},
)
registry.MustRegister(metricSystem)

metricSystemDevice := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_device_info",
Help: "",
},
[]string{"systemID", "deviceID", "deviceName", "serialNumber", "connectionState", "firmwareVersion"},
)
registry.MustRegister(metricSystemDevice)

metricSystemDevicePoint := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "myuplink_system_device_point",
Help: "",
},
[]string{"systemID", "deviceID", "category", "parameterID", "parameterName", "parameterUnit"},
)
registry.MustRegister(metricSystemDevicePoint)
metrics := NewMyUplinkMetrics(registry)

systemList, err := cacheResult(
"systems",
Expand All @@ -90,14 +65,14 @@ func myuplinkProbe(w http.ResponseWriter, r *http.Request) {
}

for _, system := range systemList.(*myuplink.ResultSystems).Systems {
metricSystem.With(prometheus.Labels{
metrics.system.With(prometheus.Labels{
"systemID": system.SystemID,
"systemName": system.Name,
"country": system.Country,
}).Set(1)

for _, device := range system.Devices {
metricSystemDevice.With(prometheus.Labels{
metrics.systemDevice.With(prometheus.Labels{
"systemID": system.SystemID,
"deviceID": device.ID,
"deviceName": device.Product.Name,
Expand All @@ -121,14 +96,32 @@ func myuplinkProbe(w http.ResponseWriter, r *http.Request) {

for _, devicePoint := range *devicePoints.(*myuplink.SystemDevicePoints) {
if devicePoint.Value != nil {
metricSystemDevicePoint.With(prometheus.Labels{
metrics.systemDevicePoint.With(prometheus.Labels{
"systemID": system.SystemID,
"deviceID": device.ID,
"category": devicePoint.Category,
"parameterID": devicePoint.ParameterID,
"parameterName": devicePoint.ParameterName,
"parameterUnit": devicePoint.ParameterUnit,
}).Set(*devicePoint.Value)

enumValue := fmt.Sprintf("%d", int64(*devicePoint.Value))
for _, enumVal := range devicePoint.EnumValues {
enumMetricVal := float64(0)
if enumVal.Value == enumValue {
enumMetricVal = 1
}

metrics.systemDevicePointEnum.With(prometheus.Labels{
"systemID": system.SystemID,
"deviceID": device.ID,
"category": devicePoint.Category,
"parameterID": devicePoint.ParameterID,
"parameterName": devicePoint.ParameterName,
"parameterUnit": devicePoint.ParameterUnit,
"valueText": enumVal.Text,
}).Set(enumMetricVal)
}
}
}
}
Expand Down

0 comments on commit 7530357

Please sign in to comment.