mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
added more flexibility to routes parsing
This commit is contained in:
parent
d2704d04f7
commit
7da71225be
2 changed files with 52 additions and 8 deletions
|
@ -278,6 +278,18 @@ func parseInt(from string) int64 {
|
|||
return val
|
||||
}
|
||||
|
||||
func parseBgpRoutes(input string) Parsed {
|
||||
routes := Parsed{}
|
||||
// Input: 1 imported, 0 filtered, 2 exported, 1 preferred
|
||||
tokens := strings.Split(input, ",")
|
||||
for _, token := range tokens {
|
||||
token = strings.TrimSpace(token)
|
||||
content := strings.Split(token, " ")
|
||||
routes[content[1]] = parseInt(content[0])
|
||||
}
|
||||
return routes
|
||||
}
|
||||
|
||||
func parseBgp(input string) Parsed {
|
||||
res := Parsed{}
|
||||
lines := getLinesFromString(input)
|
||||
|
@ -286,7 +298,10 @@ func parseBgp(input string) Parsed {
|
|||
bgp_rx := regexp.MustCompile(`^([\w\.:]+)\s+BGP\s+(\w+)\s+(\w+)\s+([0-9]{4}-[0-9]{2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}:[0-9]{2})\s*(\w+)?.*$`)
|
||||
num_val_rx := regexp.MustCompile(`^\s+([^:]+):\s+([\d]+)\s*$`)
|
||||
str_val_rx := regexp.MustCompile(`^\s+([^:]+):\s+(.+)\s*$`)
|
||||
routes_rx := regexp.MustCompile(`^\s+Routes:\s+(\d+)\s+imported,\s+(\d+)\s+filtered,\s+(\d+)\s+exported,\s+(\d+)\s+preferred\s*$`)
|
||||
|
||||
// routes_rx := regexp.MustCompile(`^\s+Routes:\s+(\d+)\s+imported,\s+(\d+)\s+filtered,\s+(\d+)\s+exported,\s+(\d+)\s+preferred\s*$`)
|
||||
routes_rx := regexp.MustCompile(`^\s+Routes:\s+(.*)`)
|
||||
|
||||
imp_updates_rx := regexp.MustCompile(`^\s+Import updates:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$`)
|
||||
imp_withdraws_rx := regexp.MustCompile(`^\s+Import withdraws:\s+(\d+)\s+(\d+)\s+\-\-\-\s+(\d+)\s+(\d+)\s*$`)
|
||||
exp_updates_rx := regexp.MustCompile(`^\s+Export updates:\s+(\d+)\s+(\d+)\s+(\d+)\s+\-\-\-\s+(\d+)\s*$`)
|
||||
|
@ -302,14 +317,8 @@ func parseBgp(input string) Parsed {
|
|||
res["state_changed"] = groups[4]
|
||||
res["connection"] = groups[5]
|
||||
} else if routes_rx.MatchString(line) {
|
||||
routes := Parsed{}
|
||||
groups := routes_rx.FindStringSubmatch(line)
|
||||
|
||||
routes["imported"] = parseInt(groups[1])
|
||||
routes["filtered"] = parseInt(groups[2])
|
||||
routes["exported"] = parseInt(groups[3])
|
||||
routes["preferred"] = parseInt(groups[4])
|
||||
|
||||
routes := parseBgpRoutes(groups[1])
|
||||
res["routes"] = routes
|
||||
} else if imp_updates_rx.MatchString(line) {
|
||||
updates := Parsed{}
|
||||
|
|
35
bird/parser_test.go
Normal file
35
bird/parser_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package bird
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseBgpRoutes(t *testing.T) {
|
||||
|
||||
inputs := []string{
|
||||
"1 imported, 0 filtered, 2 exported, 1 preferred",
|
||||
"0 imported, 2846 exported", // Bird 1.4.x
|
||||
}
|
||||
|
||||
expected := []Parsed{
|
||||
Parsed{
|
||||
"imported": int64(1),
|
||||
"filtered": int64(0),
|
||||
"exported": int64(2),
|
||||
"preferred": int64(1),
|
||||
},
|
||||
Parsed{
|
||||
"imported": int64(0),
|
||||
"exported": int64(2846),
|
||||
},
|
||||
}
|
||||
|
||||
for i, in := range inputs {
|
||||
routes := parseBgpRoutes(in)
|
||||
if !reflect.DeepEqual(routes, expected[i]) {
|
||||
t.Error("Parse bgpRoutes:", routes, "expected:", expected[i])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue