Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added HA-Peer status metric #303

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Global:
* _WebUI/State_
* `fortigate_last_reboot_seconds`
* `fortigate_last_snapshot_seconds`
* _System/HAPeerStatus_
* `fortigate_ha_member_info`

Per-VDOM:

Expand Down
1 change: 1 addition & 0 deletions pkg/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (p *ProbeCollector) Probe(ctx context.Context, target map[string]string, hc
{"Wifi/ManagedAP", probeWifiManagedAP},
{"Switch/ManagedSwitch", probeManagedSwitch},
{"OSPF/Neighbors", probeOSPFNeighbors},
{"System/HAPeer", probeSystemHAPeer},
} {
wanted := false

Expand Down
55 changes: 55 additions & 0 deletions pkg/probe/system_ha_peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package probe

import (
"log"
"strconv"

"github.com/bluecmd/fortigate_exporter/pkg/http"
"github.com/prometheus/client_golang/prometheus"
)

func probeSystemHAPeer(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) {
var (
memberInfo = prometheus.NewDesc(
"fortigate_ha_peer_info",
"Info metrics regarding cluster HA peers",
[]string{"vdom", "hostname", "serial", "priority", "vcluster_id", "primary"}, nil,
)
)

type HAResults struct {
Hostname string `json:"hostname"`
SerialNo string `json:"serial_no"`
VclusterID int `json:"vcluster_id"`
Priority int `json:"priority"`
Primary bool `json:"primary"` // Set to true if primary node in FortiOS 7.4+
}

type HAResponse struct {
HTTPMethod string `json:"http_method"`
Results []HAResults `json:"results"`
VDOM string `json:"vdom"`
Path string `json:"path"`
Name string `json:"name"`
Status string `json:"status"`
Serial string `json:"serial"`
Version string `json:"version"`
Build int64 `json:"build"`
}
var r HAResponse

if err := c.Get("api/v2/monitor/system/ha-peer", "", &r); err != nil {
log.Printf("Error: %v", err)
return nil, false
}

m := []prometheus.Metric{}
for _, result := range r.Results {
strPrimary := strconv.FormatBool(result.Primary)
if meta.VersionMajor < 7 || (meta.VersionMajor == 7 && meta.VersionMinor < 4) {
strPrimary = "Unsupported"
}
m = append(m, prometheus.MustNewConstMetric(memberInfo, prometheus.GaugeValue, 1, r.VDOM, result.Hostname, result.SerialNo, strconv.Itoa(result.Priority), strconv.Itoa(result.VclusterID), strPrimary))
}
return m, true
}
57 changes: 57 additions & 0 deletions pkg/probe/system_ha_peer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package probe

import (
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
)

func TestHAPeer(t *testing.T) {
c := newFakeClient()
c.prepare("api/v2/monitor/system/ha-peer", "testdata/ha-peer.jsonnet")
r := prometheus.NewPedanticRegistry()
meta := &TargetMetadata{
VersionMajor: 7,
VersionMinor: 0,
}
if !testProbeWithMetadata(probeSystemHAPeer, c, meta, r) {
t.Errorf("probeSystemHAPeer() returned non-success")
}

em := `
# HELP fortigate_ha_peer_info Info metrics regarding cluster HA peers
# TYPE fortigate_ha_peer_info gauge
fortigate_ha_peer_info{hostname="member-name-1",primary="Unsupported",priority="200",serial="FGT61E4QXXXXXXXX1",vcluster_id="0",vdom="root"} 1
fortigate_ha_peer_info{hostname="member-name-2",primary="Unsupported",priority="100",serial="FGT61E4QXXXXXXXX2",vcluster_id="0",vdom="root"} 1
`

if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil {
t.Fatalf("metric compare: err %v", err)
}
}

func TestHAPeer74(t *testing.T) {
c := newFakeClient()
c.prepare("api/v2/monitor/system/ha-peer", "testdata/ha-peer-74+.jsonnet")
r := prometheus.NewPedanticRegistry()
meta := &TargetMetadata{
VersionMajor: 7,
VersionMinor: 4,
}
if !testProbeWithMetadata(probeSystemHAPeer, c, meta, r) {
t.Errorf("probeSystemHAPeer() returned non-success")
}

em := `
# HELP fortigate_ha_peer_info Info metrics regarding cluster HA peers
# TYPE fortigate_ha_peer_info gauge
fortigate_ha_peer_info{hostname="member-name-1",primary="true",priority="200",serial="FGT61E4QXXXXXXXX1",vcluster_id="0",vdom="root"} 1
fortigate_ha_peer_info{hostname="member-name-2",primary="false",priority="100",serial="FGT61E4QXXXXXXXX2",vcluster_id="0",vdom="root"} 1
`

if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil {
t.Fatalf("metric compare: err %v", err)
}
}
27 changes: 27 additions & 0 deletions pkg/probe/testdata/ha-peer-74+.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# api/v2/monitor/system/ha-peer
{
"http_method":"GET",
"results":[
{
"serial_no":"FGT61E4QXXXXXXXX1",
"vcluster_id":0,
"priority":200,
"hostname":"member-name-1",
"primary":true
},
{
"serial_no":"FGT61E4QXXXXXXXX2",
"vcluster_id":0,
"priority":100,
"hostname":"member-name-2"
}
],
"vdom":"root",
"path":"system",
"name":"ha-peer",
"action":"",
"status":"success",
"serial":"FGT61E4QXXXXXXXX1",
"version":"v7.4.0",
"build":700
}
26 changes: 26 additions & 0 deletions pkg/probe/testdata/ha-peer.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# api/v2/monitor/system/ha-peer
{
"http_method":"GET",
"results":[
{
"serial_no":"FGT61E4QXXXXXXXX1",
"vcluster_id":0,
"priority":200,
"hostname":"member-name-1"
},
{
"serial_no":"FGT61E4QXXXXXXXX2",
"vcluster_id":0,
"priority":100,
"hostname":"member-name-2"
}
],
"vdom":"root",
"path":"system",
"name":"ha-peer",
"action":"",
"status":"success",
"serial":"FGT61E4QXXXXXXXX1",
"version":"v7.0.12",
"build":523
}
Loading