From a7fd6b874387e5adb907cd6d7745c3bf9063c684 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Wed, 31 Jan 2018 15:06:58 +0000 Subject: [PATCH] Export systemd timer last trigger sec. --- collector/systemd_linux.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index cbb951d1..7713f61e 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go @@ -18,6 +18,7 @@ package collector import ( "fmt" "regexp" + "strings" "github.com/coreos/go-systemd/dbus" "github.com/prometheus/client_golang/prometheus" @@ -35,6 +36,7 @@ type systemdCollector struct { unitDesc *prometheus.Desc systemRunningDesc *prometheus.Desc summaryDesc *prometheus.Desc + timerLastTriggerDesc *prometheus.Desc unitWhitelistPattern *regexp.Regexp unitBlacklistPattern *regexp.Regexp } @@ -61,6 +63,9 @@ func NewSystemdCollector() (Collector, error) { summaryDesc := prometheus.NewDesc( prometheus.BuildFQName(namespace, subsystem, "units"), "Summary of systemd unit states", []string{"state"}, nil) + timerLastTriggerDesc := prometheus.NewDesc( + prometheus.BuildFQName(namespace, subsystem, "timer_last_trigger_seconds"), + "Seconds since epoch of last trigger.", []string{"name"}, nil) unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist)) unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist)) @@ -68,6 +73,7 @@ func NewSystemdCollector() (Collector, error) { unitDesc: unitDesc, systemRunningDesc: systemRunningDesc, summaryDesc: summaryDesc, + timerLastTriggerDesc: timerLastTriggerDesc, unitWhitelistPattern: unitWhitelistPattern, unitBlacklistPattern: unitBlacklistPattern, }, nil @@ -84,6 +90,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error { units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern) c.collectUnitStatusMetrics(ch, units) + c.collectTimers(ch, units) systemState, err := c.getSystemState() if err != nil { @@ -108,6 +115,32 @@ func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric, } } +func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []dbus.UnitStatus) error { + conn, err := c.newDbus() + if err != nil { + return fmt.Errorf("couldn't get dbus connection: %s", err) + } + defer conn.Close() + + for _, unit := range units { + if !strings.HasSuffix(unit.Name, ".timer") { + continue + } + + lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec") + if err != nil { + log.Errorf("couldn't get unit '%s' LastTriggerUSec: %s", unit.Name, err) + continue + } + + lastTrigger := lastTriggerValue.Value.Value().(uint64) + ch <- prometheus.MustNewConstMetric( + c.timerLastTriggerDesc, prometheus.GaugeValue, + float64(lastTrigger)/1e6, unit.Name) + } + return nil +} + func (c *systemdCollector) collectSummaryMetrics(ch chan<- prometheus.Metric, summary map[string]float64) { for stateName, count := range summary { ch <- prometheus.MustNewConstMetric(