Debian Codegraph @

parser/bfd.go

raw symbols SCIP
1 package parser
2
3 import (
4 "bufio"
5 "bytes"
6 "regexp"
7 "strings"
8
9 "github.com/czerwonk/bird_exporter/protocol"
10 )
11
12 var (
13 bfdSessionRegex *regexp.Regexp
14 )
15
16 func init() {
17 bfdSessionRegex = regexp.MustCompile(`^([^\s]+)\s+([^\s]+)\s+(Up|Down|Init)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|[^\s]+)\s+(\d{1,})?\s+([0-9\.]+)\s+([0-9\.]+)$`)
18 }
19
20 type bfdContext struct {
21 line string
22 sessions []*protocol.BFDSession
23 protocol string
24 }
25
26 func ParseBFDSessions(protocolName string, data []byte) []*protocol.BFDSession {
27 reader := bytes.NewReader(data)
28 scanner := bufio.NewScanner(reader)
29
30 c := &bfdContext{
31 sessions: make([]*protocol.BFDSession, 0),
32 protocol: protocolName,
33 }
34
35 for scanner.Scan() {
36 c.line = strings.TrimSpace(scanner.Text())
37 parseBFDSessionLine(c)
38 }
39
40 return c.sessions
41 }
42
43 func parseBFDSessionLine(c *bfdContext) {
44 m := bfdSessionRegex.FindStringSubmatch(c.line)
45 if m == nil {
46 return
47 }
48 var since_epoch int64
49 if m[5] != "" {
50 since_epoch = parseInt(m[5])
51 }
52
53 sess := protocol.BFDSession{
54 ProtocolName: c.protocol,
55 IP: m[1],
56 Interface: m[2],
57 Since: parseUptime(m[4]),
58 SinceEpoch: since_epoch,
59 Interval: parseFloat(m[6]),
60 Timeout: parseFloat(m[7]),
61 }
62
63 if m[3] == "Up" {
64 sess.Up = true
65 }
66
67 c.sessions = append(c.sessions, &sess)
68 }