aboutsummaryrefslogtreecommitdiff
path: root/collector
diff options
context:
space:
mode:
authorBen Kochie <superq@gmail.com>2019-09-09 17:44:03 +0200
committerGitHub <noreply@github.com>2019-09-09 17:44:03 +0200
commit82b7b1f732142506ab5c21af8174b6c2ea30232b (patch)
treeff4f7b799566a4b518fb7b4bbf3371adc4ff673a /collector
parent664025d60c3cd0eeca3da5dad7aba8c6b0a7905f (diff)
parent93fbb93a46ae300ae0f53b6ae6fde613bc7c51de (diff)
downloadprometheus_node_collector-82b7b1f732142506ab5c21af8174b6c2ea30232b.tar.bz2
prometheus_node_collector-82b7b1f732142506ab5c21af8174b6c2ea30232b.tar.xz
prometheus_node_collector-82b7b1f732142506ab5c21af8174b6c2ea30232b.zip
Merge branch 'master' into coolingDevice
Diffstat (limited to 'collector')
-rw-r--r--collector/filesystem_linux_test.go1
-rw-r--r--collector/fixtures_bindmount/proc/mounts1
-rw-r--r--collector/paths.go6
-rw-r--r--collector/systemd_linux.go74
4 files changed, 69 insertions, 13 deletions
diff --git a/collector/filesystem_linux_test.go b/collector/filesystem_linux_test.go
index 267ad06..6271c19 100644
--- a/collector/filesystem_linux_test.go
+++ b/collector/filesystem_linux_test.go
@@ -120,6 +120,7 @@ func TestPathRootfs(t *testing.T) {
120 120
121 expected := map[string]string{ 121 expected := map[string]string{
122 // should modify these mountpoints (removes /host, see fixture proc file) 122 // should modify these mountpoints (removes /host, see fixture proc file)
123 "/": "",
123 "/media/volume1": "", 124 "/media/volume1": "",
124 "/media/volume2": "", 125 "/media/volume2": "",
125 // should not modify these mountpoints 126 // should not modify these mountpoints
diff --git a/collector/fixtures_bindmount/proc/mounts b/collector/fixtures_bindmount/proc/mounts
index 2ef6401..32f9567 100644
--- a/collector/fixtures_bindmount/proc/mounts
+++ b/collector/fixtures_bindmount/proc/mounts
@@ -1,3 +1,4 @@
1/dev/nvme1n0 /host ext4 rw,seclabel,relatime,data=ordered 0 0
1/dev/nvme1n1 /host/media/volume1 ext4 rw,seclabel,relatime,data=ordered 0 0 2/dev/nvme1n1 /host/media/volume1 ext4 rw,seclabel,relatime,data=ordered 0 0
2/dev/nvme1n2 /host/media/volume2 ext4 rw,seclabel,relatime,data=ordered 0 0 3/dev/nvme1n2 /host/media/volume2 ext4 rw,seclabel,relatime,data=ordered 0 0
3tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0 4tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
diff --git a/collector/paths.go b/collector/paths.go
index 6150600..5f5a7b4 100644
--- a/collector/paths.go
+++ b/collector/paths.go
@@ -44,5 +44,9 @@ func rootfsStripPrefix(path string) string {
44 if *rootfsPath == "/" { 44 if *rootfsPath == "/" {
45 return path 45 return path
46 } 46 }
47 return strings.TrimPrefix(path, *rootfsPath) 47 stripped := strings.TrimPrefix(path, *rootfsPath)
48 if stripped == "" {
49 return "/"
50 }
51 return stripped
48} 52}
diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go
index 4b9db10..eb1fa78 100644
--- a/collector/systemd_linux.go
+++ b/collector/systemd_linux.go
@@ -19,6 +19,7 @@ import (
19 "fmt" 19 "fmt"
20 "math" 20 "math"
21 "regexp" 21 "regexp"
22 "strconv"
22 "strings" 23 "strings"
23 "sync" 24 "sync"
24 "time" 25 "time"
@@ -29,6 +30,13 @@ import (
29 kingpin "gopkg.in/alecthomas/kingpin.v2" 30 kingpin "gopkg.in/alecthomas/kingpin.v2"
30) 31)
31 32
33const (
34 // minSystemdVersionSystemState is the minimum SystemD version for availability of
35 // the 'SystemState' manager property and the timer property 'LastTriggerUSec'
36 // https://github.com/prometheus/node_exporter/issues/291
37 minSystemdVersionSystemState = 212
38)
39
32var ( 40var (
33 unitWhitelist = kingpin.Flag("collector.systemd.unit-whitelist", "Regexp of systemd units to whitelist. Units must both match whitelist and not match blacklist to be included.").Default(".+").String() 41 unitWhitelist = kingpin.Flag("collector.systemd.unit-whitelist", "Regexp of systemd units to whitelist. Units must both match whitelist and not match blacklist to be included.").Default(".+").String()
34 unitBlacklist = kingpin.Flag("collector.systemd.unit-blacklist", "Regexp of systemd units to blacklist. Units must both match whitelist and not match blacklist to be included.").Default(".+\\.(automount|device|mount|scope|slice)").String() 42 unitBlacklist = kingpin.Flag("collector.systemd.unit-blacklist", "Regexp of systemd units to blacklist. Units must both match whitelist and not match blacklist to be included.").Default(".+\\.(automount|device|mount|scope|slice)").String()
@@ -50,6 +58,8 @@ type systemdCollector struct {
50 socketAcceptedConnectionsDesc *prometheus.Desc 58 socketAcceptedConnectionsDesc *prometheus.Desc
51 socketCurrentConnectionsDesc *prometheus.Desc 59 socketCurrentConnectionsDesc *prometheus.Desc
52 socketRefusedConnectionsDesc *prometheus.Desc 60 socketRefusedConnectionsDesc *prometheus.Desc
61 systemdVersionDesc *prometheus.Desc
62 systemdVersion int
53 unitWhitelistPattern *regexp.Regexp 63 unitWhitelistPattern *regexp.Regexp
54 unitBlacklistPattern *regexp.Regexp 64 unitBlacklistPattern *regexp.Regexp
55} 65}
@@ -103,9 +113,18 @@ func NewSystemdCollector() (Collector, error) {
103 socketRefusedConnectionsDesc := prometheus.NewDesc( 113 socketRefusedConnectionsDesc := prometheus.NewDesc(
104 prometheus.BuildFQName(namespace, subsystem, "socket_refused_connections_total"), 114 prometheus.BuildFQName(namespace, subsystem, "socket_refused_connections_total"),
105 "Total number of refused socket connections", []string{"name"}, nil) 115 "Total number of refused socket connections", []string{"name"}, nil)
116 systemdVersionDesc := prometheus.NewDesc(
117 prometheus.BuildFQName(namespace, subsystem, "version"),
118 "Detected systemd version", []string{}, nil)
106 unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist)) 119 unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist))
107 unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist)) 120 unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist))
108 121
122 systemdVersion := getSystemdVersion()
123 if systemdVersion < minSystemdVersionSystemState {
124 log.Warnf("Detected systemd version %v is lower than minimum %v", systemdVersion, minSystemdVersionSystemState)
125 log.Warn("Some systemd state and timer metrics will not be available")
126 }
127
109 return &systemdCollector{ 128 return &systemdCollector{
110 unitDesc: unitDesc, 129 unitDesc: unitDesc,
111 unitStartTimeDesc: unitStartTimeDesc, 130 unitStartTimeDesc: unitStartTimeDesc,
@@ -118,6 +137,8 @@ func NewSystemdCollector() (Collector, error) {
118 socketAcceptedConnectionsDesc: socketAcceptedConnectionsDesc, 137 socketAcceptedConnectionsDesc: socketAcceptedConnectionsDesc,
119 socketCurrentConnectionsDesc: socketCurrentConnectionsDesc, 138 socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
120 socketRefusedConnectionsDesc: socketRefusedConnectionsDesc, 139 socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
140 systemdVersionDesc: systemdVersionDesc,
141 systemdVersion: systemdVersion,
121 unitWhitelistPattern: unitWhitelistPattern, 142 unitWhitelistPattern: unitWhitelistPattern,
122 unitBlacklistPattern: unitBlacklistPattern, 143 unitBlacklistPattern: unitBlacklistPattern,
123 }, nil 144 }, nil
@@ -127,7 +148,7 @@ func NewSystemdCollector() (Collector, error) {
127// to reduce wait time for responses. 148// to reduce wait time for responses.
128func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error { 149func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
129 begin := time.Now() 150 begin := time.Now()
130 conn, err := c.newDbus() 151 conn, err := newSystemdDbusConn()
131 if err != nil { 152 if err != nil {
132 return fmt.Errorf("couldn't get dbus connection: %s", err) 153 return fmt.Errorf("couldn't get dbus connection: %s", err)
133 } 154 }
@@ -179,13 +200,15 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
179 }() 200 }()
180 } 201 }
181 202
182 wg.Add(1) 203 if c.systemdVersion >= minSystemdVersionSystemState {
183 go func() { 204 wg.Add(1)
184 defer wg.Done() 205 go func() {
185 begin = time.Now() 206 defer wg.Done()
186 c.collectTimers(conn, ch, units) 207 begin = time.Now()
187 log.Debugf("systemd collectTimers took %f", time.Since(begin).Seconds()) 208 c.collectTimers(conn, ch, units)
188 }() 209 log.Debugf("systemd collectTimers took %f", time.Since(begin).Seconds())
210 }()
211 }
189 212
190 wg.Add(1) 213 wg.Add(1)
191 go func() { 214 go func() {
@@ -195,9 +218,15 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
195 log.Debugf("systemd collectSockets took %f", time.Since(begin).Seconds()) 218 log.Debugf("systemd collectSockets took %f", time.Since(begin).Seconds())
196 }() 219 }()
197 220
198 begin = time.Now() 221 if c.systemdVersion >= minSystemdVersionSystemState {
199 err = c.collectSystemState(conn, ch) 222 begin = time.Now()
200 log.Debugf("systemd collectSystemState took %f", time.Since(begin).Seconds()) 223 err = c.collectSystemState(conn, ch)
224 log.Debugf("systemd collectSystemState took %f", time.Since(begin).Seconds())
225 }
226
227 ch <- prometheus.MustNewConstMetric(
228 c.systemdVersionDesc, prometheus.GaugeValue, float64(c.systemdVersion))
229
201 return err 230 return err
202} 231}
203 232
@@ -369,7 +398,7 @@ func (c *systemdCollector) collectSystemState(conn *dbus.Conn, ch chan<- prometh
369 return nil 398 return nil
370} 399}
371 400
372func (c *systemdCollector) newDbus() (*dbus.Conn, error) { 401func newSystemdDbusConn() (*dbus.Conn, error) {
373 if *systemdPrivate { 402 if *systemdPrivate {
374 return dbus.NewSystemdConnection() 403 return dbus.NewSystemdConnection()
375 } 404 }
@@ -424,3 +453,24 @@ func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp
424 453
425 return filtered 454 return filtered
426} 455}
456
457func getSystemdVersion() int {
458 conn, err := newSystemdDbusConn()
459 if err != nil {
460 log.Warnf("Unable to get systemd dbus connection, defaulting systemd version to 0: %s", err)
461 return 0
462 }
463 defer conn.Close()
464 version, err := conn.GetManagerProperty("Version")
465 if err != nil {
466 log.Warn("Unable to get systemd version property, defaulting to 0")
467 return 0
468 }
469 version = strings.Replace(version, "\"", "", 2)
470 v, err := strconv.Atoi(version)
471 if err != nil {
472 log.Warnf("Got invalid systemd version: %v", version)
473 return 0
474 }
475 return v
476}