You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
3 years ago
|
package system
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
"os/exec"
|
||
|
"bytes"
|
||
|
"hash/fnv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// this package serves to add in wrappers for system commands to get hwinfo from the board
|
||
|
// this package does not actually use the hwinfo command, but rather gathers hardware info from a variety of commands
|
||
|
|
||
|
// command for model :
|
||
|
// lshw -C system 2>/dev/null | head -n 1
|
||
|
// command for ip && mac address :
|
||
|
// ifconfig eth0 | awk '/inet |ether / {print $2}'
|
||
|
|
||
|
// can combine and dump into file using
|
||
|
// lshw -C system 2>/dev/null | head -n 1 > hwinfo.txt && ifconfig eth0 | awk '/inet |ether / {print $2}' >> hwinfo.txt
|
||
|
// *** will just replace info in file everytime
|
||
|
|
||
|
|
||
|
type HWMonitor struct {
|
||
|
Ip string
|
||
|
Id uint32
|
||
|
Bus int
|
||
|
mu sync.Mutex
|
||
|
}
|
||
|
|
||
|
func NewHWMonitor() *HWMonitor {
|
||
|
h := &HWMonitor{}
|
||
|
err := h.Start()
|
||
|
if err != nil {
|
||
|
fmt.Println("hw setup failed " + fmt.Sprint(err))
|
||
|
}
|
||
|
return h
|
||
|
}
|
||
|
|
||
|
func (h *HWMonitor) GetIp() string {
|
||
|
h.mu.Lock()
|
||
|
defer h.mu.Unlock()
|
||
|
return h.Ip
|
||
|
}
|
||
|
|
||
|
func (h *HWMonitor) GetId() uint32 {
|
||
|
h.mu.Lock()
|
||
|
defer h.mu.Unlock()
|
||
|
return h.Id
|
||
|
}
|
||
|
|
||
|
func (h *HWMonitor) GetBus() int {
|
||
|
h.mu.Lock()
|
||
|
defer h.mu.Unlock()
|
||
|
return h.Bus
|
||
|
}
|
||
|
|
||
|
func (h *HWMonitor) Start() error {
|
||
|
// responsible for filling out struct
|
||
|
h.mu.Lock()
|
||
|
defer h.mu.Unlock() // want to ensure we get all values before the RLC reads
|
||
|
bus := map[string]int{"raspberrypi":1,"beaglebone":2} // eventually will replace this with a config file
|
||
|
|
||
|
ipcmd := "ifconfig eth0 | awk '/inet / {print $2}'"
|
||
|
maccmd := "ifconfig eth0 | awk '/ether / {print $2}'"
|
||
|
devcmd := "lshw -C system 2>/dev/null | head -n 1"
|
||
|
|
||
|
res := [3]bytes.Buffer{}
|
||
|
var stderr bytes.Buffer
|
||
|
cmds := []string{ipcmd,maccmd,devcmd}
|
||
|
for i,c := range cmds {
|
||
|
cmd := exec.Command("bash","-c",c)
|
||
|
cmd.Stdout = &res[i]
|
||
|
cmd.Stderr = &stderr
|
||
|
err := cmd.Run()
|
||
|
if err != nil {
|
||
|
fmt.Println(fmt.Sprint(err)+": "+stderr.String())
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
// formatting
|
||
|
ip := res[0].String()
|
||
|
ip = strings.Trim(ip," \n")
|
||
|
h.Ip = ip
|
||
|
|
||
|
hash := fnv.New32a()
|
||
|
hash.Write(res[1].Bytes())
|
||
|
h.Id = hash.Sum32()
|
||
|
|
||
|
b := res[2].String()
|
||
|
b = strings.Trim(b," \n")
|
||
|
h.Bus = bus[b]
|
||
|
return nil
|
||
|
}
|