bird_exporter/client/bird_client.go

69 lines
1.5 KiB
Go

package client
import (
"github.com/czerwonk/bird_exporter/parser"
"github.com/czerwonk/bird_exporter/protocol"
"github.com/czerwonk/bird_socket"
)
type BirdClient struct {
Options *BirdClientOptions
}
type BirdClientOptions struct {
BirdV2 bool
BirdEnabled bool
Bird6Enabled bool
BirdSocket string
Bird6Socket string
}
func (c *BirdClient) GetProtocols() ([]*protocol.Protocol, error) {
ipVersions := make([]string, 0)
if c.Options.BirdV2 {
ipVersions = append(ipVersions, "")
} else {
if c.Options.BirdEnabled {
ipVersions = append(ipVersions, "4")
}
if c.Options.Bird6Enabled {
ipVersions = append(ipVersions, "6")
}
}
return c.getProtocolsFromBird(ipVersions)
}
func (c *BirdClient) getProtocolsFromBird(ipVersions []string) ([]*protocol.Protocol, error) {
protocols := make([]*protocol.Protocol, 0)
for _, ipVersion := range ipVersions {
sock := c.socketFor(ipVersion)
s, err := c.getProtocolsFromSocket(sock, ipVersion)
if err != nil {
return nil, err
}
protocols = append(protocols, s...)
}
return protocols, nil
}
func (c *BirdClient) getProtocolsFromSocket(socketPath string, ipVersion string) ([]*protocol.Protocol, error) {
b, err := birdsocket.Query(socketPath, "show protocols all")
if err != nil {
return nil, err
}
return parser.Parse(b, ipVersion), nil
}
func (c *BirdClient) socketFor(ipVersion string) string {
if !c.Options.BirdV2 && ipVersion == "6" {
return c.Options.Bird6Socket
}
return c.Options.BirdSocket
}