Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add socket stat collection for systemd socket units #968

Merged
merged 1 commit into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [CHANGE]
* [FEATURE] Collect NRestarts property for systemd service units
* [FEATURE] Add socket unit stats to systemd collector #968
* [ENHANCEMENT]
* [BUGFIX]

Expand Down
77 changes: 61 additions & 16 deletions collector/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ var (
)

type systemdCollector struct {
unitDesc *prometheus.Desc
systemRunningDesc *prometheus.Desc
summaryDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc
timerLastTriggerDesc *prometheus.Desc
unitWhitelistPattern *regexp.Regexp
unitBlacklistPattern *regexp.Regexp
unitDesc *prometheus.Desc
systemRunningDesc *prometheus.Desc
summaryDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc
timerLastTriggerDesc *prometheus.Desc
socketAcceptedConnectionsDesc *prometheus.Desc
socketCurrentConnectionsDesc *prometheus.Desc
unitWhitelistPattern *regexp.Regexp
unitBlacklistPattern *regexp.Regexp
}

var unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"}
Expand Down Expand Up @@ -70,17 +72,25 @@ func NewSystemdCollector() (Collector, error) {
timerLastTriggerDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "timer_last_trigger_seconds"),
"Seconds since epoch of last trigger.", []string{"name"}, nil)
socketAcceptedConnectionsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "socket_accepted_connections_total"),
"Total number of accepted socket connections", []string{"name"}, nil)
socketCurrentConnectionsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "socket_current_connections"),
"Current number of socket connections", []string{"name"}, nil)
unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist))
unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist))

return &systemdCollector{
unitDesc: unitDesc,
systemRunningDesc: systemRunningDesc,
summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc,
unitWhitelistPattern: unitWhitelistPattern,
unitBlacklistPattern: unitBlacklistPattern,
unitDesc: unitDesc,
systemRunningDesc: systemRunningDesc,
summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc,
socketAcceptedConnectionsDesc: socketAcceptedConnectionsDesc,
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
unitWhitelistPattern: unitWhitelistPattern,
unitBlacklistPattern: unitBlacklistPattern,
}, nil
}

Expand All @@ -96,6 +106,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)
c.collectSockets(ch, units)

systemState, err := c.getSystemState()
if err != nil {
Expand Down Expand Up @@ -125,6 +136,22 @@ func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric,
}
}

func (c *systemdCollector) collectSockets(ch chan<- prometheus.Metric, units []unit) error {
for _, unit := range units {
if !strings.HasSuffix(unit.Name, ".socket") {
continue
}

ch <- prometheus.MustNewConstMetric(
c.socketAcceptedConnectionsDesc, prometheus.CounterValue,
float64(unit.acceptedConnections), unit.Name)
ch <- prometheus.MustNewConstMetric(
c.socketCurrentConnectionsDesc, prometheus.GaugeValue,
float64(unit.currentConnections), unit.Name)
}
return nil
}

func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []unit) error {
for _, unit := range units {
if !strings.HasSuffix(unit.Name, ".timer") {
Expand Down Expand Up @@ -162,8 +189,10 @@ func (c *systemdCollector) newDbus() (*dbus.Conn, error) {

type unit struct {
dbus.UnitStatus
lastTriggerUsec uint64
nRestarts uint32
lastTriggerUsec uint64
nRestarts uint32
acceptedConnections uint32
currentConnections uint32
}

func (c *systemdCollector) getAllUnits() ([]unit, error) {
Expand Down Expand Up @@ -201,6 +230,22 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
unit.nRestarts = nRestarts.Value.Value().(uint32)
}

if strings.HasSuffix(unit.Name, ".socket") {
acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted")
if err != nil {
return nil, fmt.Errorf("couldn't get unit '%s' NAccepted: %s", unit.Name, err)
}

unit.acceptedConnections = acceptedConnectionCount.Value.Value().(uint32)

currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections")
if err != nil {
return nil, fmt.Errorf("couldn't get unit '%s' NConnections: %s", unit.Name, err)
}
unit.currentConnections = currentConnectionCount.Value.Value().(uint32)

}

result = append(result, unit)
}

Expand Down
1 change: 1 addition & 0 deletions collector/systemd_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestSystemdCollectorDoesntCrash(t *testing.T) {
collector := (c).(*systemdCollector)
for _, units := range fixtures {
collector.collectUnitStatusMetrics(sink, units)
collector.collectSockets(sink, units)
}
}

Expand Down