Skip to content

Commit ac5a981

Browse files
xginn8SuperQ
authored andcommitted
Adding socket stat collection for systemd socket units (#968)
Signed-off-by: xginn8 <[email protected]>
1 parent 8af84a2 commit ac5a981

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [CHANGE]
66
* [FEATURE] Collect NRestarts property for systemd service units
7+
* [FEATURE] Add socket unit stats to systemd collector #968
78
* [ENHANCEMENT]
89
* [BUGFIX]
910

collector/systemd_linux.go

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ var (
3333
)
3434

3535
type systemdCollector struct {
36-
unitDesc *prometheus.Desc
37-
systemRunningDesc *prometheus.Desc
38-
summaryDesc *prometheus.Desc
39-
nRestartsDesc *prometheus.Desc
40-
timerLastTriggerDesc *prometheus.Desc
41-
unitWhitelistPattern *regexp.Regexp
42-
unitBlacklistPattern *regexp.Regexp
36+
unitDesc *prometheus.Desc
37+
systemRunningDesc *prometheus.Desc
38+
summaryDesc *prometheus.Desc
39+
nRestartsDesc *prometheus.Desc
40+
timerLastTriggerDesc *prometheus.Desc
41+
socketAcceptedConnectionsDesc *prometheus.Desc
42+
socketCurrentConnectionsDesc *prometheus.Desc
43+
unitWhitelistPattern *regexp.Regexp
44+
unitBlacklistPattern *regexp.Regexp
4345
}
4446

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

7684
return &systemdCollector{
77-
unitDesc: unitDesc,
78-
systemRunningDesc: systemRunningDesc,
79-
summaryDesc: summaryDesc,
80-
nRestartsDesc: nRestartsDesc,
81-
timerLastTriggerDesc: timerLastTriggerDesc,
82-
unitWhitelistPattern: unitWhitelistPattern,
83-
unitBlacklistPattern: unitBlacklistPattern,
85+
unitDesc: unitDesc,
86+
systemRunningDesc: systemRunningDesc,
87+
summaryDesc: summaryDesc,
88+
nRestartsDesc: nRestartsDesc,
89+
timerLastTriggerDesc: timerLastTriggerDesc,
90+
socketAcceptedConnectionsDesc: socketAcceptedConnectionsDesc,
91+
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
92+
unitWhitelistPattern: unitWhitelistPattern,
93+
unitBlacklistPattern: unitBlacklistPattern,
8494
}, nil
8595
}
8696

@@ -96,6 +106,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
96106
units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern)
97107
c.collectUnitStatusMetrics(ch, units)
98108
c.collectTimers(ch, units)
109+
c.collectSockets(ch, units)
99110

100111
systemState, err := c.getSystemState()
101112
if err != nil {
@@ -125,6 +136,22 @@ func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric,
125136
}
126137
}
127138

139+
func (c *systemdCollector) collectSockets(ch chan<- prometheus.Metric, units []unit) error {
140+
for _, unit := range units {
141+
if !strings.HasSuffix(unit.Name, ".socket") {
142+
continue
143+
}
144+
145+
ch <- prometheus.MustNewConstMetric(
146+
c.socketAcceptedConnectionsDesc, prometheus.CounterValue,
147+
float64(unit.acceptedConnections), unit.Name)
148+
ch <- prometheus.MustNewConstMetric(
149+
c.socketCurrentConnectionsDesc, prometheus.GaugeValue,
150+
float64(unit.currentConnections), unit.Name)
151+
}
152+
return nil
153+
}
154+
128155
func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []unit) error {
129156
for _, unit := range units {
130157
if !strings.HasSuffix(unit.Name, ".timer") {
@@ -162,8 +189,10 @@ func (c *systemdCollector) newDbus() (*dbus.Conn, error) {
162189

163190
type unit struct {
164191
dbus.UnitStatus
165-
lastTriggerUsec uint64
166-
nRestarts uint32
192+
lastTriggerUsec uint64
193+
nRestarts uint32
194+
acceptedConnections uint32
195+
currentConnections uint32
167196
}
168197

169198
func (c *systemdCollector) getAllUnits() ([]unit, error) {
@@ -201,6 +230,22 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
201230
unit.nRestarts = nRestarts.Value.Value().(uint32)
202231
}
203232

233+
if strings.HasSuffix(unit.Name, ".socket") {
234+
acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted")
235+
if err != nil {
236+
return nil, fmt.Errorf("couldn't get unit '%s' NAccepted: %s", unit.Name, err)
237+
}
238+
239+
unit.acceptedConnections = acceptedConnectionCount.Value.Value().(uint32)
240+
241+
currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections")
242+
if err != nil {
243+
return nil, fmt.Errorf("couldn't get unit '%s' NConnections: %s", unit.Name, err)
244+
}
245+
unit.currentConnections = currentConnectionCount.Value.Value().(uint32)
246+
247+
}
248+
204249
result = append(result, unit)
205250
}
206251

collector/systemd_linux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func TestSystemdCollectorDoesntCrash(t *testing.T) {
103103
collector := (c).(*systemdCollector)
104104
for _, units := range fixtures {
105105
collector.collectUnitStatusMetrics(sink, units)
106+
collector.collectSockets(sink, units)
106107
}
107108
}
108109

0 commit comments

Comments
 (0)