-
Notifications
You must be signed in to change notification settings - Fork 4
/
memdev.go
136 lines (113 loc) · 2.81 KB
/
memdev.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
//Package memdev
//A Go package that access to Memory Devices information.
package memdev
import (
"encoding/binary"
"github.com/digitalocean/go-smbios/smbios"
)
//Memory Device Structure
type Memory struct {
Bank string `json:"bank"`
Size int `json:"size"`
Unit string `json:"unit"`
Type string `json:"type"`
FormFactor string `json:"formFactor"`
Manufacturer string `json:"manufacturer"`
Serial string `json:"serial"`
AssetTag string `json:"assetTag"`
PartNumber string `json:"partNumber"`
Speed int `json:"speed"`
DataWidth int `json:"dataWidth"`
TotalWidth int `json:"totalWidth"`
}
//Slot availability information
type Slot struct {
Free int `json:"free"`
Used int `json:"used"`
}
//Info returns a slice of Memory struct with Memory Modules Information.
func Info() ([]Memory, error) {
var mems []Memory
stream, _, err := smbios.Stream()
if err != nil {
return nil, err
}
defer stream.Close()
d := smbios.NewDecoder(stream)
ss, err := d.Decode()
if err != nil {
return nil, err
}
for _, s := range ss {
if s.Header.Type != 17 {
continue
}
if len(s.Strings) > 1 {
size := int(binary.LittleEndian.Uint16(s.Formatted[8:10]))
bankLocator := s.Strings[0]
if size == 0 {
bankLocator = "empty"
continue
}
if size == 0x7fff {
size = int(binary.LittleEndian.Uint32(s.Formatted[24:28]))
}
manufacturer := s.Strings[1]
serial := s.Strings[2]
assetTag := s.Strings[3]
partNumber := s.Strings[4]
totalWidth := s.Formatted[4]
dataWidth := s.Formatted[6]
formFactorBytes := s.Formatted[10]
memTypeBytes := s.Formatted[14]
memType := memoryType(int(memTypeBytes))
formFactor := formFactorType(int(formFactorBytes))
memSpeed := int(binary.LittleEndian.Uint16(s.Formatted[17:19]))
unit := "KB"
if s.Formatted[9]&0x80 == 0 {
unit = "MB"
}
mems = append(mems, Memory{
Bank: bankLocator,
Size: size,
Unit: unit,
Type: memType,
FormFactor: formFactor,
Manufacturer: manufacturer,
Serial: serial,
AssetTag: assetTag,
PartNumber: partNumber,
Speed: memSpeed,
DataWidth: int(dataWidth),
TotalWidth: int(totalWidth),
})
}
}
return mems, nil
}
//Slots returns a Slot struct with the amount of Free and Used slots of memory.
func Slots() (Slot, error) {
var slots Slot
stream, _, err := smbios.Stream()
if err != nil {
return Slot{}, err
}
defer stream.Close()
d := smbios.NewDecoder(stream)
ss, err := d.Decode()
if err != nil {
return Slot{}, err
}
for _, s := range ss {
if s.Header.Type != 17 {
continue
}
size := int(binary.LittleEndian.Uint16(s.Formatted[8:10]))
if size == 0 {
slots.Free++
continue
}
slots.Used++
}
return slots, nil
}