|
| 1 | +// Copyright 2023 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 | + "fmt" |
| 18 | + |
| 19 | + "github.com/go-kit/log" |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + "github.com/prometheus/procfs/sysfs" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + cpuVulerabilitiesCollector = "cpu_vulnerabilities" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + vulnerabilityDesc = prometheus.NewDesc( |
| 30 | + prometheus.BuildFQName(namespace, cpuVulerabilitiesCollector, "info"), |
| 31 | + "Details of each CPU vulnerability reported by sysfs. The value of the series is an int encoded state of the vulnerability. The same state is stored as a string in the label", |
| 32 | + []string{"codename", "state"}, |
| 33 | + nil, |
| 34 | + ) |
| 35 | +) |
| 36 | + |
| 37 | +type cpuVulnerabilitiesCollector struct{} |
| 38 | + |
| 39 | +func init() { |
| 40 | + registerCollector(cpuVulerabilitiesCollector, defaultDisabled, NewVulnerabilitySysfsCollector) |
| 41 | +} |
| 42 | + |
| 43 | +func NewVulnerabilitySysfsCollector(logger log.Logger) (Collector, error) { |
| 44 | + return &cpuVulnerabilitiesCollector{}, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (v *cpuVulnerabilitiesCollector) Update(ch chan<- prometheus.Metric) error { |
| 48 | + fs, err := sysfs.NewFS(*sysPath) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to open sysfs: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + vulnerabilities, err := fs.CPUVulnerabilities() |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to get vulnerabilities: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + for _, vulnerability := range vulnerabilities { |
| 59 | + ch <- prometheus.MustNewConstMetric( |
| 60 | + vulnerabilityDesc, |
| 61 | + prometheus.GaugeValue, |
| 62 | + 1.0, |
| 63 | + vulnerability.CodeName, |
| 64 | + sysfs.VulnerabilityHumanEncoding[vulnerability.State], |
| 65 | + ) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
0 commit comments