mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-02-02 08:42:31 -08:00
Only use one dbus connection, required some refactoring.
This commit is contained in:
parent
a7fd6b8743
commit
0316bacceb
|
@ -101,7 +101,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric, units []dbus.UnitStatus) {
|
func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric, units []unit) {
|
||||||
for _, unit := range units {
|
for _, unit := range units {
|
||||||
for _, stateName := range unitStatesName {
|
for _, stateName := range unitStatesName {
|
||||||
isActive := 0.0
|
isActive := 0.0
|
||||||
|
@ -115,28 +115,15 @@ func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []dbus.UnitStatus) error {
|
func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []unit) 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 {
|
for _, unit := range units {
|
||||||
if !strings.HasSuffix(unit.Name, ".timer") {
|
if !strings.HasSuffix(unit.Name, ".timer") {
|
||||||
continue
|
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(
|
ch <- prometheus.MustNewConstMetric(
|
||||||
c.timerLastTriggerDesc, prometheus.GaugeValue,
|
c.timerLastTriggerDesc, prometheus.GaugeValue,
|
||||||
float64(lastTrigger)/1e6, unit.Name)
|
float64(unit.lastTriggerUsec)/1e6, unit.Name)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -163,22 +150,45 @@ func (c *systemdCollector) newDbus() (*dbus.Conn, error) {
|
||||||
return dbus.New()
|
return dbus.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *systemdCollector) getAllUnits() ([]dbus.UnitStatus, error) {
|
type unit struct {
|
||||||
|
dbus.UnitStatus
|
||||||
|
lastTriggerUsec uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
conn, err := c.newDbus()
|
conn, err := c.newDbus()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("couldn't get dbus connection: %s", err)
|
return nil, fmt.Errorf("couldn't get dbus connection: %s", err)
|
||||||
}
|
}
|
||||||
allUnits, err := conn.ListUnits()
|
defer conn.Close()
|
||||||
conn.Close()
|
|
||||||
|
|
||||||
|
allUnits, err := conn.ListUnits()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []dbus.UnitStatus{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return allUnits, nil
|
result := make([]unit, 0, len(allUnits))
|
||||||
|
for _, status := range allUnits {
|
||||||
|
unit := unit{
|
||||||
|
UnitStatus: status,
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(unit.Name, ".timer") {
|
||||||
|
lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("couldn't get unit '%s' LastTriggerUSec: %s", unit.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
unit.lastTriggerUsec = lastTriggerValue.Value.Value().(uint64)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func summarizeUnits(units []dbus.UnitStatus) map[string]float64 {
|
func summarizeUnits(units []unit) map[string]float64 {
|
||||||
summarized := make(map[string]float64)
|
summarized := make(map[string]float64)
|
||||||
|
|
||||||
for _, unitStateName := range unitStatesName {
|
for _, unitStateName := range unitStatesName {
|
||||||
|
@ -192,8 +202,8 @@ func summarizeUnits(units []dbus.UnitStatus) map[string]float64 {
|
||||||
return summarized
|
return summarized
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterUnits(units []dbus.UnitStatus, whitelistPattern, blacklistPattern *regexp.Regexp) []dbus.UnitStatus {
|
func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp) []unit {
|
||||||
filtered := make([]dbus.UnitStatus, 0, len(units))
|
filtered := make([]unit, 0, len(units))
|
||||||
for _, unit := range units {
|
for _, unit := range units {
|
||||||
if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) {
|
if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) {
|
||||||
filtered = append(filtered, unit)
|
filtered = append(filtered, unit)
|
||||||
|
|
Loading…
Reference in a new issue