Skip to content

Commit a0f2dfc

Browse files
committed
System ha peer information added
Signed-off-by: Örnfeldt Philip (66140321) <philip.ornfeldt@forsakringskassan.se>
1 parent afcfb5f commit a0f2dfc

5 files changed

Lines changed: 161 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Global:
5151
* `fortigate_current_sessions`
5252
* _System/HAChecksums_
5353
* `fortigate_ha_member_has_role`
54+
* _System/HaPeer_
55+
* `fortigate_ha_peer_priority`
5456
* _License/Status_
5557
* `fortigate_license_vdom_usage`
5658
* `fortigate_license_vdom_max`
@@ -414,6 +416,7 @@ To improve security, limit permissions to required ones only (least privilege pr
414416
|System/AvailableCertificates | *any* |api/v2/monitor/system/available-certificates |
415417
|System/Fortimanager/Status | sysgrp.cfg |api/v2/monitor/system/fortimanager/status |
416418
|System/HAStatistics | sysgrp.cfg |api/v2/monitor/system/ha-statistics<br>api/v2/cmdb/system/ha |
419+
|System/Ha-peer | sysgrp.cfg |api/v2/monitor/system/ha-peer |
417420
|System/Interface | netgrp.cfg |api/v2/monitor/system/interface/select |
418421
|System/LinkMonitor | sysgrp.cfg |api/v2/monitor/system/link-monitor |
419422
|System/Resource/Usage | sysgrp.cfg |api/v2/monitor/system/resource/usage |

pkg/probe/probe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func (p *ProbeCollector) Probe(ctx context.Context, target map[string]string, hc
143143
{"System/AvailableCertificates", probeSystemAvailableCertificates},
144144
{"System/Fortimanager/Status", probeSystemFortimanagerStatus},
145145
{"System/HAStatistics", probeSystemHAStatistics},
146+
{"System/Ha-peer", probeSystemHaPeer},
146147
{"System/Interface", probeSystemInterface},
147148
{"System/LinkMonitor", probeSystemLinkMonitor},
148149
{"System/Resource/Usage", probeSystemResourceUsage},

pkg/probe/system_ha_peer.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package probe
15+
16+
import (
17+
"log"
18+
"strconv"
19+
20+
"github.com/prometheus-community/fortigate_exporter/pkg/http"
21+
"github.com/prometheus/client_golang/prometheus"
22+
)
23+
24+
func probeSystemHaPeer (c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) {
25+
var (
26+
Priority = prometheus.NewDesc(
27+
"fortigate_ha_peer_priority",
28+
"Priority level of the HA peer device.",
29+
[]string{"serial","vcluster","hostname","master","primary"}, nil,
30+
)
31+
)
32+
33+
type SystemHaPeer struct {
34+
Serial string `json:"serial_no"`
35+
Vcluster int64 `json:"vcluster_id"`
36+
Priority float64 `json:"priority"`
37+
Hostname string `json:"hostname"`
38+
Master bool `json:"master"`
39+
Primary bool `json:"primary"`
40+
}
41+
42+
type SystemHaPeerResult struct {
43+
Result []SystemHaPeer `json:"results"`
44+
}
45+
46+
var res SystemHaPeerResult
47+
if err := c.Get("api/v2/monitor/system/ha-peer", "", &res); err != nil {
48+
log.Printf("Warning: %v", err)
49+
return nil, false
50+
}
51+
m := []prometheus.Metric{}
52+
for _, r := range res.Result {
53+
if meta.VersionMajor >= 7 && meta.VersionMinor >= 4 {
54+
m = append(m, prometheus.MustNewConstMetric(Priority, prometheus.GaugeValue, r.Priority, r.Serial, strconv.FormatInt(r.Vcluster, 10), r.Hostname, strconv.FormatBool(r.Master), strconv.FormatBool(r.Primary)))
55+
} else {
56+
m = append(m, prometheus.MustNewConstMetric(Priority, prometheus.GaugeValue, r.Priority, r.Serial, strconv.FormatInt(r.Vcluster, 10), r.Hostname, strconv.FormatBool(r.Master), "Unsupported"))
57+
}
58+
}
59+
return m, true
60+
}

pkg/probe/system_ha_peer_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package probe
15+
16+
import (
17+
"strings"
18+
"testing"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
"github.com/prometheus/client_golang/prometheus/testutil"
22+
)
23+
24+
func TestSystemHaPeerOld(t *testing.T) {
25+
c := newFakeClient()
26+
c.prepare("api/v2/monitor/system/ha-peer", "testdata/system-ha-peer.jsonnet")
27+
r := prometheus.NewPedanticRegistry()
28+
meta := &TargetMetadata{
29+
VersionMajor: 7,
30+
VersionMinor: 0,
31+
}
32+
if !testProbeWithMetadata(probeSystemHaPeer, c, meta, r) {
33+
t.Errorf("probeSystemHaPeer() returned non-success")
34+
}
35+
36+
em := `
37+
# HELP fortigate_ha_peer_priority Priority level of the HA peer device.
38+
# TYPE fortigate_ha_peer_priority gauge
39+
fortigate_ha_peer_priority{hostname="member-name-1",master="false",primary="Unsupported",serial="FGT61E4QXXXXXXXX1",vcluster="0"} 200
40+
fortigate_ha_peer_priority{hostname="member-name-2",master="false",primary="Unsupported",serial="FGT61E4QXXXXXXXX2",vcluster="0"} 100
41+
`
42+
43+
if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil {
44+
t.Fatalf("metric compare: err %v", err)
45+
}
46+
}
47+
48+
func TestSystemHaPeerAfter74(t *testing.T) {
49+
c := newFakeClient()
50+
c.prepare("api/v2/monitor/system/ha-peer", "testdata/system-ha-peer.jsonnet")
51+
r := prometheus.NewPedanticRegistry()
52+
meta := &TargetMetadata{
53+
VersionMajor: 7,
54+
VersionMinor: 4,
55+
}
56+
if !testProbeWithMetadata(probeSystemHaPeer, c, meta, r) {
57+
t.Errorf("probeSystemHaPeer() returned non-success")
58+
}
59+
60+
em := `
61+
# HELP fortigate_ha_peer_priority Priority level of the HA peer device.
62+
# TYPE fortigate_ha_peer_priority gauge
63+
fortigate_ha_peer_priority{hostname="member-name-1",master="false",primary="true",serial="FGT61E4QXXXXXXXX1",vcluster="0"} 200
64+
fortigate_ha_peer_priority{hostname="member-name-2",master="false",primary="false",serial="FGT61E4QXXXXXXXX2",vcluster="0"} 100
65+
`
66+
67+
if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil {
68+
t.Fatalf("metric compare: err %v", err)
69+
}
70+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# api/v2/monitor/system/ha-peer
2+
{
3+
"http_method":"GET",
4+
"results":[
5+
{
6+
"serial_no":"FGT61E4QXXXXXXXX1",
7+
"vcluster_id":0,
8+
"priority":200,
9+
"hostname":"member-name-1",
10+
"primary":true
11+
},
12+
{
13+
"serial_no":"FGT61E4QXXXXXXXX2",
14+
"vcluster_id":0,
15+
"priority":100,
16+
"hostname":"member-name-2"
17+
}
18+
],
19+
"vdom":"root",
20+
"path":"system",
21+
"name":"ha-peer",
22+
"action":"",
23+
"status":"success",
24+
"serial":"FGT61E4QXXXXXXXX1",
25+
"version":"v7.4.0",
26+
"build":700
27+
}

0 commit comments

Comments
 (0)