-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdmidecode.go
188 lines (149 loc) · 4.42 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
// ADDED BY DROP - https://github.com/matryer/drop (v0.6)
// source: github.com/dselans/dmidecode (08eabb429b4ad1353e56dd634048b05d811ba062)
// update: drop -f github.com/dselans/dmidecode
// license: (see repo for details)
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
//DMI :
type DMI struct {
Data map[string]map[string]string
}
//NewDMI :
func NewDMI() *DMI {
dmi := &DMI{}
dmi.Data = make(map[string]map[string]string)
return dmi
}
//Run : Wrapper for FindBin, ExecCmd, ParseDmidecode
func (d *DMI) Run() error {
bin, findErr := d.FindBin("dmidecode")
if findErr != nil {
return findErr
}
cmdOutput, cmdErr := d.ExecDmidecode(bin)
if cmdErr != nil {
return cmdErr
}
if err := d.ParseDmidecode(cmdOutput); err != nil {
return err
}
return nil
}
//FindBin :
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 :
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 : Gross; maybe there is a cleaner way to get this done via multiline regex
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]
d.Data[dmiHandle] = make(map[string]string)
d.Data[dmiHandle]["DMIType"] = handleData[2]
d.Data[dmiHandle]["DMISize"] = handleData[3]
// Okay, we know 2nd line == name
d.Data[dmiHandle]["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]
}
d.Data[dmiHandle][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 {
d.Data[dmiHandle][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
}
}
}
if len(d.Data) == 0 {
return errors.New("Unable to parse 'dmidecode' output")
}
return nil
}
//GenericSearchBy : Generic map lookup method
func (d *DMI) GenericSearchBy(param, value string) (map[string]string, error) {
if len(d.Data) == 0 {
return nil, errors.New("DMI data is empty; make sure to .Run() first")
}
for _, v := range d.Data {
if v[param] == value {
return v, nil
}
}
return make(map[string]string), nil
}
//SearchByName :Search for a specific DMI record by name
func (d *DMI) SearchByName(name string) (map[string]string, error) {
return d.GenericSearchBy("DMIName", name)
}
//SearchByType :Search for a specific DMI record by its type
func (d *DMI) SearchByType(id int) (map[string]string, error) {
return d.GenericSearchBy("DMIType", strconv.Itoa(id))
}