Skip to content

Commit fd7b9b6

Browse files
xerox81Joel Norberg
and
Joel Norberg
authored
feat: added OSPF metrics and corresponding testcase (#255)
Co-authored-by: Joel Norberg <[email protected]>
1 parent 9da7dd6 commit fd7b9b6

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ Per-VDOM:
162162
* `fortigate_bgp_neighbor_ipv6_paths`
163163
* `fortigate_bgp_neighbor_ipv6_best_paths`
164164

165+
Per-OSPF-Neighbor and VDOM:
166+
* _OSPF/Neighbors_
167+
* `fortigate_ospf_neighbor_info`
168+
165169
Per-VirtualServer and VDOM:
166170
* _Firewall/LoadBalance_
167171
* `fortigate_lb_virtual_server_info`

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
3333
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3434
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
3535
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
36+
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
3637
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3738
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
3839
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

Diff for: pkg/probe/ospf_neighbors.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package probe
2+
3+
import (
4+
"log"
5+
"strconv"
6+
7+
"github.com/bluecmd/fortigate_exporter/pkg/http"
8+
"github.com/prometheus/client_golang/prometheus"
9+
)
10+
11+
type OSPFNeighbor struct {
12+
NeighborIP string `json:"neighbor_ip"`
13+
RouterID string `json:"router_id"`
14+
State string `json:"state"`
15+
Priority int `json:"priority"`
16+
}
17+
18+
type OSPFNeighborResponse struct {
19+
Results []OSPFNeighbor `json:"results"`
20+
VDOM string `json:"vdom"`
21+
Version string `json:"version"`
22+
}
23+
24+
func probeOSPFNeighbors(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) {
25+
if meta.VersionMajor < 7 {
26+
// not supported version. Before 7.0.0 the requested endpoint doesn't exist
27+
return nil, true
28+
}
29+
var (
30+
mOSPFNeighbor = prometheus.NewDesc(
31+
"fortigate_ospf_neighbor_info",
32+
"List all discovered OSPF neighbors, return state as value (1 - Down, 2 - Attempt, 3 - Init, 4 - Two way, 5 - Exchange start, 6 - Exchange, 7 - Loading, 8 - Full)",
33+
[]string{"vdom", "state", "priority", "router_id", "neighbor_ip"}, nil,
34+
)
35+
)
36+
37+
var rs []OSPFNeighborResponse
38+
39+
if err := c.Get("api/v2/monitor/router/ospf/neighbors", "vdom=*", &rs); err != nil {
40+
log.Printf("Error: %v", err)
41+
return nil, false
42+
}
43+
44+
m := []prometheus.Metric{}
45+
46+
for _, r := range rs {
47+
for _, peer := range r.Results {
48+
m = append(m, prometheus.MustNewConstMetric(mOSPFNeighbor, prometheus.GaugeValue, ospfStateToNumber(peer.State), r.VDOM, peer.State, strconv.Itoa(peer.Priority), peer.RouterID, peer.NeighborIP))
49+
}
50+
}
51+
52+
return m, true
53+
}
54+
55+
func ospfStateToNumber(ospfState string) float64 {
56+
switch ospfState {
57+
case "Attempt":
58+
return 1
59+
case "Init":
60+
return 2
61+
case "Two way":
62+
return 3
63+
case "Exchange start":
64+
return 4
65+
case "Exchange":
66+
return 5
67+
case "Loading":
68+
return 6
69+
case "Full":
70+
return 7
71+
default: // Down
72+
return 0
73+
}
74+
}

Diff for: pkg/probe/ospf_neighbors_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package probe
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/prometheus/client_golang/prometheus"
8+
"github.com/prometheus/client_golang/prometheus/testutil"
9+
)
10+
11+
func TestOSPFNeighborsIPv4(t *testing.T) {
12+
c := newFakeClient()
13+
c.prepare("api/v2/monitor/router/ospf/neighbors", "testdata/router-ospf-neighbors.jsonnet")
14+
r := prometheus.NewPedanticRegistry()
15+
if !testProbe(probeOSPFNeighbors, c, r) {
16+
t.Errorf("probeOSPFNeighbors() returned non-success")
17+
}
18+
19+
em := `
20+
# HELP fortigate_ospf_neighbor_info List all discovered OSPF neighbors, return state as value (1 - Down, 2 - Attempt, 3 - Init, 4 - Two way, 5 - Exchange start, 6 - Exchange, 7 - Loading, 8 - Full)
21+
# TYPE fortigate_ospf_neighbor_info gauge
22+
fortigate_ospf_neighbor_info{neighbor_ip="10.0.0.1",priority="3",router_id="12345",state="Full",vdom="root"} 7
23+
`
24+
25+
if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil {
26+
t.Fatalf("metric compare: err %v", err)
27+
}
28+
}

Diff for: pkg/probe/probe.go

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func (p *ProbeCollector) Probe(ctx context.Context, target map[string]string, hc
148148
{"Wifi/Clients", probeWifiClients},
149149
{"Wifi/ManagedAP", probeWifiManagedAP},
150150
{"Switch/ManagedSwitch", probeManagedSwitch},
151+
{"OSPF/Neighbors", probeOSPFNeighbors},
151152
} {
152153
wanted := false
153154

Diff for: pkg/probe/testdata/router-ospf-neighbors.jsonnet

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# api/v2/monitor/router/ospf/neighbors?vdom=*
2+
[
3+
{
4+
"http_method":"GET",
5+
"results":[
6+
{
7+
"neighbor_ip":"10.0.0.1",
8+
"priority":3,
9+
"state":"Full",
10+
"router_id":"12345"
11+
}
12+
],
13+
"vdom":"root",
14+
"path":"router",
15+
"name":"ospf",
16+
"action":"neighbors",
17+
"status":"success",
18+
"serial":"FGT61FT000000000",
19+
"version":"v7.0.0",
20+
"build":66
21+
}
22+
]

0 commit comments

Comments
 (0)