-
Notifications
You must be signed in to change notification settings - Fork 51
/
dmesgparser.go
70 lines (56 loc) · 1.54 KB
/
dmesgparser.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
package dmesgparser
import (
"fmt"
"os/exec"
"strconv"
"strings"
"sync"
)
// Dmesg struct handle for the dmesg parser
type Dmesg struct {
chanSize int
lastProcessedTime float64
sync.Mutex
}
func getEntryTime(line string) float64 {
leftindex := strings.Index(line, "[")
rightIndex := strings.Index(line, "]")
val, _ := strconv.ParseFloat(line[leftindex+1:rightIndex], 64)
return val
}
// TODOD move to Dmesg -w mode later
// func (r *Dmesg) runDmesgCommandFollowMode(outputChan chan string, interval time.Duration) {
// cmdCtx,cancel := context.WithTimeout(ctx, interval)
// defer cancel()
// cmd := exec.CommandContext(, "Dmesg", "-w", "-l", "warn")
// }
// RunDmesgCommand runs the Dmesg command to capture raw Dmesg output
func (d *Dmesg) RunDmesgCommand() ([]string, error) {
output, err := exec.Command("Dmesg").CombinedOutput()
if err != nil {
return nil, fmt.Errorf("Cannot run Dmesg cmd %s", err)
}
lines := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")
outputslice := make([]string, len(lines))
elementsadded := 0
for _, line := range lines {
if !isTraceOutput(line) {
continue
}
if d.lastProcessedTime < getEntryTime(line) {
outputslice[elementsadded] = line
}
}
return outputslice[elementsadded:], nil
}
func isTraceOutput(line string) bool {
substring := string(line[strings.Index(line, "]")])
return strings.HasPrefix(strings.TrimSpace(substring), "TRACE:")
}
// New return an initialized Dmesg
func New() *Dmesg {
return &Dmesg{
chanSize: 10000,
lastProcessedTime: 0,
}
}