diff --git a/sysfs/class_mei.go b/sysfs/class_mei.go new file mode 100644 index 00000000..f961d325 --- /dev/null +++ b/sysfs/class_mei.go @@ -0,0 +1,75 @@ +package sysfs + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/prometheus/procfs/internal/util" +) + +const meiClassPath = "class/mei/mei0" + +type MEIClass struct { + Dev *string + DevState *string + FWStatus *string + FWVersion *string + HBMVersion *string + HBMVersionDrv *string + Kind *string + Trc *string + TxQueueLimit *string +} + +// MEIClass returns Management Engine Interface (DMI) information read from /sys/class/mei/. +func (fs FS) MEIClass() (*MEIClass, error) { + path := fs.sys.Path(meiClassPath) + + files, err := os.ReadDir(path) + if err != nil { + return nil, fmt.Errorf("failed to read directory %q: %w", path, err) + } + + var mei MEIClass + for _, f := range files { + if !f.Type().IsRegular() { + continue + } + + name := f.Name() + if name == "uevent" { + continue + } + + filename := filepath.Join(path, name) + value, err := util.SysReadFile(filename) + if err != nil { + // no check for perms since all files (well apart from tx_queue_limit) are 0444 + return nil, fmt.Errorf("failed to read file %q: %w", filename, err) + } + + switch name { + case "dev": + mei.Dev = &value + case "dev_state": + mei.DevState = &value + case "fw_status": + mei.FWStatus = &value + case "fw_ver": + mei.FWVersion = &value + case "hbm_ver": + mei.HBMVersion = &value + case "hbm_ver_drv": + mei.HBMVersionDrv = &value + case "kind": + mei.Kind = &value + case "trc": + mei.Trc = &value + case "tx_queue_limit": + mei.TxQueueLimit = &value + } + } + + return &mei, nil +} diff --git a/sysfs/class_mei_test.go b/sysfs/class_mei_test.go new file mode 100644 index 00000000..f4d22ebc --- /dev/null +++ b/sysfs/class_mei_test.go @@ -0,0 +1,60 @@ +// Copyright 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. + +//go:build linux + +package sysfs + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestMEIClass(t *testing.T) { + fs, err := NewFS(sysTestFixtures) + if err != nil { + t.Fatal(err) + } + + got, err := fs.MEIClass() + if err != nil { + t.Fatal(err) + } + + dev := "244:0" + devState := "ENABLED" + fwStatus := "90000245\n00110500\n00000020\n00000000\n02F41F03\n40000000" + fwVer := "0:18.0.5.2098\n0:18.0.5.2098\n0:18.0.5.2098" + hbmVer := "2.2" + hbmVerDrv := "2.2" + kind := "mei" + trc := "00000889" + txQueueLimit := "50" + + want := &MEIClass{ + Dev: &dev, + DevState: &devState, + FWStatus: &fwStatus, + FWVersion: &fwVer, + HBMVersion: &hbmVer, + HBMVersionDrv: &hbmVerDrv, + Kind: &kind, + Trc: &trc, + TxQueueLimit: &txQueueLimit, + } + + if diff := cmp.Diff(want, got); diff != "" { + t.Fatalf("unexpected DMI class (-want +got):\n%s", diff) + } +} diff --git a/testdata/fixtures.ttar b/testdata/fixtures.ttar index 844757c9..30d0ad3e 100644 --- a/testdata/fixtures.ttar +++ b/testdata/fixtures.ttar @@ -6338,6 +6338,71 @@ Lines: 1 4: ACTIVE Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: fixtures/sys/class/mei +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: fixtures/sys/class/mei/mei0 +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/dev +Lines: 1 +244:0 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/dev_state +Lines: 1 +ENABLEDEOF +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/fw_status +Lines: 6 +90000245 +00110500 +00000020 +00000000 +02F41F03 +40000000 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/fw_ver +Lines: 3 +0:18.0.5.2098 +0:18.0.5.2098 +0:18.0.5.2098 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/hbm_ver +Lines: 1 +2.2 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/hbm_ver_drv +Lines: 1 +2.2 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/kind +Lines: 1 +mei +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/trc +Lines: 1 +00000889 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/tx_queue_limit +Lines: 1 +50 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/sys/class/mei/mei0/uevent +Lines: 3 +MAJOR=244 +MINOR=0 +DEVNAME=mei0 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: fixtures/sys/class/net Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -