Add device last seen gauge

This commit is contained in:
Joshua Peek 2023-10-01 21:11:29 -07:00 committed by GitHub
parent 1dd976e37f
commit 3fe4ee8502
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,28 @@ func collectDevices(client *tailscale.Client) []prometheus.Collector {
[]string{"id", "created", "name"},
)
deviceLastSeen := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_devices_last_seen",
Help: "The last time the device was active on the tailnet",
ConstLabels: prometheus.Labels{
"tailnet": client.GetTailnet(),
},
},
[]string{"id", "created", "name"},
)
deviceLastSeenAgo := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_devices_last_seen_ago",
Help: "The number of seconds since the device was last active on the tailnet",
ConstLabels: prometheus.Labels{
"tailnet": client.GetTailnet(),
},
},
[]string{"id", "created", "name"},
)
deviceUpdateAvailable := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tailscale_devices_update_available",
@ -51,6 +73,8 @@ func collectDevices(client *tailscale.Client) []prometheus.Collector {
// Reset gauges so we don't leave old devices around
deviceExpiry.Reset()
deviceSecondsRemaining.Reset()
deviceLastSeen.Reset()
deviceLastSeenAgo.Reset()
deviceUpdateAvailable.Reset()
for _, device := range devices {
@ -61,6 +85,10 @@ func collectDevices(client *tailscale.Client) []prometheus.Collector {
deviceSecondsRemaining.With(prometheus.Labels{"id": device.ID, "created": device.Created.String(), "name": device.Name}).Set(remainingSeconds)
}
secondsAgo := time.Since(device.LastSeen.Time).Seconds()
deviceLastSeen.With(prometheus.Labels{"id": device.ID, "created": device.Created.String(), "name": device.Name}).Set(float64(device.LastSeen.Unix()))
deviceLastSeenAgo.With(prometheus.Labels{"id": device.ID, "created": device.Created.String(), "name": device.Name}).Set(secondsAgo)
updateAvailable := 0.0
if device.UpdateAvailable {
updateAvailable = 1.0
@ -73,5 +101,5 @@ func collectDevices(client *tailscale.Client) []prometheus.Collector {
}
}()
return []prometheus.Collector{deviceExpiry, deviceSecondsRemaining, deviceUpdateAvailable}
return []prometheus.Collector{deviceExpiry, deviceSecondsRemaining, deviceLastSeen, deviceLastSeenAgo, deviceUpdateAvailable}
}