Skip to content

Commit

Permalink
ignore datapoints from offline devices
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Dec 12, 2023
1 parent 76bcf4a commit 53a5b63
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions config/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type (
ClientID string `long:"myuplink.auth.clientid" env:"MYUPLINK_AUTH_CLIENTID" description:"ClientID from myUplink" required:"yes"`
ClientSecret string `long:"myuplink.auth.clientsecret" env:"MYUPLINK_AUTH_CLIENTSECRET" description:"ClientSecret from myUplink" json:"-" required:"yes"`
}

Device struct {
AllowedConnectionStates []string `long:"myuplink.device.allowed-connectionstates" env:"MYUPLINK_DEVICE_ALLOWED_CONNECTIONSTATES" env-delim:"," description:"Allowed device connection states" default:"Connected"`
}
}

// general options
Expand Down
41 changes: 27 additions & 14 deletions myuplink/results.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package myuplink

import (
"strings"
"time"
)

Expand All @@ -10,23 +11,25 @@ type (
ItemsPerPage int `json:"itemsPerPage"`
NumItems int `json:"numItems"`
Systems []struct {
SystemID string `json:"systemId"`
Name string `json:"name"`
SecurityLevel string `json:"securityLevel"`
HasAlarm bool `json:"hasAlarm"`
Country string `json:"country"`
Devices []struct {
ID string `json:"id"`
ConnectionState string `json:"connectionState"`
CurrentFwVersion string `json:"currentFwVersion"`
Product struct {
SerialNumber string `json:"serialNumber"`
Name string `json:"name"`
} `json:"product"`
} `json:"devices"`
SystemID string `json:"systemId"`
Name string `json:"name"`
SecurityLevel string `json:"securityLevel"`
HasAlarm bool `json:"hasAlarm"`
Country string `json:"country"`
Devices []SystemDevice `json:"devices"`
} `json:"systems"`
}

SystemDevice struct {
ID string `json:"id"`
ConnectionState string `json:"connectionState"`
CurrentFwVersion string `json:"currentFwVersion"`
Product struct {
SerialNumber string `json:"serialNumber"`
Name string `json:"name"`
} `json:"product"`
}

SystemDeviceFirmwareInfo struct {
DeviceID string `json:"deviceId"`
FirmwareID string `json:"firmwareId"`
Expand Down Expand Up @@ -57,3 +60,13 @@ type (
ZoneID string `json:"zoneId"`
}
)

func (d *SystemDevice) IsConnectionStateAllowed(allowedValues []string) bool {
for _, allowedVal := range allowedValues {
if strings.EqualFold(d.ConnectionState, allowedVal) {
return true
}
}

return false
}
5 changes: 5 additions & 0 deletions probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func myuplinkProbe(w http.ResponseWriter, r *http.Request) {
}).Set(1)

for _, device := range system.Devices {
if !device.IsConnectionStateAllowed(opts.MyUplink.Device.AllowedConnectionStates) {
contextLogger.Warnf(`ignoring system "%s" device "%s", connection state is "%s"`, system.Name, device.ID, device.ConnectionState)
continue
}

metrics.systemDevice.With(prometheus.Labels{
"systemID": system.SystemID,
"deviceID": device.ID,
Expand Down

0 comments on commit 53a5b63

Please sign in to comment.