aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/prometheus/procfs/sysfs/class_dmi.go
blob: 35f8d1f390dd9bd043fb95135e4474104a89eac8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2021 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
// +build linux

package sysfs

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/prometheus/procfs/internal/util"
)

const dmiClassPath = "class/dmi/id"

// DMIClass contains info from files in /sys/class/dmi/id.
type DMIClass struct {
	BiosDate        *string // /sys/class/dmi/id/bios_date
	BiosRelease     *string // /sys/class/dmi/id/bios_release
	BiosVendor      *string // /sys/class/dmi/id/bios_vendor
	BiosVersion     *string // /sys/class/dmi/id/bios_version
	BoardAssetTag   *string // /sys/class/dmi/id/board_asset_tag
	BoardName       *string // /sys/class/dmi/id/board_name
	BoardSerial     *string // /sys/class/dmi/id/board_serial
	BoardVendor     *string // /sys/class/dmi/id/board_vendor
	BoardVersion    *string // /sys/class/dmi/id/board_version
	ChassisAssetTag *string // /sys/class/dmi/id/chassis_asset_tag
	ChassisSerial   *string // /sys/class/dmi/id/chassis_serial
	ChassisType     *string // /sys/class/dmi/id/chassis_type
	ChassisVendor   *string // /sys/class/dmi/id/chassis_vendor
	ChassisVersion  *string // /sys/class/dmi/id/chassis_version
	ProductFamily   *string // /sys/class/dmi/id/product_family
	ProductName     *string // /sys/class/dmi/id/product_name
	ProductSerial   *string // /sys/class/dmi/id/product_serial
	ProductSKU      *string // /sys/class/dmi/id/product_sku
	ProductUUID     *string // /sys/class/dmi/id/product_uuid
	ProductVersion  *string // /sys/class/dmi/id/product_version
	SystemVendor    *string // /sys/class/dmi/id/sys_vendor
}

// DMIClass returns Desktop Management Interface (DMI) information read from /sys/class/dmi.
func (fs FS) DMIClass() (*DMIClass, error) {
	path := fs.sys.Path(dmiClassPath)

	files, err := os.ReadDir(path)
	if err != nil {
		return nil, fmt.Errorf("failed to read directory %q: %w", path, err)
	}

	var dmi DMIClass
	for _, f := range files {
		if !f.Type().IsRegular() {
			continue
		}

		name := f.Name()
		if name == "modalias" || name == "uevent" {
			continue
		}

		filename := filepath.Join(path, name)
		value, err := util.SysReadFile(filename)
		if err != nil {
			if os.IsPermission(err) {
				// Only root is allowed to read the serial and product_uuid files!
				continue
			}
			return nil, fmt.Errorf("failed to read file %q: %w", filename, err)
		}

		switch name {
		case "bios_date":
			dmi.BiosDate = &value
		case "bios_release":
			dmi.BiosRelease = &value
		case "bios_vendor":
			dmi.BiosVendor = &value
		case "bios_version":
			dmi.BiosVersion = &value
		case "board_asset_tag":
			dmi.BoardAssetTag = &value
		case "board_name":
			dmi.BoardName = &value
		case "board_serial":
			dmi.BoardSerial = &value
		case "board_vendor":
			dmi.BoardVendor = &value
		case "board_version":
			dmi.BoardVersion = &value
		case "chassis_asset_tag":
			dmi.ChassisAssetTag = &value
		case "chassis_serial":
			dmi.ChassisSerial = &value
		case "chassis_type":
			dmi.ChassisType = &value
		case "chassis_vendor":
			dmi.ChassisVendor = &value
		case "chassis_version":
			dmi.ChassisVersion = &value
		case "product_family":
			dmi.ProductFamily = &value
		case "product_name":
			dmi.ProductName = &value
		case "product_serial":
			dmi.ProductSerial = &value
		case "product_sku":
			dmi.ProductSKU = &value
		case "product_uuid":
			dmi.ProductUUID = &value
		case "product_version":
			dmi.ProductVersion = &value
		case "sys_vendor":
			dmi.SystemVendor = &value
		}
	}

	return &dmi, nil
}