-
Notifications
You must be signed in to change notification settings - Fork 34
/
bfd.go
94 lines (79 loc) · 2.97 KB
/
bfd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package collector
import (
"encoding/json"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
var (
bfdSubsystem = "bfd"
)
func init() {
registerCollector(bfdSubsystem, enabledByDefault, NewBFDCollector)
}
type bfdCollector struct {
logger log.Logger
descriptions map[string]*prometheus.Desc
}
// NewBFDCollector collects BFD metrics, implemented as per the Collector interface.
func NewBFDCollector(logger log.Logger) (Collector, error) {
return &bfdCollector{logger: logger, descriptions: getBFDDesc()}, nil
}
func getBFDDesc() map[string]*prometheus.Desc {
countLabels := []string{}
peerLabels := []string{"local", "peer"}
return map[string]*prometheus.Desc{
"bfdPeerCount": colPromDesc(bfdSubsystem, "peer_count", "Number of peers detected.", countLabels),
"bfdPeerUptime": colPromDesc(bfdSubsystem, "peer_uptime", "Uptime of bfd peer in seconds", peerLabels),
"bfdPeerState": colPromDesc(bfdSubsystem, "peer_state", "State of the bfd peer (1 = Up, 0 = Down).", peerLabels),
}
}
// Update implemented as per the Collector interface.
func (c *bfdCollector) Update(ch chan<- prometheus.Metric) error {
cmd := "show bfd peers json"
jsonBFDInterface, err := executeBFDCommand(cmd)
if err != nil {
return err
}
if err = processBFDPeers(ch, jsonBFDInterface, c.descriptions); err != nil {
return cmdOutputProcessError(cmd, string(jsonBFDInterface), err)
}
return nil
}
func processBFDPeers(ch chan<- prometheus.Metric, jsonBFDInterface []byte, bfdDesc map[string]*prometheus.Desc) error {
var bfdPeers []bfdPeer
if err := json.Unmarshal(jsonBFDInterface, &bfdPeers); err != nil {
return err
}
// metric is a count of the number of peers
newGauge(ch, bfdDesc["bfdPeerCount"], float64(len(bfdPeers)))
for _, p := range bfdPeers {
labels := []string{p.Local, p.Peer}
// get the uptime of the connection to the peer in seconds
newGauge(ch, bfdDesc["bfdPeerUptime"], float64(p.Uptime), labels...)
// state of connection to the bfd peer, up or down
var bfdState float64
if p.Status == "up" {
bfdState = 1
}
newGauge(ch, bfdDesc["bfdPeerState"], bfdState, labels...)
}
return nil
}
type bfdPeer struct {
Multihop bool `json:"multihop"`
Peer string `json:"peer"`
Local string `json:"local"`
Vrf string `json:"vrf"`
ID uint32 `json:"id"`
RemoteID uint32 `json:"remote-id"`
Status string `json:"status"`
Uptime uint64 `json:"uptime"`
Diagnostic string `json:"diagnostic"`
RemoteDiagnostic string `json:"remote-diagnostic"`
ReceiveInterval uint32 `json:"receive-interval"`
TransmitInterval uint32 `json:"transmit-interval"`
EchoInterval uint32 `json:"echo-interval"`
RemoteReceiveInterval uint32 `json:"remote-receive-interval"`
RemoteTransmitInterval uint32 `json:"remote-transmit-interval"`
RemoteEchoInterval uint32 `json:"remote-echo-interval"`
}