Merge pull request #1 from josh/device-last-seen

Device last seen
This commit is contained in:
Marcus Noble 2023-10-02 10:23:54 +01:00 committed by GitHub
commit e5470dec4f
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"}, []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( deviceUpdateAvailable := prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "tailscale_devices_update_available", 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 // Reset gauges so we don't leave old devices around
deviceExpiry.Reset() deviceExpiry.Reset()
deviceSecondsRemaining.Reset() deviceSecondsRemaining.Reset()
deviceLastSeen.Reset()
deviceLastSeenAgo.Reset()
deviceUpdateAvailable.Reset() deviceUpdateAvailable.Reset()
for _, device := range devices { 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) 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 updateAvailable := 0.0
if device.UpdateAvailable { if device.UpdateAvailable {
updateAvailable = 1.0 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}
} }