Debian Codegraph @

parser/parser.go

raw symbols SCIP
1 package parser
2
3 import (
4 "bufio"
5 "bytes"
6 "regexp"
7 "strconv"
8 "strings"
9
10 "github.com/czerwonk/bird_exporter/protocol"
11 )
12
13 var (
14 protocolRegex *regexp.Regexp
15 descriptionRegex *regexp.Regexp
16 routeRegex *regexp.Regexp
17 uptimeRegex *regexp.Regexp
18 routeChangeRegex *regexp.Regexp
19 filterRegex *regexp.Regexp
20 channelRegex *regexp.Regexp
21 )
22
23 type context struct {
24 current *protocol.Protocol
25 line string
26 handled bool
27 protocols []*protocol.Protocol
28 ipVersion string
29 }
30
31 func init() {
32 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+(.*?))?$`)
33 descriptionRegex = regexp.MustCompile(`Description:\s+(.*)`)
34 routeRegex = regexp.MustCompile(`^\s+Routes:\s+(\d+) imported, (?:(\d+) filtered, )?(\d+) exported(?:, (\d+) preferred)?`)
35 uptimeRegex = regexp.MustCompile(`^(?:((\d+):(\d{2}):(\d{2}))|(\d+)|(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))$`)
36 routeChangeRegex = regexp.MustCompile(`(Import|Export) (updates|withdraws):\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s+(\d+|---)\s*`)
37 filterRegex = regexp.MustCompile(`(Input|Output) filter:\s+(.*)`)
38 channelRegex = regexp.MustCompile(`Channel ipv(4|6)`)
39 }
40
41 // ParseProtocols parses bird output and returns protocol.Protocol structs
42 func ParseProtocols(data []byte, ipVersion string) []*protocol.Protocol {
43 reader := bytes.NewReader(data)
44 scanner := bufio.NewScanner(reader)
45
46 c := &context{protocols: make([]*protocol.Protocol, 0), ipVersion: ipVersion}
47
48 var handlers = []func(*context){
49 handleEmptyLine,
50 parseLineForProtocol,
51 parseLineForDescription,
52 parseLineForChannel,
53 parseLineForRoutes,
54 parseLineForRouteChanges,
55 parseLineForFilterName,
56 }
57
58 for scanner.Scan() {
59 c.line = strings.TrimRight(scanner.Text(), " ")
60 c.handled = false
61
62 for _, h := range handlers {
63 if !c.handled {
64 h(c)
65 }
66 }
67 }
68
69 return c.protocols
70 }
71
72 func handleEmptyLine(c *context) {
73 if c.line != "" {
74 return
75 }
76
77 c.current = nil
78 c.handled = true
79 }
80
81 func parseLineForProtocol(c *context) {
82 match := protocolRegex.FindStringSubmatch(c.line)
83
84 if match == nil {
85 return
86 }
87
88 proto := parseProto(match[2])
89 ut := parseUptime(match[5])
90
91 c.current = protocol.NewProtocol(match[1], proto, c.ipVersion, ut)
92 c.current.Up = parseState(match[4])
93 c.current.State = match[6]
94
95 c.protocols = append(c.protocols, c.current)
96 c.handled = true
97 }
98
99 func parseLineForDescription(c *context) {
100 match := descriptionRegex.FindStringSubmatch(c.line)
101
102 if match == nil {
103 return
104 }
105
106 if len(match) <= 1 {
107 return
108 }
109
110 c.current.Description = strings.Join(match[1:], " ")
111 }
112
113 func parseProto(val string) protocol.Proto {
114 switch val {
115 case "BGP":
116 return protocol.BGP
117 case "OSPF":
118 return protocol.OSPF
119 case "Direct":
120 return protocol.Direct
121 case "Kernel":
122 return protocol.Kernel
123 case "Static":
124 return protocol.Static
125 case "Babel":
126 return protocol.Babel
127 case "RPKI":
128 return protocol.RPKI
129 case "BFD":
130 return protocol.BFD
131 }
132
133 return protocol.PROTO_UNKNOWN
134 }
135
136 func parseState(state string) int {
137 if state == "up" {
138 return 1
139 }
140
141 return 0
142 }
143
144 func parseUptime(value string) int {
145 match := uptimeRegex.FindStringSubmatch(value)
146
147 if match == nil {
148 return 0
149 }
150
151 if len(match[1]) > 0 {
152 return parseUptimeForDuration(match)
153 }
154
155 if len(match[5]) > 0 {
156 return parseUptimeForTimestamp(value)
157 }
158
159 return parseUptimeForIso(value)
160 }
161
162 func parseLineForChannel(c *context) {
163 if c.ipVersion != "" || c.current == nil {
164 return
165 }
166
167 channel := channelRegex.FindStringSubmatch(c.line)
168 if channel == nil {
169 return
170 }
171
172 if len(c.current.IPVersion) == 0 {
173 c.current.IPVersion = channel[1]
174 } else {
175 c.current = &protocol.Protocol{
176 Name: c.current.Name,
177 Proto: c.current.Proto,
178 Up: c.current.Up,
179 Uptime: c.current.Uptime,
180 IPVersion: channel[1],
181 }
182 c.protocols = append(c.protocols, c.current)
183 }
184
185 c.handled = true
186 }
187
188 func parseLineForRoutes(c *context) {
189 if c.current == nil {
190 return
191 }
192
193 match := routeRegex.FindStringSubmatch(c.line)
194 if match == nil {
195 return
196 }
197
198 c.current.Imported, _ = strconv.ParseInt(match[1], 10, 64)
199 c.current.Exported, _ = strconv.ParseInt(match[3], 10, 64)
200
201 if len(match[2]) > 0 {
202 c.current.Filtered, _ = strconv.ParseInt(match[2], 10, 64)
203 }
204
205 if len(match[4]) > 0 {
206 c.current.Preferred, _ = strconv.ParseInt(match[4], 10, 64)
207 }
208
209 c.handled = true
210 }
211
212 func parseLineForRouteChanges(c *context) {
213 if c.current == nil {
214 return
215 }
216
217 match := routeChangeRegex.FindStringSubmatch(c.line)
218 if match == nil {
219 return
220 }
221
222 x := getRouteChangeCount(match, c.current)
223 x.Received = parseRouteChangeValue(match[3])
224 x.Rejected = parseRouteChangeValue(match[4])
225 x.Filtered = parseRouteChangeValue(match[5])
226 x.Ignored = parseRouteChangeValue(match[6])
227 x.Accepted = parseRouteChangeValue(match[7])
228
229 c.handled = true
230 }
231
232 func getRouteChangeCount(values []string, p *protocol.Protocol) *protocol.RouteChangeCount {
233 if values[1] == "Import" {
234 if values[2] == "updates" {
235 return &p.ImportUpdates
236 }
237
238 return &p.ImportWithdraws
239 }
240
241 if values[2] == "updates" {
242 return &p.ExportUpdates
243 }
244
245 return &p.ExportWithdraws
246 }
247
248 func parseRouteChangeValue(value string) int64 {
249 if value == "---" {
250 return 0
251 }
252
253 return parseInt(value)
254 }
255
256 func parseLineForFilterName(c *context) {
257 if c.current == nil {
258 return
259 }
260
261 match := filterRegex.FindStringSubmatch(c.line)
262 if match == nil {
263 return
264 }
265
266 if match[1] == "Input" {
267 c.current.ImportFilter = match[2]
268 } else {
269 c.current.ExportFilter = match[2]
270 }
271
272 c.handled = true
273 }