-
Notifications
You must be signed in to change notification settings - Fork 8
/
ospf.go
98 lines (84 loc) · 3.09 KB
/
ospf.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
95
96
97
package collector
import (
"encoding/xml"
"fmt"
"github.com/Juniper/go-netconf/netconf"
"github.com/prometheus/client_golang/prometheus"
)
var (
ospfSubsystem = "ospf"
totalOSPFErrors = 0.0
ospfPeerLabels = []string{"neighbor_address", "neighbor_id", "local_interface"}
ospfDesc = map[string]*prometheus.Desc{
"NeighborStatus": colPromDesc(ospfSubsystem, "neighbot_status", "OSPF Neighbor Status", ospfPeerLabels),
}
)
// EnvCollector collects environment metrics, implemented as per the Collector interface.
type OSPFCollector struct{}
// NewEnvCollector returns a new EnvCollector.
func NewOSPFCollector() *OSPFCollector {
return &OSPFCollector{}
}
// Name of the collector.
func (*OSPFCollector) Name() string {
return ospfSubsystem
}
// Get metrics and send to the Prometheus.Metric channel.
func (c *OSPFCollector) Get(ch chan<- prometheus.Metric, conf Config) ([]error, float64) {
errors := []error{}
s, err := netconf.DialSSH(conf.SSHTarget, conf.SSHClientConfig)
if err != nil {
totalOSPFErrors++
errors = append(errors, fmt.Errorf("could not connect to %q: %s", conf.SSHTarget, err))
return errors, totalOSPFErrors
}
defer s.Close()
// show ospf neighbor | display xml
reply, err := s.Exec(netconf.RawMethod(`<get-ospf-neighbor-information/>`))
if err != nil {
totalOSPFErrors++
errors = append(errors, fmt.Errorf("could not execute netconf RPC call: %s", err))
return errors, totalOSPFErrors
}
if err := processOSPFNetconfReply(reply, ch); err != nil {
totalOSPFErrors++
errors = append(errors, err)
}
return errors, totalOSPFErrors
}
func processOSPFNetconfReply(reply *netconf.RPCReply, ch chan<- prometheus.Metric) error {
ospfNbrStatus := 0.0
var netconfReply ospfNeighborRPCReply
if err := xml.Unmarshal([]byte(reply.RawReply), &netconfReply); err != nil {
return fmt.Errorf("could not unmarshal netconf ospf neighbor reply: %s", err)
}
for _, neighbor := range netconfReply.OSPFNbrInformation.OSPFNeighbor {
ospfNbrStatus = 0.0
ospfPeerLabels := []string{
neighbor.NeighborAddress,
neighbor.NeighborId,
neighbor.InterfaceName,
}
if neighbor.OSPFNeighborState == "Full" {
ospfNbrStatus = 1.0
}
ch <- prometheus.MustNewConstMetric(ospfDesc["NeighborStatus"], prometheus.GaugeValue, ospfNbrStatus, ospfPeerLabels...)
}
return nil
}
type ospfNeighborRPCReply struct {
XMLName xml.Name `xml:"rpc-reply"`
Xmlns string `xml:"xmlns,attr"`
OSPFNbrInformation ospfNbrInformation `xml:"ospf-neighbor-information"`
}
type ospfNbrInformation struct {
OSPFNeighbor []ospfNeighbor `xml:"ospf-neighbor"`
}
type ospfNeighbor struct {
NeighborAddress string `xml:"neighbor-address"`
InterfaceName string `xml:"interface-name"`
OSPFNeighborState string `xml:"ospf-neighbor-state"`
NeighborId string `xml:"neighbor-id"`
NeighborPriority string `xml:"neighbor-priority"`
ActivityTimer string `xml:"activity-timer"`
}