-
Notifications
You must be signed in to change notification settings - Fork 1
/
hwinfo.go
181 lines (151 loc) · 5.06 KB
/
hwinfo.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package hwinfo
// #include "hwisenssm2.h"
import "C"
import (
"fmt"
"unsafe"
"github.com/rs/zerolog/log"
"github.com/zachstence/hwinfo-telegraf-plugin/plugins/inputs/hwinfo/internal/mutex"
"github.com/zachstence/hwinfo-telegraf-plugin/plugins/inputs/hwinfo/internal/shmem"
"github.com/zachstence/hwinfo-telegraf-plugin/plugins/inputs/hwinfo/internal/util"
)
// SharedMemory provides access to the HWiNFO shared memory
type HWiNFO struct {
data []byte
shmem C.PHWiNFO_SENSORS_SHARED_MEM2
}
// ReadSharedMem reads data from HWiNFO shared memory
// creating a copy of the data
func Read() (*HWiNFO, error) {
data, err := shmem.Read()
if err != nil {
return nil, err
}
// If first byte is empty, nothing was read
if data[0] == 0 {
log.Fatal().Msg("no data exists in HWiNFO shared memory, do you have it enabled?")
}
shmem := &HWiNFO{
data: append([]byte(nil), data...),
shmem: C.PHWiNFO_SENSORS_SHARED_MEM2(unsafe.Pointer(&data[0])),
}
// If we read less than expected, we're missing data
headerLength := C.sizeof_HWiNFO_SENSORS_SHARED_MEM2
sensorsLength := shmem.NumSensorElements() * C.sizeof_HWiNFO_SENSORS_SENSOR_ELEMENT
readingsLength := shmem.NumReadingElements() * C.sizeof_HWiNFO_SENSORS_READING_ELEMENT
expectedBytes := headerLength + sensorsLength + readingsLength
actualBytes := len(data)
if actualBytes < expectedBytes {
// TODO how to resolve this? Config options? Read header first to determine buffer size?
log.Fatal().Msg("didn't read full shared memory buffer")
}
return shmem, nil
}
func UnlockMutex() {
mutex.Unlock()
}
// Signature "HWiS" if active, 'DEAD' when inactive
func (hwinfo *HWiNFO) Signature() string {
return util.DecodeCharPtr(unsafe.Pointer(&hwinfo.shmem.dwSignature), C.sizeof_DWORD)
}
// Version v1 is latest
func (hwinfo *HWiNFO) Version() int {
return int(hwinfo.shmem.dwVersion)
}
// Revision revision of version
func (hwinfo *HWiNFO) Revision() int {
return int(hwinfo.shmem.dwRevision)
}
// PollTime last polling time
func (hwinfo *HWiNFO) PollTime() uint64 {
addr := unsafe.Pointer(uintptr(unsafe.Pointer(&hwinfo.shmem.dwRevision)) + C.sizeof_DWORD)
return uint64(*(*C.__time64_t)(addr))
}
// OffsetOfSensorSection offset of the Sensor section from beginning of HWiNFO_SENSORS_SHARED_MEM2
func (hwinfo *HWiNFO) OffsetOfSensorSection() int {
return int(hwinfo.shmem.dwOffsetOfSensorSection)
}
// SizeOfSensorElement size of each sensor element = sizeof( HWiNFO_SENSORS_SENSOR_ELEMENT )
func (hwinfo *HWiNFO) SizeOfSensorElement() int {
return int(hwinfo.shmem.dwSizeOfSensorElement)
}
// NumSensorElements number of sensor elements
func (hwinfo *HWiNFO) NumSensorElements() int {
return int(hwinfo.shmem.dwNumSensorElements)
}
// OffsetOfReadingSection offset of the Reading section from beginning of HWiNFO_SENSORS_SHARED_MEM2
func (hwinfo *HWiNFO) OffsetOfReadingSection() int {
return int(hwinfo.shmem.dwOffsetOfReadingSection)
}
// SizeOfReadingElement size of each Reading element = sizeof( HWiNFO_SENSORS_READING_ELEMENT )
func (hwinfo *HWiNFO) SizeOfReadingElement() int {
return int(hwinfo.shmem.dwSizeOfReadingElement)
}
// NumReadingElements number of Reading elements
func (hwinfo *HWiNFO) NumReadingElements() int {
return int(hwinfo.shmem.dwNumReadingElements)
}
func (hwinfo *HWiNFO) dataForSensor(pos int) ([]byte, error) {
if pos >= hwinfo.NumSensorElements() {
return nil, fmt.Errorf("dataForSensor pos out of range, %d for size %d", pos, hwinfo.NumSensorElements())
}
start := hwinfo.OffsetOfSensorSection() + (pos * hwinfo.SizeOfSensorElement())
end := start + hwinfo.SizeOfSensorElement()
return hwinfo.data[start:end], nil
}
type SensorResult struct {
Sensor Sensor
Error error
}
// IterSensors iterate over each sensor
func (hwinfo *HWiNFO) IterSensors() <-chan SensorResult {
resultCh := make(chan SensorResult)
go func() {
for i := 0; i < hwinfo.NumSensorElements(); i++ {
data, err := hwinfo.dataForSensor(i)
if err != nil {
resultCh <- SensorResult{
Error: fmt.Errorf("failed getting sensor at index %d, %v", i, err),
}
} else {
resultCh <- SensorResult{
Sensor: NewSensor(data),
}
}
}
close(resultCh)
}()
return resultCh
}
func (hwinfo *HWiNFO) dataForReading(pos int) ([]byte, error) {
if pos >= hwinfo.NumReadingElements() {
return nil, fmt.Errorf("dataForReading pos out of range, %d for size %d", pos, hwinfo.NumSensorElements())
}
start := hwinfo.OffsetOfReadingSection() + (pos * hwinfo.SizeOfReadingElement())
end := start + hwinfo.SizeOfReadingElement()
return hwinfo.data[start:end], nil
}
type ReadingResult struct {
Reading Reading
Error error
}
// IterReadings iterate over each sensor
func (hwinfo *HWiNFO) IterReadings() <-chan ReadingResult {
resultCh := make(chan ReadingResult)
go func() {
for i := 0; i < hwinfo.NumReadingElements(); i++ {
data, err := hwinfo.dataForReading(i)
if err != nil {
resultCh <- ReadingResult{
Error: fmt.Errorf("failed getting reading at index %d, %v", i, err),
}
} else {
resultCh <- ReadingResult{
Reading: NewReading(data),
}
}
}
close(resultCh)
}()
return resultCh
}