using czerwonk/bird_socket lib for communication with bird

This commit is contained in:
Daniel Czerwonk 2017-06-30 16:57:40 +02:00
parent bf309b106f
commit 0619a4c05c
3 changed files with 10 additions and 65 deletions

View File

@ -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
}

View File

@ -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.")

6
vendor/vendor.json vendored
View File

@ -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",