-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added OSPF metrics and corresponding testcase
- Loading branch information
Joel Norberg
committed
Nov 24, 2023
1 parent
9da7dd6
commit f0887dd
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package probe | ||
|
||
import ( | ||
"log" | ||
"strconv" | ||
|
||
"github.com/bluecmd/fortigate_exporter/pkg/http" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type OSPFNeighbor struct { | ||
NeighborIP string `json:"neighbor_ip"` | ||
RouterID string `json:"router_id"` | ||
State string `json:"state"` | ||
Priority int `json:"priority"` | ||
} | ||
|
||
type OSPFNeighborResponse struct { | ||
Results []OSPFNeighbor `json:"results"` | ||
VDOM string `json:"vdom"` | ||
Version string `json:"version"` | ||
} | ||
|
||
func probeOSPFNeighbors(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) { | ||
if meta.VersionMajor < 7 { | ||
// not supported version. Before 7.0.0 the requested endpoint doesn't exist | ||
return nil, true | ||
} | ||
var ( | ||
mOSPFNeighbor = prometheus.NewDesc( | ||
"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)", | ||
[]string{"vdom", "state", "priority", "router_id", "neighbor_ip"}, nil, | ||
) | ||
) | ||
|
||
var rs []OSPFNeighborResponse | ||
|
||
if err := c.Get("api/v2/monitor/router/ospf/neighbors", "vdom=*", &rs); err != nil { | ||
log.Printf("Error: %v", err) | ||
return nil, false | ||
} | ||
|
||
m := []prometheus.Metric{} | ||
|
||
for _, r := range rs { | ||
for _, peer := range r.Results { | ||
m = append(m, prometheus.MustNewConstMetric(mOSPFNeighbor, prometheus.GaugeValue, ospfStateToNumber(peer.State), r.VDOM, peer.State, strconv.Itoa(peer.Priority), peer.RouterID, peer.NeighborIP)) | ||
} | ||
} | ||
|
||
return m, true | ||
} | ||
|
||
func ospfStateToNumber(ospfState string) float64 { | ||
switch ospfState { | ||
case "Attempt": | ||
return 1 | ||
case "Init": | ||
return 2 | ||
case "Two way": | ||
return 3 | ||
case "Exchange start": | ||
return 4 | ||
case "Exchange": | ||
return 5 | ||
case "Loading": | ||
return 6 | ||
case "Full": | ||
return 7 | ||
default: // Down | ||
return 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package probe | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
) | ||
|
||
func TestOSPFNeighborsIPv4(t *testing.T) { | ||
c := newFakeClient() | ||
c.prepare("api/v2/monitor/router/ospf/neighbors", "testdata/router-ospf-neighbors.jsonnet") | ||
r := prometheus.NewPedanticRegistry() | ||
if !testProbe(probeOSPFNeighbors, c, r) { | ||
t.Errorf("probeOSPFNeighbors() returned non-success") | ||
} | ||
|
||
em := ` | ||
# 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) | ||
# TYPE fortigate_ospf_neighbor_info gauge | ||
fortigate_ospf_neighbor_info{neighbor_ip="10.0.0.1",priority="3",router_id="12345",state="Full",vdom="root"} 7 | ||
` | ||
|
||
if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil { | ||
t.Fatalf("metric compare: err %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# api/v2/monitor/router/ospf/neighbors?vdom=* | ||
[ | ||
{ | ||
"http_method":"GET", | ||
"results":[ | ||
{ | ||
"neighbor_ip":"10.0.0.1", | ||
"priority":3, | ||
"state":"Full", | ||
"router_id":"12345" | ||
} | ||
], | ||
"vdom":"root", | ||
"path":"router", | ||
"name":"ospf", | ||
"action":"neighbors", | ||
"status":"success", | ||
"serial":"FGT61FT000000000", | ||
"version":"v7.0.0", | ||
"build":66 | ||
} | ||
] |