Skip to content

Commit 0a3dc56

Browse files
jpdsoblitorum
authored andcommitted
Add selinux collector (prometheus#2205)
Add selinux collector Signed-off-by: Jonathan Davies <[email protected]>
1 parent 247ee6c commit 0a3dc56

File tree

7 files changed

+92
-0
lines changed

7 files changed

+92
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ powersupplyclass | Exposes Power Supply statistics from `/sys/class/power_supply
122122
pressure | Exposes pressure stall statistics from `/proc/pressure/`. | Linux (kernel 4.20+ and/or [CONFIG\_PSI](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/accounting/psi.txt))
123123
rapl | Exposes various statistics from `/sys/class/powercap`. | Linux
124124
schedstat | Exposes task scheduler statistics from `/proc/schedstat`. | Linux
125+
selinux | Exposes SELinux statistics. | Linux
125126
sockstat | Exposes various statistics from `/proc/net/sockstat`. | Linux
126127
softnet | Exposes statistics from `/proc/net/softnet_stat`. | Linux
127128
stat | Exposes various statistics from `/proc/stat`. This includes boot time, forks and interrupts. | Linux

collector/fixtures/e2e-64k-page-output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,7 @@ node_scrape_collector_success{collector="processes"} 1
30123012
node_scrape_collector_success{collector="qdisc"} 1
30133013
node_scrape_collector_success{collector="rapl"} 1
30143014
node_scrape_collector_success{collector="schedstat"} 1
3015+
node_scrape_collector_success{collector="selinux"} 1
30153016
node_scrape_collector_success{collector="sockstat"} 1
30163017
node_scrape_collector_success{collector="softnet"} 1
30173018
node_scrape_collector_success{collector="stat"} 1
@@ -3025,6 +3026,9 @@ node_scrape_collector_success{collector="wifi"} 1
30253026
node_scrape_collector_success{collector="xfs"} 1
30263027
node_scrape_collector_success{collector="zfs"} 1
30273028
node_scrape_collector_success{collector="zoneinfo"} 1
3029+
# HELP node_selinux_enabled SELinux is enabled, 1 is true, 0 is false
3030+
# TYPE node_selinux_enabled gauge
3031+
node_selinux_enabled 0
30283032
# HELP node_sockstat_FRAG6_inuse Number of FRAG6 sockets in state inuse.
30293033
# TYPE node_sockstat_FRAG6_inuse gauge
30303034
node_sockstat_FRAG6_inuse 0

collector/fixtures/e2e-output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,7 @@ node_scrape_collector_success{collector="processes"} 1
33033303
node_scrape_collector_success{collector="qdisc"} 1
33043304
node_scrape_collector_success{collector="rapl"} 1
33053305
node_scrape_collector_success{collector="schedstat"} 1
3306+
node_scrape_collector_success{collector="selinux"} 1
33063307
node_scrape_collector_success{collector="sockstat"} 1
33073308
node_scrape_collector_success{collector="softnet"} 1
33083309
node_scrape_collector_success{collector="stat"} 1
@@ -3316,6 +3317,9 @@ node_scrape_collector_success{collector="wifi"} 1
33163317
node_scrape_collector_success{collector="xfs"} 1
33173318
node_scrape_collector_success{collector="zfs"} 1
33183319
node_scrape_collector_success{collector="zoneinfo"} 1
3320+
# HELP node_selinux_enabled SELinux is enabled, 1 is true, 0 is false
3321+
# TYPE node_selinux_enabled gauge
3322+
node_selinux_enabled 0
33193323
# HELP node_sockstat_FRAG6_inuse Number of FRAG6 sockets in state inuse.
33203324
# TYPE node_sockstat_FRAG6_inuse gauge
33213325
node_sockstat_FRAG6_inuse 0

collector/selinux_linux.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2022 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+
//go:build linux && !noselinux
15+
// +build linux,!noselinux
16+
17+
package collector
18+
19+
import (
20+
"github.com/go-kit/log"
21+
"github.com/opencontainers/selinux/go-selinux"
22+
"github.com/prometheus/client_golang/prometheus"
23+
)
24+
25+
type selinuxCollector struct {
26+
configMode *prometheus.Desc
27+
currentMode *prometheus.Desc
28+
enabled *prometheus.Desc
29+
logger log.Logger
30+
}
31+
32+
func init() {
33+
registerCollector("selinux", defaultEnabled, NewSelinuxCollector)
34+
}
35+
36+
// NewSelinuxCollector returns a new Collector exposing SELinux statistics.
37+
func NewSelinuxCollector(logger log.Logger) (Collector, error) {
38+
const subsystem = "selinux"
39+
40+
return &selinuxCollector{
41+
configMode: prometheus.NewDesc(
42+
prometheus.BuildFQName(namespace, subsystem, "config_mode"),
43+
"Configured SELinux enforcement mode",
44+
nil, nil,
45+
),
46+
currentMode: prometheus.NewDesc(
47+
prometheus.BuildFQName(namespace, subsystem, "current_mode"),
48+
"Current SELinux enforcement mode",
49+
nil, nil,
50+
),
51+
enabled: prometheus.NewDesc(
52+
prometheus.BuildFQName(namespace, subsystem, "enabled"),
53+
"SELinux is enabled, 1 is true, 0 is false",
54+
nil, nil,
55+
),
56+
logger: logger,
57+
}, nil
58+
}
59+
60+
func (c *selinuxCollector) Update(ch chan<- prometheus.Metric) error {
61+
if !selinux.GetEnabled() {
62+
ch <- prometheus.MustNewConstMetric(
63+
c.enabled, prometheus.GaugeValue, 0)
64+
65+
return nil
66+
}
67+
68+
ch <- prometheus.MustNewConstMetric(
69+
c.enabled, prometheus.GaugeValue, 1)
70+
71+
ch <- prometheus.MustNewConstMetric(
72+
c.configMode, prometheus.GaugeValue, float64(selinux.DefaultEnforceMode()))
73+
74+
ch <- prometheus.MustNewConstMetric(
75+
c.currentMode, prometheus.GaugeValue, float64(selinux.EnforceMode()))
76+
77+
return nil
78+
}

end-to-end-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enabled_collectors=$(cat << COLLECTORS
3737
qdisc
3838
rapl
3939
schedstat
40+
selinux
4041
sockstat
4142
stat
4243
thermal_zone

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/mattn/go-xmlrpc v0.0.3
1717
github.com/mdlayher/netlink v1.6.0
1818
github.com/mdlayher/wifi v0.0.0-20220320220353-954ff73a19a5
19+
github.com/opencontainers/selinux v1.10.1
1920
github.com/prometheus/client_golang v1.12.2
2021
github.com/prometheus/client_model v0.2.0
2122
github.com/prometheus/common v0.35.0

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
204204
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
205205
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
206206
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
207+
github.com/opencontainers/selinux v1.10.1 h1:09LIPVRP3uuZGQvgR+SgMSNBd1Eb3vlRbGqQpoHsF8w=
208+
github.com/opencontainers/selinux v1.10.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
207209
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
208210
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
209211
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -380,6 +382,7 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w
380382
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
381383
golang.org/x/sys v0.0.0-20190902133755-9109b7679e13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
382384
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
385+
golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
383386
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
384387
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
385388
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)