File: parser.go

package info (click to toggle)
prometheus-bird-exporter 1.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: makefile: 12
file content (273 lines) | stat: -rw-r--r-- 5,653 bytes parent folder | download | duplicates (3)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package parser

import (
	"bufio"
	"bytes"
	"regexp"
	"strconv"
	"strings"

	"github.com/czerwonk/bird_exporter/protocol"
)

var (
	protocolRegex    *regexp.Regexp
	descriptionRegex *regexp.Regexp
	routeRegex       *regexp.Regexp
	uptimeRegex      *regexp.Regexp
	routeChangeRegex *regexp.Regexp
	filterRegex      *regexp.Regexp
	channelRegex     *regexp.Regexp
)

type context struct {
	current   *protocol.Protocol
	line      string
	handled   bool
	protocols []*protocol.Protocol
	ipVersion string
}

func init() {
	protocolRegex = regexp.MustCompile(`^(?:1002\-)?([^\s]+)\s+(MRT|BGP|BFD|OSPF|RPKI|RIP|RAdv|Pipe|Perf|Direct|Babel|Device|Kernel|Static)\s+([^\s]+)\s+([^\s]+)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|[^\s]+)(?:\s+(.*?))?$`)
	descriptionRegex = regexp.MustCompile(`Description:\s+(.*)`)
	routeRegex = regexp.MustCompile(`^\s+Routes:\s+(\d+) imported, (?:(\d+) filtered, )?(\d+) exported(?:, (\d+) preferred)?`)
	uptimeRegex = regexp.MustCompile(`^(?:((\d+):(\d{2}):(\d{2}))|(\d+)|(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))$`)
	routeChangeRegex = regexp.MustCompile(`(Import|Export) (updates|withdraws):\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s*`)
	filterRegex = regexp.MustCompile(`(Input|Output) filter:\s+(.*)`)
	channelRegex = regexp.MustCompile(`Channel ipv(4|6)`)
}

// ParseProtocols parses bird output and returns protocol.Protocol structs
func ParseProtocols(data []byte, ipVersion string) []*protocol.Protocol {
	reader := bytes.NewReader(data)
	scanner := bufio.NewScanner(reader)

	c := &context{protocols: make([]*protocol.Protocol, 0), ipVersion: ipVersion}

	var handlers = []func(*context){
		handleEmptyLine,
		parseLineForProtocol,
		parseLineForDescription,
		parseLineForChannel,
		parseLineForRoutes,
		parseLineForRouteChanges,
		parseLineForFilterName,
	}

	for scanner.Scan() {
		c.line = strings.TrimRight(scanner.Text(), " ")
		c.handled = false

		for _, h := range handlers {
			if !c.handled {
				h(c)
			}
		}
	}

	return c.protocols
}

func handleEmptyLine(c *context) {
	if c.line != "" {
		return
	}

	c.current = nil
	c.handled = true
}

func parseLineForProtocol(c *context) {
	match := protocolRegex.FindStringSubmatch(c.line)

	if match == nil {
		return
	}

	proto := parseProto(match[2])
	ut := parseUptime(match[5])

	c.current = protocol.NewProtocol(match[1], proto, c.ipVersion, ut)
	c.current.Up = parseState(match[4])
	c.current.State = match[6]

	c.protocols = append(c.protocols, c.current)
	c.handled = true
}

func parseLineForDescription(c *context) {
	match := descriptionRegex.FindStringSubmatch(c.line)

	if match == nil {
		return
	}

	if len(match) <= 1 {
		return
	}

	c.current.Description = strings.Join(match[1:], " ")
}

func parseProto(val string) protocol.Proto {
	switch val {
	case "BGP":
		return protocol.BGP
	case "OSPF":
		return protocol.OSPF
	case "Direct":
		return protocol.Direct
	case "Kernel":
		return protocol.Kernel
	case "Static":
		return protocol.Static
	case "Babel":
		return protocol.Babel
	case "RPKI":
		return protocol.RPKI
	case "BFD":
		return protocol.BFD
	}

	return protocol.PROTO_UNKNOWN
}

func parseState(state string) int {
	if state == "up" {
		return 1
	}

	return 0
}

func parseUptime(value string) int {
	match := uptimeRegex.FindStringSubmatch(value)

	if match == nil {
		return 0
	}

	if len(match[1]) > 0 {
		return parseUptimeForDuration(match)
	}

	if len(match[5]) > 0 {
		return parseUptimeForTimestamp(value)
	}

	return parseUptimeForIso(value)
}

func parseLineForChannel(c *context) {
	if c.ipVersion != "" || c.current == nil {
		return
	}

	channel := channelRegex.FindStringSubmatch(c.line)
	if channel == nil {
		return
	}

	if len(c.current.IPVersion) == 0 {
		c.current.IPVersion = channel[1]
	} else {
		c.current = &protocol.Protocol{
			Name:      c.current.Name,
			Proto:     c.current.Proto,
			Up:        c.current.Up,
			Uptime:    c.current.Uptime,
			IPVersion: channel[1],
		}
		c.protocols = append(c.protocols, c.current)
	}

	c.handled = true
}

func parseLineForRoutes(c *context) {
	if c.current == nil {
		return
	}

	match := routeRegex.FindStringSubmatch(c.line)
	if match == nil {
		return
	}

	c.current.Imported, _ = strconv.ParseInt(match[1], 10, 64)
	c.current.Exported, _ = strconv.ParseInt(match[3], 10, 64)

	if len(match[2]) > 0 {
		c.current.Filtered, _ = strconv.ParseInt(match[2], 10, 64)
	}

	if len(match[4]) > 0 {
		c.current.Preferred, _ = strconv.ParseInt(match[4], 10, 64)
	}

	c.handled = true
}

func parseLineForRouteChanges(c *context) {
	if c.current == nil {
		return
	}

	match := routeChangeRegex.FindStringSubmatch(c.line)
	if match == nil {
		return
	}

	x := getRouteChangeCount(match, c.current)
	x.Received = parseRouteChangeValue(match[3])
	x.Rejected = parseRouteChangeValue(match[4])
	x.Filtered = parseRouteChangeValue(match[5])
	x.Ignored = parseRouteChangeValue(match[6])
	x.Accepted = parseRouteChangeValue(match[7])

	c.handled = true
}

func getRouteChangeCount(values []string, p *protocol.Protocol) *protocol.RouteChangeCount {
	if values[1] == "Import" {
		if values[2] == "updates" {
			return &p.ImportUpdates
		}

		return &p.ImportWithdraws
	}

	if values[2] == "updates" {
		return &p.ExportUpdates
	}

	return &p.ExportWithdraws
}

func parseRouteChangeValue(value string) int64 {
	if value == "---" {
		return 0
	}

	return parseInt(value)
}

func parseLineForFilterName(c *context) {
	if c.current == nil {
		return
	}

	match := filterRegex.FindStringSubmatch(c.line)
	if match == nil {
		return
	}

	if match[1] == "Input" {
		c.current.ImportFilter = match[2]
	} else {
		c.current.ExportFilter = match[2]
	}

	c.handled = true
}