-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
51 lines (48 loc) · 1.41 KB
/
parser.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
package HAProxyGoStat
import (
"github.com/oleiade/reflections"
"strconv"
"strings"
"time"
)
// takes in a full list of strings and turns them into a HAProxyStatSnapshot
func ParseToSnapshot(data []string, parser func(statsLine string) HAProxyStat) *HAProxyStatSnapshot {
snapshot := GenerateNewSnapshot()
for _, line := range data {
if len(line) < 2 || strings.HasPrefix(line, "# ") {
continue
}
snapshot.Stats = append(snapshot.Stats, parser(line))
}
return snapshot
}
// Create a new parser.
// Pass in the header line (first line) of the socket output
// parser := CreateHAProxyCSVParser(headers)
// stat = parser(line)
func CreateHAProxyCSVParser(headers string) func(statsLine string) HAProxyStat {
HeaderMap := strings.Split(strings.TrimSpace(strings.TrimPrefix(headers, "# ")), ",")
for i, header := range HeaderMap {
HeaderMap[i] = HAProxyColumnNames[header]
}
return func(statsLine string) HAProxyStat {
statsLineSplit := strings.Split(strings.TrimSpace(statsLine), ",")
stat := new(HAProxyStat)
for i, header := range HeaderMap {
if strings.HasPrefix(statsLineSplit[i], "# ") {
continue
}
stat.Timestamp = time.Now()
statInt, err := strconv.Atoi(statsLineSplit[i])
if err != nil {
err = reflections.SetField(stat, header, statsLineSplit[i])
} else {
err = reflections.SetField(stat, header, statInt)
}
if err != nil {
continue
}
}
return *stat
}
}