Scrape cooling_device state

Signed-off-by: Alex Schmitz <alex.schmitz@gmail.com>
This commit is contained in:
Alex Schmitz 2019-08-11 22:52:16 -05:00 committed by Alex Schmitz
commit 664025d60c
No known key found for this signature in database
GPG key ID: DB2FA6E56B5D4228
11 changed files with 557 additions and 3 deletions

View file

@ -0,0 +1,90 @@
// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows
package sysfs
import (
"path/filepath"
"strconv"
"strings"
"github.com/prometheus/procfs/internal/util"
)
// ClassCoolingDeviceStats contains info from files in /sys/class/thermal/cooling_device[0-9]*
// for a single device.
// https://www.kernel.org/doc/Documentation/thermal/sysfs-api.txt
type ClassCoolingDeviceStats struct {
Name string // The name of the cooling device.
Type string // Type of the cooling device(processor/fan/...)
MaxState int64 // Maximum cooling state of the cooling device
CurState int64 // Current cooling state of the cooling device
}
func (fs FS) ClassCoolingDeviceStats() ([]ClassCoolingDeviceStats, error) {
cds, err := filepath.Glob(fs.sys.Path("class/thermal/cooling_device[0-9]*"))
if err != nil {
return []ClassCoolingDeviceStats{}, err
}
var coolingDeviceStats = ClassCoolingDeviceStats{}
stats := make([]ClassCoolingDeviceStats, len(cds))
for i, cd := range cds {
cdName := strings.TrimPrefix(filepath.Base(cd), "cooling_device")
coolingDeviceStats, err = parseCoolingDeviceStats(cd)
if err != nil {
return []ClassCoolingDeviceStats{}, err
}
coolingDeviceStats.Name = cdName
stats[i] = coolingDeviceStats
}
return stats, nil
}
func parseCoolingDeviceStats(cd string) (ClassCoolingDeviceStats, error) {
cdType, err := util.SysReadFile(filepath.Join(cd, "type"))
if err != nil {
return ClassCoolingDeviceStats{}, err
}
cdMaxStateString, err := util.SysReadFile(filepath.Join(cd, "max_state"))
if err != nil {
return ClassCoolingDeviceStats{}, err
}
cdMaxStateInt, err := strconv.ParseInt(cdMaxStateString, 10, 64)
if err != nil {
return ClassCoolingDeviceStats{}, err
}
// cur_state can be -1, eg intel powerclamp
// https://www.kernel.org/doc/Documentation/thermal/intel_powerclamp.txt
cdCurStateString, err := util.SysReadFile(filepath.Join(cd, "cur_state"))
if err != nil {
return ClassCoolingDeviceStats{}, err
}
cdCurStateInt, err := strconv.ParseInt(cdCurStateString, 10, 64)
if err != nil {
return ClassCoolingDeviceStats{}, err
}
return ClassCoolingDeviceStats{
Type: cdType,
MaxState: cdMaxStateInt,
CurState: cdCurStateInt,
}, nil
}

View file

@ -0,0 +1,84 @@
// Copyright 2019 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sysfs
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
const (
notAffected = "Not Affected"
vulnerable = "Vulnerable"
mitigation = "Mitigation"
)
// CPUVulnerabilities retrieves a map of vulnerability names to their mitigations.
func (fs FS) CPUVulnerabilities() ([]Vulnerability, error) {
matches, err := filepath.Glob(fs.sys.Path("devices/system/cpu/vulnerabilities/*"))
if err != nil {
return nil, err
}
vulnerabilities := make([]Vulnerability, 0, len(matches))
for _, match := range matches {
name := filepath.Base(match)
value, err := ioutil.ReadFile(match)
if err != nil {
return nil, err
}
v, err := parseVulnerability(name, string(value))
if err != nil {
return nil, err
}
vulnerabilities = append(vulnerabilities, v)
}
return vulnerabilities, nil
}
// Vulnerability represents a single vulnerability extracted from /sys/devices/system/cpu/vulnerabilities/
type Vulnerability struct {
CodeName string
State string
Mitigation string
}
func parseVulnerability(name, value string) (Vulnerability, error) {
v := Vulnerability{CodeName: name}
value = strings.TrimSpace(value)
if value == notAffected {
v.State = notAffected
return v, nil
}
if strings.HasPrefix(value, vulnerable) {
v.State = vulnerable
v.Mitigation = strings.TrimPrefix(strings.TrimPrefix(value, vulnerable), ": ")
return v, nil
}
if strings.HasPrefix(value, mitigation) {
v.State = mitigation
v.Mitigation = strings.TrimPrefix(strings.TrimPrefix(value, mitigation), ": ")
return v, nil
}
return v, fmt.Errorf("unknown vulnerability state for %s: %s", name, value)
}