Debian Codegraph @

client/bird_client.go

raw symbols SCIP
1 package client
2
3 import (
4 "fmt"
5
6 "github.com/czerwonk/bird_exporter/parser"
7 "github.com/czerwonk/bird_exporter/protocol"
8 birdsocket "github.com/czerwonk/bird_socket"
9 )
10
11 // BirdClient communicates with the bird socket to retrieve information
12 type BirdClient struct {
13 Options *BirdClientOptions
14 }
15
16 // BirdClientOptions defines options to connect to bird
17 type BirdClientOptions struct {
18 BirdV2 bool
19 BirdEnabled bool
20 Bird6Enabled bool
21 BirdSocket string
22 Bird6Socket string
23 }
24
25 // GetProtocols retrieves protocol information and statistics from bird
26 func (c *BirdClient) GetProtocols() ([]*protocol.Protocol, error) {
27 ipVersions := make([]string, 0)
28 if c.Options.BirdV2 {
29 ipVersions = append(ipVersions, "")
30 } else {
31 if c.Options.BirdEnabled {
32 ipVersions = append(ipVersions, "4")
33 }
34
35 if c.Options.Bird6Enabled {
36 ipVersions = append(ipVersions, "6")
37 }
38 }
39
40 return c.protocolsFromBird(ipVersions)
41 }
42
43 // GetOSPFAreas retrieves OSPF specific information from bird
44 func (c *BirdClient) GetOSPFAreas(protocol *protocol.Protocol) ([]*protocol.OSPFArea, error) {
45 sock := c.socketFor(protocol.IPVersion)
46 b, err := birdsocket.Query(sock, fmt.Sprintf("show ospf %s", protocol.Name))
47 if err != nil {
48 return nil, err
49 }
50
51 return parser.ParseOSPF(b), nil
52 }
53
54 // GetBFDSessions retrieves BFD specific information from bird
55 func (c *BirdClient) GetBFDSessions(protocol *protocol.Protocol) ([]*protocol.BFDSession, error) {
56 sock := c.socketFor(protocol.IPVersion)
57 b, err := birdsocket.Query(sock, fmt.Sprintf("show bfd sessions %s", protocol.Name))
58 if err != nil {
59 return nil, err
60 }
61
62 return parser.ParseBFDSessions(protocol.Name, b), nil
63 }
64
65 func (c *BirdClient) protocolsFromBird(ipVersions []string) ([]*protocol.Protocol, error) {
66 protocols := make([]*protocol.Protocol, 0)
67
68 for _, ipVersion := range ipVersions {
69 sock := c.socketFor(ipVersion)
70 s, err := c.protocolsFromSocket(sock, ipVersion)
71 if err != nil {
72 return nil, err
73 }
74
75 protocols = append(protocols, s...)
76 }
77
78 return protocols, nil
79 }
80
81 func (c *BirdClient) protocolsFromSocket(socketPath string, ipVersion string) ([]*protocol.Protocol, error) {
82 b, err := birdsocket.Query(socketPath, "show protocols all")
83 if err != nil {
84 return nil, err
85 }
86
87 return parser.ParseProtocols(b, ipVersion), nil
88 }
89
90 func (c *BirdClient) socketFor(ipVersion string) string {
91 if !c.Options.BirdV2 && ipVersion == "6" {
92 return c.Options.Bird6Socket
93 }
94
95 return c.Options.BirdSocket
96 }
97
98 // StatusFromSocket retrieves status information from bird
99 func (c *BirdClient) StatusFromSocket(socketPath string) (*parser.Status, error) {
100 b, err := birdsocket.Query(socketPath, "show status")
101 if err != nil {
102 return nil, err
103 }
104
105 return parser.ParseStatus(b), nil
106 }