diff options
author | Paul Gier <pgier@redhat.com> | 2019-01-29 16:54:47 -0600 |
---|---|---|
committer | Johannes 'fish' Ziemke <github@freigeist.org> | 2019-01-29 23:54:47 +0100 |
commit | 40dce45d8dfd5f89abc00eb94a4a252d755e0552 (patch) | |
tree | 8869f6425b1609788a60e17a9c6d59669216e2d1 | |
parent | 6a3b92ce57430ce801ffa149b1fa06acf80513f1 (diff) | |
download | prometheus_node_collector-40dce45d8dfd5f89abc00eb94a4a252d755e0552.tar.bz2 prometheus_node_collector-40dce45d8dfd5f89abc00eb94a4a252d755e0552.tar.xz prometheus_node_collector-40dce45d8dfd5f89abc00eb94a4a252d755e0552.zip |
collector/systemd: add new label "type" for systemd_unit_state (#1229)
Adds a new label called "type" systemd_unit_state which contains the
Type field from the unit file. This applies only to the .service and
.mount unit types. The other unit types do not include the optional
type field.
Fixes #1210
Signed-off-by: Paul Gier <pgier@redhat.com>
-rw-r--r-- | collector/systemd_linux.go | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index ac1d018..b34c938 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go | |||
@@ -17,14 +17,14 @@ package collector | |||
17 | 17 | ||
18 | import ( | 18 | import ( |
19 | "fmt" | 19 | "fmt" |
20 | "math" | ||
20 | "regexp" | 21 | "regexp" |
21 | "strings" | 22 | "strings" |
22 | 23 | ||
23 | "github.com/coreos/go-systemd/dbus" | 24 | "github.com/coreos/go-systemd/dbus" |
24 | "github.com/prometheus/client_golang/prometheus" | 25 | "github.com/prometheus/client_golang/prometheus" |
25 | "github.com/prometheus/common/log" | 26 | "github.com/prometheus/common/log" |
26 | "gopkg.in/alecthomas/kingpin.v2" | 27 | kingpin "gopkg.in/alecthomas/kingpin.v2" |
27 | "math" | ||
28 | ) | 28 | ) |
29 | 29 | ||
30 | var ( | 30 | var ( |
@@ -61,7 +61,7 @@ func NewSystemdCollector() (Collector, error) { | |||
61 | 61 | ||
62 | unitDesc := prometheus.NewDesc( | 62 | unitDesc := prometheus.NewDesc( |
63 | prometheus.BuildFQName(namespace, subsystem, "unit_state"), | 63 | prometheus.BuildFQName(namespace, subsystem, "unit_state"), |
64 | "Systemd unit", []string{"name", "state"}, nil, | 64 | "Systemd unit", []string{"name", "state", "type"}, nil, |
65 | ) | 65 | ) |
66 | unitStartTimeDesc := prometheus.NewDesc( | 66 | unitStartTimeDesc := prometheus.NewDesc( |
67 | prometheus.BuildFQName(namespace, subsystem, "unit_start_time_seconds"), | 67 | prometheus.BuildFQName(namespace, subsystem, "unit_start_time_seconds"), |
@@ -153,7 +153,7 @@ func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric, | |||
153 | } | 153 | } |
154 | ch <- prometheus.MustNewConstMetric( | 154 | ch <- prometheus.MustNewConstMetric( |
155 | c.unitDesc, prometheus.GaugeValue, isActive, | 155 | c.unitDesc, prometheus.GaugeValue, isActive, |
156 | unit.Name, stateName) | 156 | unit.Name, stateName, unit.serviceType) |
157 | } | 157 | } |
158 | if strings.HasSuffix(unit.Name, ".service") && unit.nRestarts != nil { | 158 | if strings.HasSuffix(unit.Name, ".service") && unit.nRestarts != nil { |
159 | ch <- prometheus.MustNewConstMetric( | 159 | ch <- prometheus.MustNewConstMetric( |
@@ -252,11 +252,22 @@ type unit struct { | |||
252 | tasksCurrent *uint64 | 252 | tasksCurrent *uint64 |
253 | tasksMax *uint64 | 253 | tasksMax *uint64 |
254 | nRestarts *uint32 | 254 | nRestarts *uint32 |
255 | serviceType string | ||
255 | acceptedConnections uint32 | 256 | acceptedConnections uint32 |
256 | currentConnections uint32 | 257 | currentConnections uint32 |
257 | refusedConnections *uint32 | 258 | refusedConnections *uint32 |
258 | } | 259 | } |
259 | 260 | ||
261 | // unitType gets the suffix after the last "." in the | ||
262 | // unit name and capitalizes the first letter | ||
263 | func (u *unit) unitType() string { | ||
264 | suffixIndex := strings.LastIndex(u.Name, ".") + 1 | ||
265 | if suffixIndex < 1 || suffixIndex > len(u.Name) { | ||
266 | return "" | ||
267 | } | ||
268 | return strings.Title(u.Name[suffixIndex:]) | ||
269 | } | ||
270 | |||
260 | func (c *systemdCollector) getAllUnits() ([]unit, error) { | 271 | func (c *systemdCollector) getAllUnits() ([]unit, error) { |
261 | conn, err := c.newDbus() | 272 | conn, err := c.newDbus() |
262 | if err != nil { | 273 | if err != nil { |
@@ -276,7 +287,15 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
276 | unit := unit{ | 287 | unit := unit{ |
277 | UnitStatus: status, | 288 | UnitStatus: status, |
278 | } | 289 | } |
279 | 290 | unitType := unit.unitType() | |
291 | if unitType == "Service" || unitType == "Mount" { | ||
292 | serviceType, err := conn.GetUnitTypeProperty(unit.Name, unitType, "Type") | ||
293 | if err != nil { | ||
294 | log.Debugf("couldn't get type for unit '%s': %s", unit.Name, err) | ||
295 | } else { | ||
296 | unit.serviceType = serviceType.Value.Value().(string) | ||
297 | } | ||
298 | } | ||
280 | if strings.HasSuffix(unit.Name, ".timer") { | 299 | if strings.HasSuffix(unit.Name, ".timer") { |
281 | lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec") | 300 | lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec") |
282 | if err != nil { | 301 | if err != nil { |