diff --git a/bird/bird.go b/bird/bird.go index 4a00200..7c2fa78 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -3,6 +3,7 @@ package bird import ( "os/exec" "strings" + "regexp" ) func Run(args string) ([]byte, error) { @@ -30,6 +31,23 @@ func Protocols() Parsed { return RunAndParse("protocols all", parseProtocols) } +func ProtocolsBgp() Parsed { + protocols := Protocols() + + bgpProto := Parsed{} + bgp_rx := regexp.MustCompile(`^(\w+)\s+BGP\s+.*`) + + for _, v := range protocols { + vs := v.(string) + if bgp_rx.MatchString(vs) { + key := bgp_rx.FindStringSubmatch(vs)[1] + bgpProto[key] = parseBgp(vs) + } + } + + return bgpProto +} + func Symbols() Parsed { return RunAndParse("symbols", parseSymbols) } diff --git a/bird/parser.go b/bird/parser.go index 9eb50ca..7465f3d 100644 --- a/bird/parser.go +++ b/bird/parser.go @@ -230,3 +230,9 @@ func parseRoutesCount(input []byte) Parsed { return res } + +func parseBgp(input string) Parsed { + res := Parsed{} + + return res +} diff --git a/birdwatcher.go b/birdwatcher.go index 566b6ca..4b8d9bd 100644 --- a/birdwatcher.go +++ b/birdwatcher.go @@ -112,9 +112,18 @@ func main() { fmt.Printf("%v\n", conf) r := httprouter.New() - r.GET("/status", Status) - r.GET("/routes/protocol/:protocol", ProtoRoutes) - r.GET("/protocols", Protocols) + r.GET("/status", Status) // done + r.GET("/protocols/bgp", Bgp) + r.GET("/symbols", Symbols) // done + r.GET("/symbols/tables", SymbolTables) //done + r.GET("/symbols/protocols", SymbolProtocols) // done + r.GET("/routes/protocol/:protocol", ProtoRoutes) //done + r.GET("/routes/table/:table", TableRoutes) //done + r.GET("/routes/count/protocol/:protocol", ProtoCount) //done + r.GET("/routes/count/table/:table", TableCount) // done + r.GET("/route/net/:net", RouteNet) // done + r.GET("/route/net/:net/table/:table", RouteNetTable) // done + r.GET("/protocols", Protocols) // done log.Fatal(http.ListenAndServe(":29184", r)) } diff --git a/protocols.go b/protocols.go index 7d6c2cf..4a669c3 100644 --- a/protocols.go +++ b/protocols.go @@ -20,3 +20,16 @@ func Protocols(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Content-Type", "application/json") w.Write(js) } + +func Bgp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + + res["protocols"] = bird.ProtocolsBgp()["protocols"] + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} diff --git a/routes.go b/routes.go index 465c2ba..5581e92 100644 --- a/routes.go +++ b/routes.go @@ -18,3 +18,67 @@ func ProtoRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Content-Type", "application/json") w.Write(js) } + +func TableRoutes(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + res["routes"] = bird.RoutesTable(ps.ByName("table"))["routes"] + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} + +func ProtoCount(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + res["count"] = bird.RoutesProtoCount(ps.ByName("protocol")) + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} + +func TableCount(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + res["count"] = bird.RoutesTable(ps.ByName("table")) + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} + +func RouteNet(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + res["routes"] = bird.RoutesLookupTable(ps.ByName("net"), "master") + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} + + +func RouteNetTable(w http.ResponseWriter, + r *http.Request, + ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + res["routes"] = bird.RoutesLookupTable(ps.ByName("net"), + ps.ByName("table")) + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} diff --git a/symbols.go b/symbols.go new file mode 100644 index 0000000..ee7f330 --- /dev/null +++ b/symbols.go @@ -0,0 +1,51 @@ +package main + +import ( + "encoding/json" + "github.com/julienschmidt/httprouter" + "github.com/mchackorg/birdwatcher/bird" + "net/http" +) + +func Symbols(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + + res["symbols"] = bird.Symbols() + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} + +func SymbolTables(w http.ResponseWriter, + r *http.Request, + ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + + res["symbols"] = bird.Symbols()["routing table"] + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +} + +func SymbolProtocols(w http.ResponseWriter, + r *http.Request, + ps httprouter.Params) { + res := make(map[string]interface{}) + + res["api"] = GetApiInfo() + + res["symbols"] = bird.Symbols()["protocol"] + + js, _ := json.Marshal(res) + + w.Header().Set("Content-Type", "application/json") + w.Write(js) +}