diff --git a/bird/bird.go b/bird/bird.go index e6dfcde..ed33a43 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -11,65 +11,65 @@ func Run(args string) ([]byte, error) { return exec.Command("birdc", argsList...).Output() } -func RunAndParse(cmd string, parser func([]byte)([]Parsed)) []Parsed { +func RunAndParse(cmd string, parser func([]byte)(Parsed)) Parsed { out, err := Run(cmd) if err != nil { // ignore errors for now - return []Parsed{} + return Parsed{} } return parser(out) } -func Status() []Parsed { +func Status() Parsed { return RunAndParse("status", parseStatus) } -func Protocols() []Parsed { +func Protocols() Parsed { return RunAndParse("protocols all", parseProtocols) } -func Symbols() []Parsed { +func Symbols() Parsed { return RunAndParse("symbols", parseSymbols) } -func RoutesProto(protocol string) []Parsed { +func RoutesProto(protocol string) Parsed { return RunAndParse("route protocol " + protocol + " all", parseRoutes) } -func RoutesProtoCount(protocol string) []Parsed { +func RoutesProtoCount(protocol string) Parsed { return RunAndParse("route protocol " + protocol + " count", parseRoutesCount) } -func RoutesExport(protocol string) []Parsed { +func RoutesExport(protocol string) Parsed { return RunAndParse("route export " + protocol + " all", parseRoutes) } -func RoutesExportCount(protocol string) []Parsed { +func RoutesExportCount(protocol string) Parsed { return RunAndParse("route export " + protocol + " count", parseRoutesCount) } -func RoutesTable(table string) []Parsed { +func RoutesTable(table string) Parsed { return RunAndParse("route table " + table + " all", parseRoutes) } -func RoutesTableCount(table string) []Parsed { +func RoutesTableCount(table string) Parsed { return RunAndParse("route table " + table + " count", parseRoutesCount) } -func RoutesLookupTable(net string, table string) []Parsed { +func RoutesLookupTable(net string, table string) Parsed { return RunAndParse("route for " + net + " table " + table + " all", parseRoutes) } -func RoutesLookupProtocol(net string, protocol string) []Parsed { +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 index 479de08..4cd6a36 100644 --- a/bird/parser.go +++ b/bird/parser.go @@ -1,23 +1,51 @@ package bird -type Parsed string +import ( + "regexp" +) -func parseStatus(input []byte) []Parsed { - return []Parsed{} +type Parsed map[string]interface{} + +func parseStatus(input []byte) Parsed { + res := Parsed{} + line_sep := regexp.MustCompile(`((\r?\n)|(\r\n?))`) + lines := line_sep.Split(string(input), -1) + + start_line_rx := regexp.MustCompile(`^BIRD\s([0-9\.]+)\s*$`) + router_id_rx := regexp.MustCompile(`^Router\sID\sis\s([0-9\.]+)\s*$`) + current_server_rx := regexp.MustCompile(`^Current\sserver\stime\sis\s([0-9\-]+)\s([0-9\:]+)\s*$`) + last_reboot_rx := regexp.MustCompile(`^Last\sreboot\son\s([0-9\-]+)\s([0-9\:]+)\s*$`) + last_reconfig_rx := regexp.MustCompile(`^Last\sreconfiguration\son\s([0-9\-]+)\s([0-9\:]+)\s*$`) + for _, line := range lines { + if (start_line_rx.MatchString(line)) { + res["version"] = start_line_rx.FindStringSubmatch(line)[1] + } else if (router_id_rx.MatchString(line)) { + res["router_id"] = router_id_rx.FindStringSubmatch(line)[1] + } else if (current_server_rx.MatchString(line)) { + res["current_server"] = current_server_rx.FindStringSubmatch(line)[1] + } else if (last_reboot_rx.MatchString(line)) { + res["last_reboot"] = last_reboot_rx.FindStringSubmatch(line)[1] + } else if (last_reconfig_rx.MatchString(line)) { + res["last_reconfig"] = last_reconfig_rx.FindStringSubmatch(line)[1] + } else { + res["message"] = line + } + } + return res } -func parseProtocols(input []byte) []Parsed { - return []Parsed{} +func parseProtocols(input []byte) Parsed { + return Parsed{} } -func parseSymbols(input []byte) []Parsed { - return []Parsed{} +func parseSymbols(input []byte) Parsed { + return Parsed{} } -func parseRoutes(input []byte) []Parsed { - return []Parsed{} +func parseRoutes(input []byte) Parsed { + return Parsed{} } -func parseRoutesCount(input []byte) []Parsed { - return []Parsed{} +func parseRoutesCount(input []byte) Parsed { + return Parsed{} } diff --git a/status.go b/status.go index 98fbd29..241fa6a 100644 --- a/status.go +++ b/status.go @@ -12,7 +12,7 @@ func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { res["api"] = GetApiInfo() - _ = bird.Status() + res["status"] = bird.Status() js, _ := json.Marshal(res)