Skip to content

Commit

Permalink
Merge pull request #316 from kongfei605/systemd
Browse files Browse the repository at this point in the history
build !linux targets
  • Loading branch information
kongfei605 authored Jan 5, 2023
2 parents ef68cd9 + 9a44057 commit 8a3559a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 58 deletions.
53 changes: 53 additions & 0 deletions inputs/systemd/systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package systemd

import (
"regexp"

"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"github.com/coreos/go-systemd/v22/dbus"
)

const (
// minSystemdVersionSystemState is the minimum SystemD version for availability of
// the 'SystemState' manager property and the timer property 'LastTriggerUSec'
// https://github.com/prometheus/node_exporter/issues/291
minSystemdVersionSystemState = 212
inputName = `systemd`
)

type (
Systemd struct {
config.PluginConfig
Enable bool `toml:"enable"`

UnitInclude string `toml:"unit_include"`
UnitExclude string `toml:"unit_exclude"`

unitIncludePattern *regexp.Regexp `toml:"-"`
unitExcludePattern *regexp.Regexp `toml:"-"`

SystemdPrivate bool `toml:"systemd_private"`
EnableTaskMetrics bool `toml:"enable_task_metrics"`
EnableRestartMetrics bool `toml:"enable_restarts_metrics"`
EnableStartTimeMetrics bool `toml:"enable_start_time_metrics"`

conn *dbus.Conn
}
unit struct {
dbus.UnitStatus
}
)

var _ inputs.SampleGatherer = new(Systemd)

var (
systemdVersionRE = regexp.MustCompile(`[0-9]{3,}(\.[0-9]+)?`)
unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"}
)

func init() {
inputs.Add(inputName, func() inputs.Input {
return &Systemd{}
})
}
78 changes: 20 additions & 58 deletions inputs/systemd/systemd_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build linux
// +build linux

// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,56 +27,12 @@ import (
"sync"
"time"

"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/types"
"github.com/coreos/go-systemd/v22/dbus"
)

const (
// minSystemdVersionSystemState is the minimum SystemD version for availability of
// the 'SystemState' manager property and the timer property 'LastTriggerUSec'
// https://github.com/prometheus/node_exporter/issues/291
minSystemdVersionSystemState = 212
inputName = `systemd`
)

type (
Systemd struct {
config.PluginConfig
Enable bool `toml:"enable"`

UnitInclude string `toml:"unit_include"`
UnitExclude string `toml:"unit_exclude"`

unitIncludePattern *regexp.Regexp `toml:"-"`
unitExcludePattern *regexp.Regexp `toml:"-"`

SystemdPrivate bool `toml:"systemd_private"`
EnableTaskMetrics bool `toml:"enable_task_metrics"`
EnableRestartMetrics bool `toml:"enable_restarts_metrics"`
EnableStartTimeMetrics bool `toml:"enable_start_time_metrics"`

conn *dbus.Conn
}
unit struct {
dbus.UnitStatus
}
)

var _ inputs.SampleGatherer = new(Systemd)

var (
systemdVersionRE = regexp.MustCompile(`[0-9]{3,}(\.[0-9]+)?`)
unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"}
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/types"
)

func init() {
inputs.Add(inputName, func() inputs.Input {
return &Systemd{}
})
}

// Init returns a new Collector exposing systemd statistics.
func (s *Systemd) Init() error {
if !s.Enable {
Expand Down Expand Up @@ -104,7 +63,6 @@ func (s *Systemd) Close() {
// Gather gathers metrics from systemd. Dbus collection is done in parallel
// to reduce wait time for responses.
func (s *Systemd) Gather(slist *types.SampleList) {
log.Println("systemd gather starting...")
begin := time.Now()

systemdVersion, systemdVersionFull := s.getSystemdVersion()
Expand All @@ -122,11 +80,15 @@ func (s *Systemd) Gather(slist *types.SampleList) {
begin = time.Now()
summary := summarizeUnits(allUnits)
s.collectSummaryMetrics(slist, summary)
log.Println("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds())
if config.Config.DebugMode {
log.Println("D!", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds())
}

begin = time.Now()
units := filterUnits(allUnits, s.unitIncludePattern, s.unitExcludePattern)
log.Println("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds())
if config.Config.DebugMode {
log.Println("D!", "filterUnits took", "duration_seconds", time.Since(begin).Seconds())
}

var wg sync.WaitGroup
defer wg.Wait()
Expand Down Expand Up @@ -187,14 +149,14 @@ func (s *Systemd) collectUnitStatusMetrics(slist *types.SampleList, units []unit
if strings.HasSuffix(unit.Name, ".service") {
serviceTypeProperty, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Service", "Type")
if err != nil {
log.Println("msg", "couldn't get unit type", "unit", unit.Name, "err", err)
log.Println("E!", "couldn't get unit type", "unit", unit.Name, "err", err)
} else {
serviceType = serviceTypeProperty.Value.Value().(string)
}
} else if strings.HasSuffix(unit.Name, ".mount") {
serviceTypeProperty, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Mount", "Type")
if err != nil {
log.Println("msg", "couldn't get unit type", "unit", unit.Name, "err", err)
log.Println("E!", "couldn't get unit type", "unit", unit.Name, "err", err)
} else {
serviceType = serviceTypeProperty.Value.Value().(string)
}
Expand All @@ -211,7 +173,7 @@ func (s *Systemd) collectUnitStatusMetrics(slist *types.SampleList, units []unit
// NRestarts wasn't added until systemd 235.
restartsCount, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Service", "NRestarts")
if err != nil {
log.Println("msg", "couldn't get unit NRestarts", "unit", unit.Name, "err", err)
log.Println("E!", "couldn't get unit NRestarts", "unit", unit.Name, "err", err)
} else {
slist.PushSample(inputName, "service_restart_total", restartsCount.Value.Value().(uint32),
map[string]string{"name": unit.Name})
Expand All @@ -229,7 +191,7 @@ func (s *Systemd) collectSockets(slist *types.SampleList, units []unit) {

acceptedConnectionCount, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Socket", "NAccepted")
if err != nil {
log.Println("msg", "couldn't get unit NAccepted", "unit", unit.Name, "err", err)
log.Println("W!", "couldn't get unit NAccepted", "unit", unit.Name, "err", err)
continue
}
tag["name"] = unit.Name
Expand All @@ -238,7 +200,7 @@ func (s *Systemd) collectSockets(slist *types.SampleList, units []unit) {

currentConnectionCount, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Socket", "NConnections")
if err != nil {
log.Println("msg", "couldn't get unit NConnections", "unit", unit.Name, "err", err)
log.Println("W!", "couldn't get unit NConnections", "unit", unit.Name, "err", err)
continue
}
slist.PushSample(inputName, "socket_current_connections",
Expand All @@ -265,7 +227,7 @@ func (s *Systemd) collectUnitStartTimeMetrics(slist *types.SampleList, units []u
} else {
timestampValue, err := s.conn.GetUnitPropertyContext(context.TODO(), unit.Name, "ActiveEnterTimestamp")
if err != nil {
log.Println("msg", "couldn't get unit StartTimeUsec", "unit", unit.Name, "err", err)
log.Println("W!", "couldn't get unit StartTimeUsec", "unit", unit.Name, "err", err)
continue
}
startTimeUsec = timestampValue.Value.Value().(uint64)
Expand All @@ -286,7 +248,7 @@ func (s *Systemd) collectUnitTasksMetrics(slist *types.SampleList, units []unit)
if strings.HasSuffix(unit.Name, ".service") {
tasksCurrentCount, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Service", "TasksCurrent")
if err != nil {
log.Println("msg", "couldn't get unit TasksCurrent", "unit", unit.Name, "err", err)
log.Println("E!", "couldn't get unit TasksCurrent", "unit", unit.Name, "err", err)
} else {
val = tasksCurrentCount.Value.Value().(uint64)
// Don't set if tasksCurrent if dbus reports MaxUint64.
Expand All @@ -296,7 +258,7 @@ func (s *Systemd) collectUnitTasksMetrics(slist *types.SampleList, units []unit)
}
tasksMaxCount, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Service", "TasksMax")
if err != nil {
log.Println("msg", "couldn't get unit TasksMax", "unit", unit.Name, "err", err)
log.Println("E!", "couldn't get unit TasksMax", "unit", unit.Name, "err", err)
} else {
val = tasksMaxCount.Value.Value().(uint64)
// Don't set if tasksMax if dbus reports MaxUint64.
Expand All @@ -318,7 +280,7 @@ func (s *Systemd) collectTimers(slist *types.SampleList, units []unit) {

lastTriggerValue, err := s.conn.GetUnitTypePropertyContext(context.TODO(), unit.Name, "Timer", "LastTriggerUSec")
if err != nil {
log.Println("msg", "couldn't get unit LastTriggerUSec", "unit", unit.Name, "err", err)
log.Println("W!", "couldn't get unit LastTriggerUSec", "unit", unit.Name, "err", err)
continue
}

Expand Down
15 changes: 15 additions & 0 deletions inputs/systemd/systemd_nonlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !linux
// +build !linux

package systemd

import (
"flashcat.cloud/categraf/types"
)

func (s *Systemd) Init() error {
return nil
}

func (s *Systemd) Gather(slist *types.SampleList) {
}

0 comments on commit 8a3559a

Please sign in to comment.