|
| 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 | +} |
0 commit comments