-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdmidecode.go
194 lines (153 loc) · 4.45 KB
/
dmidecode.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
182
183
184
185
186
187
188
189
190
191
192
193
194
package dmidecode
import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
const (
DMIDecodeBinary = "dmidecode"
)
type Record map[string]string
type DMI struct {
Data map[string][]Record
Binary string
}
func New() *DMI {
return &DMI{
Data: make(map[string][]Record, 0),
Binary: DMIDecodeBinary,
}
}
// Run will attempt to find a a valid `dmidecode` bin, attempt to execute it and
// parse whatever data it gets.
func (d *DMI) Run() error {
bin, err := d.FindBin(d.Binary)
if err != nil {
return err
}
output, err := d.ExecDmidecode(bin)
if err != nil {
return err
}
return d.ParseDmidecode(output)
}
// FindBin will attempt to find a given binary in common bin paths.
func (d *DMI) FindBin(binary string) (string, error) {
locations := []string{"/sbin", "/usr/sbin", "/usr/local/sbin"}
for _, path := range locations {
lookup := path + "/" + binary
fileInfo, err := os.Stat(path + "/" + binary)
if err != nil {
continue
}
if !fileInfo.IsDir() {
return lookup, nil
}
}
return "", fmt.Errorf("Unable to find the '%v' binary", binary)
}
// ExecDmiDecode will attempt to execute a given binary, capture its output and
// return it (or an any errors it encounters)
func (d *DMI) ExecDmidecode(binary string) (string, error) {
cmd := exec.Command(binary)
output, err := cmd.Output()
if err != nil {
return "", err
}
return string(output), nil
}
// ParseDmiDecode will attempt to parse dmidecode output and place all matching
// content in d.Data.
func (d *DMI) ParseDmidecode(output string) error {
// Each record is separated by double newlines
splitOutput := strings.Split(output, "\n\n")
for _, record := range splitOutput {
recordElements := strings.Split(record, "\n")
// Entries with less than 3 lines are incomplete/inactive; skip them
if len(recordElements) < 3 {
continue
}
handleRegex, _ := regexp.Compile("^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$")
handleData := handleRegex.FindStringSubmatch(recordElements[0])
if len(handleData) == 0 {
continue
}
dmiHandle := handleData[1]
r := Record{}
r["DMIType"] = handleData[2]
r["DMISize"] = handleData[3]
// Okay, we know 2nd line == name
r["DMIName"] = recordElements[1]
inBlockElement := ""
inBlockList := ""
// Loop over the rest of the record, gathering values
for i := 2; i < len(recordElements); i++ {
// Check whether we are inside a \t\t block
if inBlockElement != "" {
inBlockRegex, _ := regexp.Compile("^\\t\\t(.+)$")
inBlockData := inBlockRegex.FindStringSubmatch(recordElements[i])
if len(inBlockData) > 0 {
if len(inBlockList) == 0 {
inBlockList = inBlockData[1]
} else {
inBlockList = inBlockList + "\t\t" + inBlockData[1]
}
r[inBlockElement] = inBlockList
continue
} else {
// We are out of the \t\t block; reset it again, and let
// the parsing continue
inBlockElement = ""
}
}
recordRegex, _ := regexp.Compile("\\t(.+):\\s+(.+)$")
recordData := recordRegex.FindStringSubmatch(recordElements[i])
// Is this the line containing handle identifier, type, size?
if len(recordData) > 0 {
r[recordData[1]] = recordData[2]
continue
}
// Didn't match regular entry, maybe an array of data?
recordRegex2, _ := regexp.Compile("\\t(.+):$")
recordData2 := recordRegex2.FindStringSubmatch(recordElements[i])
if len(recordData2) > 0 {
// This is an array of data - let the loop know we are inside
// an array block
inBlockElement = recordData2[1]
continue
}
}
d.Data[dmiHandle] = append(d.Data[dmiHandle], r)
}
if len(d.Data) == 0 {
return fmt.Errorf("unable to parse 'dmidecode' output")
}
return nil
}
// GenericSearchBy will search for any param w/ value in the d.Data map.
func (d *DMI) GenericSearchBy(param, value string) ([]Record, error) {
if len(d.Data) == 0 {
return nil, fmt.Errorf("DMI data is empty; make sure to .Run() first")
}
var records []Record
for _, v := range d.Data {
for _, d := range v {
if d[param] != value {
continue
}
records = append(records, d)
}
}
return records, nil
}
// SearchByName will search for a specific DMI record by name in d.Data
func (d *DMI) SearchByName(name string) ([]Record, error) {
return d.GenericSearchBy("DMIName", name)
}
// SearchByType will search for a specific DMI record by its type in d.Data
func (d *DMI) SearchByType(id int) ([]Record, error) {
return d.GenericSearchBy("DMIType", strconv.Itoa(id))
}