diff --git a/bird/bird.go b/bird/bird.go index 2ee89b3..4a00200 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -6,7 +6,7 @@ import ( ) func Run(args string) ([]byte, error) { - args = "show" + args + args = "show " + args argsList := strings.Split(args, " ") return exec.Command("birdc", argsList...).Output() } diff --git a/bird/parser.go b/bird/parser.go index e2bda49..d682eb2 100644 --- a/bird/parser.go +++ b/bird/parser.go @@ -8,14 +8,22 @@ import ( type Parsed map[string]interface{} -func getLines(input []byte) []string { +func emptyLine(line string) bool { + return len(strings.TrimSpace(line)) == 0 +} + +func getLinesUnfiltered(input []byte) []string { line_sep := regexp.MustCompile(`((\r?\n)|(\r\n?))`) - lines := line_sep.Split(string(input), -1) + return line_sep.Split(string(input), -1) +} + +func getLines(input []byte) []string { + lines := getLinesUnfiltered(input) var filtered []string for _, line := range lines { - if len(strings.TrimSpace(line)) > 0 { + if !emptyLine(line) { filtered = append(filtered, line) } } @@ -37,6 +45,7 @@ func parseStatus(input []byte) Parsed { 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] @@ -56,7 +65,24 @@ func parseStatus(input []byte) Parsed { } func parseProtocols(input []byte) Parsed { - return Parsed{} + res := Parsed{} + protocols := []string{} + lines := getLinesUnfiltered(input) + + proto := "" + for _, line := range lines { + if emptyLine(line) { + if !emptyLine(proto) { + protocols = append(protocols, proto) + } + proto = "" + } else { + proto += (line + "\n") + } + } + + res["protocols"] = protocols + return res } func parseSymbols(input []byte) Parsed { diff --git a/birdwatcher.go b/birdwatcher.go index ed55403..b29a6e6 100644 --- a/birdwatcher.go +++ b/birdwatcher.go @@ -116,5 +116,5 @@ func main() { r.GET("/routes", Routes) r.GET("/protocols", Protocols) - log.Fatal(http.ListenAndServe(":8080", r)) + log.Fatal(http.ListenAndServe(":29184", r)) } diff --git a/protocols.go b/protocols.go index fe3574a..7d6c2cf 100644 --- a/protocols.go +++ b/protocols.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/julienschmidt/httprouter" + "github.com/mchackorg/birdwatcher/bird" ) func Protocols(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { @@ -12,13 +13,7 @@ func Protocols(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { res["api"] = GetApiInfo() - lines, err := readLines(conf.Conf.FileName) - if err != nil { - slog.Err("Couldn't find file: " + conf.Conf.FileName) - return - } - - res["protocols"] = pattern("getprotocol", lines) + res["protocols"] = bird.Protocols()["protocols"] js, _ := json.Marshal(res) diff --git a/status.go b/status.go index 241fa6a..5499edf 100644 --- a/status.go +++ b/status.go @@ -3,8 +3,8 @@ package main import ( "encoding/json" "github.com/julienschmidt/httprouter" + "github.com/mchackorg/birdwatcher/bird" "net/http" - "github.com/mchackorg/birdwatcher/bird" ) func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { @@ -12,7 +12,7 @@ func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { res["api"] = GetApiInfo() - res["status"] = bird.Status() + res["status"] = bird.Status() js, _ := json.Marshal(res)