diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-04-20 17:28:12 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2016-04-20 17:28:12 +0200 |
commit | 91ddafdb33c72630e189b27d6076b186b0fdb9ca (patch) | |
tree | f15370eb6a9957888750338beb3205f2e160a41b /collector/logind_linux_test.go | |
parent | d98335cbf0ffe904d568604d2aa06f7316fa2aac (diff) | |
download | prometheus_node_collector-91ddafdb33c72630e189b27d6076b186b0fdb9ca.tar.bz2 prometheus_node_collector-91ddafdb33c72630e189b27d6076b186b0fdb9ca.tar.xz prometheus_node_collector-91ddafdb33c72630e189b27d6076b186b0fdb9ca.zip |
Add 'logind' exporter
logind provides a nice interface to find out about the numbers of sessions
on a system; it is used on most Linux distributions, even those which
aren't using systemd.
The exporter exposes the total number of sessions indexed by the following
attributes:
* seat
* type ("tty", "x11", ...)
* class ("user", "greeter", ...)
* remote ("true"/"false")
Diffstat (limited to 'collector/logind_linux_test.go')
-rw-r--r-- | collector/logind_linux_test.go | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/collector/logind_linux_test.go b/collector/logind_linux_test.go new file mode 100644 index 0000000..7c94158 --- /dev/null +++ b/collector/logind_linux_test.go | |||
@@ -0,0 +1,102 @@ | |||
1 | // Copyright 2016 The Prometheus Authors | ||
2 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
3 | // you may not use this file except in compliance with the License. | ||
4 | // You may obtain a copy of the License at | ||
5 | // | ||
6 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
7 | // | ||
8 | // Unless required by applicable law or agreed to in writing, software | ||
9 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
11 | // See the License for the specific language governing permissions and | ||
12 | // limitations under the License. | ||
13 | |||
14 | package collector | ||
15 | |||
16 | import ( | ||
17 | "testing" | ||
18 | |||
19 | "github.com/godbus/dbus" | ||
20 | "github.com/prometheus/client_golang/prometheus" | ||
21 | ) | ||
22 | |||
23 | type testLogindInterface struct{} | ||
24 | |||
25 | var testSeats = []string{"seat0", ""} | ||
26 | |||
27 | func (c *testLogindInterface) listSeats() ([]string, error) { | ||
28 | return testSeats, nil | ||
29 | } | ||
30 | |||
31 | func (c *testLogindInterface) listSessions() ([]logindSessionEntry, error) { | ||
32 | return []logindSessionEntry{ | ||
33 | { | ||
34 | SessionId: "1", | ||
35 | UserId: 0, | ||
36 | UserName: "", | ||
37 | SeatId: "", | ||
38 | SessionObjectPath: dbus.ObjectPath("/org/freedesktop/login1/session/1"), | ||
39 | }, | ||
40 | { | ||
41 | SessionId: "2", | ||
42 | UserId: 0, | ||
43 | UserName: "", | ||
44 | SeatId: "seat0", | ||
45 | SessionObjectPath: dbus.ObjectPath("/org/freedesktop/login1/session/2"), | ||
46 | }, | ||
47 | }, nil | ||
48 | } | ||
49 | |||
50 | func (c *testLogindInterface) getSession(session logindSessionEntry) *logindSession { | ||
51 | sessions := map[dbus.ObjectPath]*logindSession{ | ||
52 | dbus.ObjectPath("/org/freedesktop/login1/session/1"): { | ||
53 | seat: session.SeatId, | ||
54 | remote: "true", | ||
55 | sessionType: knownStringOrOther("tty", attrTypeValues), | ||
56 | class: knownStringOrOther("user", attrClassValues), | ||
57 | }, | ||
58 | dbus.ObjectPath("/org/freedesktop/login1/session/2"): { | ||
59 | seat: session.SeatId, | ||
60 | remote: "false", | ||
61 | sessionType: knownStringOrOther("x11", attrTypeValues), | ||
62 | class: knownStringOrOther("greeter", attrClassValues), | ||
63 | }, | ||
64 | } | ||
65 | |||
66 | return sessions[session.SessionObjectPath] | ||
67 | } | ||
68 | |||
69 | func TestLogindCollectorKnownStringOrOther(t *testing.T) { | ||
70 | known := []string{"foo", "bar"} | ||
71 | |||
72 | actual := knownStringOrOther("foo", known) | ||
73 | expected := "foo" | ||
74 | if actual != expected { | ||
75 | t.Errorf("knownStringOrOther failed: got %q, expected %q.", actual, expected) | ||
76 | } | ||
77 | |||
78 | actual = knownStringOrOther("baz", known) | ||
79 | expected = "other" | ||
80 | if actual != expected { | ||
81 | t.Errorf("knownStringOrOther failed: got %q, expected %q.", actual, expected) | ||
82 | } | ||
83 | |||
84 | } | ||
85 | |||
86 | func TestLogindCollectorCollectMetrics(t *testing.T) { | ||
87 | ch := make(chan prometheus.Metric) | ||
88 | go func() { | ||
89 | collectMetrics(ch, &testLogindInterface{}) | ||
90 | close(ch) | ||
91 | }() | ||
92 | |||
93 | count := 0 | ||
94 | for range ch { | ||
95 | count++ | ||
96 | } | ||
97 | |||
98 | expected := len(testSeats) * len(attrRemoteValues) * len(attrTypeValues) * len(attrClassValues) | ||
99 | if count != expected { | ||
100 | t.Errorf("collectMetrics did not generate the expected number of metrics: got %d, expected %d.", count, expected) | ||
101 | } | ||
102 | } | ||