-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
95 lines (83 loc) · 1.67 KB
/
sensor.go
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
package sensor
import (
"bufio"
"io"
"log"
"os"
"strings"
)
var (
CENTOS_7 = "centos7"
UBUNTU = "ubuntu"
HardwareFunctionsSet = []string{"gpu", "nvram", "qat", "fpga"}
)
type Sensor interface {
IsSupported() (bool, error)
Desc() (string, error)
Detail() (string, error)
}
func NewGpuSensor() Sensor {
switch checkOSVersion() {
case CENTOS_7:
return &GpuSensorCentOS7{}
case UBUNTU:
return &GpuSensorUbuntu{}
}
return &GpuSensorFake{}
}
func NewNVRAMSensor() Sensor {
switch checkOSVersion() {
case CENTOS_7:
return &NVRAMSensorCentOS7{}
}
return &NVRAMSensorFake{}
}
func NewQATSensor() Sensor {
switch checkOSVersion() {
case CENTOS_7:
return &QATSensorCentOS7{}
}
return &QATSensorFake{}
}
func NewFPGASensor() Sensor {
switch checkOSVersion() {
case CENTOS_7, UBUNTU:
return &FPGASensorLinux{}
}
return &FPGASensorFake{}
}
func NewNICBandwidthSensor() Sensor {
switch checkOSVersion() {
case CENTOS_7:
return &NICBandwidthSensorCentOS7{}
}
return &NICBandwidthSensorFake{}
}
func checkOSVersion() string {
if _, err := os.Stat("/etc/redhat-release"); err == nil {
f, err := os.Open("/etc/redhat-release")
if err != nil {
panic(err)
}
defer f.Close()
rd := bufio.NewReader(f)
for {
line, err := rd.ReadString('\n')
if err != nil || io.EOF == err {
break
}
if strings.Contains(line, "CentOS Linux release 7") {
return CENTOS_7
}
}
} else if _, err := os.Stat("/etc/debian_version"); err == nil {
_, err := os.Open("/etc/debian_version")
if err != nil {
panic(err)
}
return UBUNTU
} else {
log.Println("Your OS is not supported. Use FAKE Driver")
}
return "fake"
}