mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-01-01 00:47:30 -08:00
Export systemd timer last trigger sec.
This commit is contained in:
parent
f9e91156d0
commit
a7fd6b8743
|
@ -18,6 +18,7 @@ package collector
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/go-systemd/dbus"
|
"github.com/coreos/go-systemd/dbus"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -35,6 +36,7 @@ type systemdCollector struct {
|
||||||
unitDesc *prometheus.Desc
|
unitDesc *prometheus.Desc
|
||||||
systemRunningDesc *prometheus.Desc
|
systemRunningDesc *prometheus.Desc
|
||||||
summaryDesc *prometheus.Desc
|
summaryDesc *prometheus.Desc
|
||||||
|
timerLastTriggerDesc *prometheus.Desc
|
||||||
unitWhitelistPattern *regexp.Regexp
|
unitWhitelistPattern *regexp.Regexp
|
||||||
unitBlacklistPattern *regexp.Regexp
|
unitBlacklistPattern *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
@ -61,6 +63,9 @@ func NewSystemdCollector() (Collector, error) {
|
||||||
summaryDesc := prometheus.NewDesc(
|
summaryDesc := prometheus.NewDesc(
|
||||||
prometheus.BuildFQName(namespace, subsystem, "units"),
|
prometheus.BuildFQName(namespace, subsystem, "units"),
|
||||||
"Summary of systemd unit states", []string{"state"}, nil)
|
"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))
|
unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist))
|
||||||
unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist))
|
unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist))
|
||||||
|
|
||||||
|
@ -68,6 +73,7 @@ func NewSystemdCollector() (Collector, error) {
|
||||||
unitDesc: unitDesc,
|
unitDesc: unitDesc,
|
||||||
systemRunningDesc: systemRunningDesc,
|
systemRunningDesc: systemRunningDesc,
|
||||||
summaryDesc: summaryDesc,
|
summaryDesc: summaryDesc,
|
||||||
|
timerLastTriggerDesc: timerLastTriggerDesc,
|
||||||
unitWhitelistPattern: unitWhitelistPattern,
|
unitWhitelistPattern: unitWhitelistPattern,
|
||||||
unitBlacklistPattern: unitBlacklistPattern,
|
unitBlacklistPattern: unitBlacklistPattern,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -84,6 +90,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
|
|
||||||
units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern)
|
units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern)
|
||||||
c.collectUnitStatusMetrics(ch, units)
|
c.collectUnitStatusMetrics(ch, units)
|
||||||
|
c.collectTimers(ch, units)
|
||||||
|
|
||||||
systemState, err := c.getSystemState()
|
systemState, err := c.getSystemState()
|
||||||
if err != nil {
|
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) {
|
func (c *systemdCollector) collectSummaryMetrics(ch chan<- prometheus.Metric, summary map[string]float64) {
|
||||||
for stateName, count := range summary {
|
for stateName, count := range summary {
|
||||||
ch <- prometheus.MustNewConstMetric(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
|
Loading…
Reference in a new issue