From 3fe4ee8502590bc5891396dc4202bb4d76b80537 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 1 Oct 2023 21:11:29 -0700 Subject: [PATCH] Add device last seen gauge --- pkg/metrics/devices.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/metrics/devices.go b/pkg/metrics/devices.go index 2b10816..4f985e7 100644 --- a/pkg/metrics/devices.go +++ b/pkg/metrics/devices.go @@ -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} }