talos

Форк
0
79 строк · 1.5 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package nethelpers
6

7
import (
8
	"bytes"
9
	"io"
10
	"os"
11
	"path/filepath"
12
	"strings"
13
)
14

15
// DeviceInfo contains device hardware information that can be read from /sys/.
16
type DeviceInfo struct {
17
	BusPath string
18
	PCIID   string
19
	Driver  string
20
}
21

22
// GetDeviceInfo get additional device information by reading /sys/ directory.
23
//
24
//nolint:gocyclo
25
func GetDeviceInfo(deviceName string) (*DeviceInfo, error) {
26
	path := filepath.Join("/sys/class/net/", deviceName, "/device/")
27

28
	readFile := func(path string) (string, error) {
29
		f, err := os.Open(path)
30
		if err != nil {
31
			return "", err
32
		}
33

34
		res, err := io.ReadAll(f)
35
		if err != nil {
36
			return "", err
37
		}
38

39
		return string(bytes.TrimSpace(res)), nil
40
	}
41

42
	_, err := os.Stat(path)
43
	if err != nil {
44
		if os.IsNotExist(err) {
45
			return &DeviceInfo{}, nil
46
		}
47

48
		return nil, err
49
	}
50

51
	ueventContents, err := readFile(filepath.Join(path, "uevent"))
52
	if err != nil {
53
		return nil, err
54
	}
55

56
	if ueventContents == "" {
57
		return &DeviceInfo{}, nil
58
	}
59

60
	device := &DeviceInfo{}
61

62
	for _, line := range strings.Split(ueventContents, "\n") {
63
		key, value, found := strings.Cut(line, "=")
64
		if !found {
65
			continue
66
		}
67

68
		switch key {
69
		case "DRIVER":
70
			device.Driver = value
71
		case "PCI_ID":
72
			device.PCIID = value
73
		case "PCI_SLOT_NAME":
74
			device.BusPath = value
75
		}
76
	}
77

78
	return device, nil
79
}
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.