From 0619a4c05c3b51cf8fd00fe237a602bb5ddaec9e Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk Date: Fri, 30 Jun 2017 16:57:40 +0200 Subject: [PATCH] using czerwonk/bird_socket lib for communication with bird --- bird_client.go | 67 +++------------------------------------------- main.go | 2 +- vendor/vendor.json | 6 +++++ 3 files changed, 10 insertions(+), 65 deletions(-) diff --git a/bird_client.go b/bird_client.go index de642fc..ee4fe79 100644 --- a/bird_client.go +++ b/bird_client.go @@ -1,25 +1,10 @@ package main import ( - "net" - - "regexp" - - "time" - "github.com/czerwonk/bird_exporter/protocol" - "github.com/prometheus/common/log" + "github.com/czerwonk/bird_socket" ) -const bufferSize = 4096 -const timeout = 30 - -var birdReturnCodeRegex *regexp.Regexp - -func init() { - birdReturnCodeRegex = regexp.MustCompile("\\d{4} \n$") -} - func getProtocols() ([]*protocol.Protocol, error) { protocols := make([]*protocol.Protocol, 0) @@ -43,56 +28,10 @@ func getProtocols() ([]*protocol.Protocol, error) { } func getProtocolsFromBird(socketPath string, ipVersion int) ([]*protocol.Protocol, error) { - conn, err := net.Dial("unix", socketPath) - if err != nil { - return nil, err - } - defer conn.Close() - - conn.SetReadDeadline(time.Now().Add(timeout * time.Second)) - - buf := make([]byte, bufferSize) - n, err := conn.Read(buf[:]) - if err != nil { - return nil, err - } - log.Debug(string(buf[:n])) - - _, err = conn.Write([]byte("show protocols all\n")) + b, err := bird_socket.Query(socketPath, "show protocols all") if err != nil { return nil, err } - output, err := readFromSocket(conn) - if err != nil { - return nil, err - } - - return parseOutput(output, ipVersion), nil -} - -func readFromSocket(conn net.Conn) ([]byte, error) { - b := make([]byte, 0) - buf := make([]byte, bufferSize) - - done := false - for !done { - n, err := conn.Read(buf[:]) - if err != nil { - return nil, err - } - - b = append(b, buf[:n]...) - done = endsWithBirdReturnCode(b) - } - - return b, nil -} - -func endsWithBirdReturnCode(b []byte) bool { - if len(b) < 6 { - return false - } - - return birdReturnCodeRegex.Match(b[len(b)-6:]) + return parseOutput(b, ipVersion), nil } diff --git a/main.go b/main.go index 4c28b7e..76f3723 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( "github.com/prometheus/common/log" ) -const version string = "0.8.1" +const version string = "0.8.2" var ( showVersion = flag.Bool("version", false, "Print version information.") diff --git a/vendor/vendor.json b/vendor/vendor.json index 103dc94..f50dbca 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -14,6 +14,12 @@ "revision": "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9", "revisionTime": "2016-08-04T10:47:26Z" }, + { + "checksumSHA1": "VWyPYxK+wZvIA4xcxiQftPmLcxE=", + "path": "github.com/czerwonk/bird_socket", + "revision": "c8361d5052354b111d4336dac81414792b580a7b", + "revisionTime": "2017-06-30T14:48:00Z" + }, { "checksumSHA1": "N4WKFoRf6d0PYFjgHkjuz3v2lcA=", "path": "github.com/czerwonk/testutils/assert",