...

Source file src/github.com/prometheus/procfs/sysfs/class_dmi.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2021 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
    15  // +build linux
    16  
    17  package sysfs
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/prometheus/procfs/internal/util"
    25  )
    26  
    27  const dmiClassPath = "class/dmi/id"
    28  
    29  // DMIClass contains info from files in /sys/class/dmi/id.
    30  type DMIClass struct {
    31  	BiosDate        *string // /sys/class/dmi/id/bios_date
    32  	BiosRelease     *string // /sys/class/dmi/id/bios_release
    33  	BiosVendor      *string // /sys/class/dmi/id/bios_vendor
    34  	BiosVersion     *string // /sys/class/dmi/id/bios_version
    35  	BoardAssetTag   *string // /sys/class/dmi/id/board_asset_tag
    36  	BoardName       *string // /sys/class/dmi/id/board_name
    37  	BoardSerial     *string // /sys/class/dmi/id/board_serial
    38  	BoardVendor     *string // /sys/class/dmi/id/board_vendor
    39  	BoardVersion    *string // /sys/class/dmi/id/board_version
    40  	ChassisAssetTag *string // /sys/class/dmi/id/chassis_asset_tag
    41  	ChassisSerial   *string // /sys/class/dmi/id/chassis_serial
    42  	ChassisType     *string // /sys/class/dmi/id/chassis_type
    43  	ChassisVendor   *string // /sys/class/dmi/id/chassis_vendor
    44  	ChassisVersion  *string // /sys/class/dmi/id/chassis_version
    45  	ProductFamily   *string // /sys/class/dmi/id/product_family
    46  	ProductName     *string // /sys/class/dmi/id/product_name
    47  	ProductSerial   *string // /sys/class/dmi/id/product_serial
    48  	ProductSKU      *string // /sys/class/dmi/id/product_sku
    49  	ProductUUID     *string // /sys/class/dmi/id/product_uuid
    50  	ProductVersion  *string // /sys/class/dmi/id/product_version
    51  	SystemVendor    *string // /sys/class/dmi/id/sys_vendor
    52  }
    53  
    54  // DMIClass returns Desktop Management Interface (DMI) information read from /sys/class/dmi.
    55  func (fs FS) DMIClass() (*DMIClass, error) {
    56  	path := fs.sys.Path(dmiClassPath)
    57  
    58  	files, err := os.ReadDir(path)
    59  	if err != nil {
    60  		return nil, fmt.Errorf("failed to read directory %q: %w", path, err)
    61  	}
    62  
    63  	var dmi DMIClass
    64  	for _, f := range files {
    65  		if !f.Type().IsRegular() {
    66  			continue
    67  		}
    68  
    69  		name := f.Name()
    70  		if name == "modalias" || name == "uevent" {
    71  			continue
    72  		}
    73  
    74  		filename := filepath.Join(path, name)
    75  		value, err := util.SysReadFile(filename)
    76  		if err != nil {
    77  			if os.IsPermission(err) {
    78  				// Only root is allowed to read the serial and product_uuid files!
    79  				continue
    80  			}
    81  			return nil, fmt.Errorf("failed to read file %q: %w", filename, err)
    82  		}
    83  
    84  		switch name {
    85  		case "bios_date":
    86  			dmi.BiosDate = &value
    87  		case "bios_release":
    88  			dmi.BiosRelease = &value
    89  		case "bios_vendor":
    90  			dmi.BiosVendor = &value
    91  		case "bios_version":
    92  			dmi.BiosVersion = &value
    93  		case "board_asset_tag":
    94  			dmi.BoardAssetTag = &value
    95  		case "board_name":
    96  			dmi.BoardName = &value
    97  		case "board_serial":
    98  			dmi.BoardSerial = &value
    99  		case "board_vendor":
   100  			dmi.BoardVendor = &value
   101  		case "board_version":
   102  			dmi.BoardVersion = &value
   103  		case "chassis_asset_tag":
   104  			dmi.ChassisAssetTag = &value
   105  		case "chassis_serial":
   106  			dmi.ChassisSerial = &value
   107  		case "chassis_type":
   108  			dmi.ChassisType = &value
   109  		case "chassis_vendor":
   110  			dmi.ChassisVendor = &value
   111  		case "chassis_version":
   112  			dmi.ChassisVersion = &value
   113  		case "product_family":
   114  			dmi.ProductFamily = &value
   115  		case "product_name":
   116  			dmi.ProductName = &value
   117  		case "product_serial":
   118  			dmi.ProductSerial = &value
   119  		case "product_sku":
   120  			dmi.ProductSKU = &value
   121  		case "product_uuid":
   122  			dmi.ProductUUID = &value
   123  		case "product_version":
   124  			dmi.ProductVersion = &value
   125  		case "sys_vendor":
   126  			dmi.SystemVendor = &value
   127  		}
   128  	}
   129  
   130  	return &dmi, nil
   131  }
   132  

View as plain text