diff --git a/bird/bird.go b/bird/bird.go new file mode 100644 index 0000000..e6dfcde --- /dev/null +++ b/bird/bird.go @@ -0,0 +1,75 @@ +package bird + +import ( + "os/exec" + "strings" +) + +func Run(args string) ([]byte, error) { + args = "show" + args + argsList := strings.Split(args, " ") + return exec.Command("birdc", argsList...).Output() +} + +func RunAndParse(cmd string, parser func([]byte)([]Parsed)) []Parsed { + out, err := Run(cmd) + + if err != nil { + // ignore errors for now + return []Parsed{} + } + + return parser(out) +} + +func Status() []Parsed { + return RunAndParse("status", parseStatus) +} + +func Protocols() []Parsed { + return RunAndParse("protocols all", parseProtocols) +} + +func Symbols() []Parsed { + return RunAndParse("symbols", parseSymbols) +} + +func RoutesProto(protocol string) []Parsed { + return RunAndParse("route protocol " + protocol + " all", + parseRoutes) +} + +func RoutesProtoCount(protocol string) []Parsed { + return RunAndParse("route protocol " + protocol + " count", + parseRoutesCount) +} + +func RoutesExport(protocol string) []Parsed { + return RunAndParse("route export " + protocol + " all", + parseRoutes) +} + +func RoutesExportCount(protocol string) []Parsed { + return RunAndParse("route export " + protocol + " count", + parseRoutesCount) +} + +func RoutesTable(table string) []Parsed { + return RunAndParse("route table " + table + " all", + parseRoutes) +} + +func RoutesTableCount(table string) []Parsed { + return RunAndParse("route table " + table + " count", + parseRoutesCount) +} + +func RoutesLookupTable(net string, table string) []Parsed { + return RunAndParse("route for " + net + " table " + table + " all", + parseRoutes) +} + +func RoutesLookupProtocol(net string, protocol string) []Parsed { + return RunAndParse("route for " + net + " protocol " + protocol + " all", + parseRoutes) +} diff --git a/bird/parser.go b/bird/parser.go new file mode 100644 index 0000000..479de08 --- /dev/null +++ b/bird/parser.go @@ -0,0 +1,23 @@ +package bird + +type Parsed string + +func parseStatus(input []byte) []Parsed { + return []Parsed{} +} + +func parseProtocols(input []byte) []Parsed { + return []Parsed{} +} + +func parseSymbols(input []byte) []Parsed { + return []Parsed{} +} + +func parseRoutes(input []byte) []Parsed { + return []Parsed{} +} + +func parseRoutesCount(input []byte) []Parsed { + return []Parsed{} +} diff --git a/status.go b/status.go index 0bb104f..98fbd29 100644 --- a/status.go +++ b/status.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/julienschmidt/httprouter" "net/http" + "github.com/mchackorg/birdwatcher/bird" ) func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { @@ -11,6 +12,8 @@ func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { res["api"] = GetApiInfo() + _ = bird.Status() + js, _ := json.Marshal(res) w.Header().Set("Content-Type", "application/json")