Skip to content

Commit c31ebb4

Browse files
authored
Add cpu vulnerabilities reporting from sysfs (#2721)
* Add cpu vulnerabilities reporting from sysfs --------- Signed-off-by: Michal Wasilewski <[email protected]>
1 parent 3e3ab17 commit c31ebb4

File tree

8 files changed

+117
-3
lines changed

8 files changed

+117
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Name | Description | OS
183183
---------|-------------|----
184184
buddyinfo | Exposes statistics of memory fragments as reported by /proc/buddyinfo. | Linux
185185
cgroups | A summary of the number of active and enabled cgroups | Linux
186+
cpu\_vulnerabilities | Exposes CPU vulnerability information from sysfs. | Linux
186187
devstat | Exposes device statistics | Dragonfly, FreeBSD
187188
drbd | Exposes Distributed Replicated Block Device statistics (to version 8.4) | Linux
188189
ethtool | Exposes network interface information and network driver statistics equivalent to `ethtool`, `ethtool -S`, and `ethtool -i`. | Linux
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,13 @@ node_cpu_seconds_total{cpu="7",mode="softirq"} 0.31
402402
node_cpu_seconds_total{cpu="7",mode="steal"} 0
403403
node_cpu_seconds_total{cpu="7",mode="system"} 101.64
404404
node_cpu_seconds_total{cpu="7",mode="user"} 290.98
405+
# HELP node_cpu_vulnerabilities_info 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
406+
# TYPE node_cpu_vulnerabilities_info gauge
407+
node_cpu_vulnerabilities_info{codename="itlb_multihit",state="not affected"} 1
408+
node_cpu_vulnerabilities_info{codename="mds",state="vulnerable"} 1
409+
node_cpu_vulnerabilities_info{codename="retbleed",state="mitigation"} 1
410+
node_cpu_vulnerabilities_info{codename="spectre_v1",state="mitigation"} 1
411+
node_cpu_vulnerabilities_info{codename="spectre_v2",state="mitigation"} 1
405412
# HELP node_disk_ata_rotation_rate_rpm ATA disk rotation rate in RPMs (0 for SSDs).
406413
# TYPE node_disk_ata_rotation_rate_rpm gauge
407414
node_disk_ata_rotation_rate_rpm{device="sda"} 7200
@@ -2887,6 +2894,7 @@ node_scrape_collector_success{collector="buddyinfo"} 1
28872894
node_scrape_collector_success{collector="cgroups"} 1
28882895
node_scrape_collector_success{collector="conntrack"} 1
28892896
node_scrape_collector_success{collector="cpu"} 1
2897+
node_scrape_collector_success{collector="cpu_vulnerabilities"} 1
28902898
node_scrape_collector_success{collector="cpufreq"} 1
28912899
node_scrape_collector_success{collector="diskstats"} 1
28922900
node_scrape_collector_success{collector="dmi"} 1

collector/fixtures/e2e-output.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@ node_cpu_seconds_total{cpu="7",mode="softirq"} 0.31
424424
node_cpu_seconds_total{cpu="7",mode="steal"} 0
425425
node_cpu_seconds_total{cpu="7",mode="system"} 101.64
426426
node_cpu_seconds_total{cpu="7",mode="user"} 290.98
427+
# HELP node_cpu_vulnerabilities_info 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
428+
# TYPE node_cpu_vulnerabilities_info gauge
429+
node_cpu_vulnerabilities_info{codename="itlb_multihit",state="not affected"} 1
430+
node_cpu_vulnerabilities_info{codename="mds",state="vulnerable"} 1
431+
node_cpu_vulnerabilities_info{codename="retbleed",state="mitigation"} 1
432+
node_cpu_vulnerabilities_info{codename="spectre_v1",state="mitigation"} 1
433+
node_cpu_vulnerabilities_info{codename="spectre_v2",state="mitigation"} 1
427434
# HELP node_disk_ata_rotation_rate_rpm ATA disk rotation rate in RPMs (0 for SSDs).
428435
# TYPE node_disk_ata_rotation_rate_rpm gauge
429436
node_disk_ata_rotation_rate_rpm{device="sda"} 7200
@@ -2909,6 +2916,7 @@ node_scrape_collector_success{collector="buddyinfo"} 1
29092916
node_scrape_collector_success{collector="cgroups"} 1
29102917
node_scrape_collector_success{collector="conntrack"} 1
29112918
node_scrape_collector_success{collector="cpu"} 1
2919+
node_scrape_collector_success{collector="cpu_vulnerabilities"} 1
29122920
node_scrape_collector_success{collector="cpufreq"} 1
29132921
node_scrape_collector_success{collector="diskstats"} 1
29142922
node_scrape_collector_success{collector="dmi"} 1

collector/fixtures/sys.ttar

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,6 +3555,34 @@ Lines: 1
35553555
0-3
35563556
Mode: 664
35573557
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3558+
Directory: sys/devices/system/cpu/vulnerabilities
3559+
Mode: 755
3560+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3561+
Path: sys/devices/system/cpu/vulnerabilities/itlb_multihit
3562+
Lines: 1
3563+
Not affected
3564+
Mode: 644
3565+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3566+
Path: sys/devices/system/cpu/vulnerabilities/mds
3567+
Lines: 1
3568+
Vulnerable
3569+
Mode: 644
3570+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3571+
Path: sys/devices/system/cpu/vulnerabilities/retbleed
3572+
Lines: 1
3573+
Mitigation: untrained return thunk; SMT enabled with STIBP protection
3574+
Mode: 644
3575+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3576+
Path: sys/devices/system/cpu/vulnerabilities/spectre_v1
3577+
Lines: 1
3578+
Mitigation: usercopy/swapgs barriers and __user pointer sanitization
3579+
Mode: 644
3580+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3581+
Path: sys/devices/system/cpu/vulnerabilities/spectre_v2
3582+
Lines: 1
3583+
Mitigation: Retpolines, IBPB: conditional, STIBP: always-on, RSB filling, PBRSB-eIBRS: Not affected
3584+
Mode: 644
3585+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35583586
Directory: sys/devices/system/edac
35593587
Mode: 755
35603588
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

end-to-end-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enabled_collectors=$(cat << COLLECTORS
1212
conntrack
1313
cpu
1414
cpufreq
15+
cpu_vulnerabilities
1516
diskstats
1617
dmi
1718
drbd

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/prometheus/client_model v0.4.0
2727
github.com/prometheus/common v0.44.0
2828
github.com/prometheus/exporter-toolkit v0.10.0
29-
github.com/prometheus/procfs v0.10.1
29+
github.com/prometheus/procfs v0.11.0
3030
github.com/safchain/ethtool v0.3.0
3131
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
3232
golang.org/x/sys v0.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
8484
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
8585
github.com/prometheus/exporter-toolkit v0.10.0 h1:yOAzZTi4M22ZzVxD+fhy1URTuNRj/36uQJJ5S8IPza8=
8686
github.com/prometheus/exporter-toolkit v0.10.0/go.mod h1:+sVFzuvV5JDyw+Ih6p3zFxZNVnKQa3x5qPmDSiPu4ZY=
87-
github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg=
88-
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
87+
github.com/prometheus/procfs v0.11.0 h1:5EAgkfkMl659uZPbe9AS2N68a7Cc1TJbPEuGzFuRbyk=
88+
github.com/prometheus/procfs v0.11.0/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
8989
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
9090
github.com/safchain/ethtool v0.3.0 h1:gimQJpsI6sc1yIqP/y8GYgiXn/NjgvpM0RNoWLVVmP0=
9191
github.com/safchain/ethtool v0.3.0/go.mod h1:SA9BwrgyAqNo7M+uaL6IYbxpm5wk3L7Mm6ocLW+CJUs=

0 commit comments

Comments
 (0)