Skip to content

Commit 8471644

Browse files
committed
Network latency implementation, tested in 7.4
Signed-off-by: Örnfeldt Philip (66140321) <[email protected]>
1 parent 81722bb commit 8471644

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Supported metrics right now as follows.
3737

3838
Global:
3939

40+
* _Network/Dns/Latency_
41+
* `fortigate_network_dns_latency`
42+
* `fortigate_network_dns_latest_update`
4043
* _System/SensorInfo_
4144
* `fortigate_sensor_fan_rpm`
4245
* `fortigate_sensor_temperature_celsius`
@@ -411,6 +414,7 @@ To improve security, limit permissions to required ones only (least privilege pr
411414
|Log/Fortianalyzer/Status | loggrp.config |api/v2/monitor/log/fortianalyzer |
412415
|Log/Fortianalyzer/Queue | loggrp.config |api/v2/monitor/log/fortianalyzer-queue |
413416
|Log/DiskUsage | loggrp.config |api/v2/monitor/log/current-disk-usage |
417+
|Network/Dns/Latency | sysgrp.cfg |api/v2/monitor/network/dns/latency |
414418
|System/AvailableCertificates | *any* |api/v2/monitor/system/available-certificates |
415419
|System/Fortimanager/Status | sysgrp.cfg |api/v2/monitor/system/fortimanager/status |
416420
|System/HAStatistics | sysgrp.cfg |api/v2/monitor/system/ha-statistics<br>api/v2/cmdb/system/ha |

pkg/probe/network_dns_latency.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +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+
114
package probe
215

316
import (
417
"log"
518

619
"github.com/prometheus-community/fortigate_exporter/pkg/http"
720
"github.com/prometheus/client_golang/prometheus"
8-
)
21+
)
22+
23+
func probeNetworkDnsLatency(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) {
24+
var (
25+
dnsLatency = prometheus.NewDesc(
26+
"fortigate_network_dns_latency",
27+
"Network dns latency",
28+
[]string{"service", "ip"}, nil,
29+
)
30+
dnsLastUpdate = prometheus.NewDesc(
31+
"fortigate_network_dns_latest_update",
32+
"Network dns last update",
33+
[]string{"service", "ip"}, nil,
34+
)
35+
)
36+
37+
type DnsLatencty struct {
38+
Service string `json:"service"`
39+
Latency float64 `json:"latency"`
40+
LastUpdate float64 `json:"last_update"`
41+
Ip string `json:"ip"`
42+
}
43+
44+
type DnsLatencyResult struct {
45+
Results []DnsLatencty `json:"results"`
46+
}
47+
48+
var res DnsLatencyResult
49+
if err := c.Get("api/v2/monitor/network/dns/latency", "", &res); err != nil {
50+
log.Printf("Warning: %v", err)
51+
return nil, false
52+
}
53+
m := []prometheus.Metric{}
54+
for _, r := range res.Results {
55+
m = append(m, prometheus.MustNewConstMetric(dnsLatency, prometheus.GaugeValue, r.Latency, r.Service, r.Ip))
56+
m = append(m, prometheus.MustNewConstMetric(dnsLastUpdate, prometheus.GaugeValue, r.LastUpdate, r.Service, r.Ip))
57+
}
58+
59+
return m, true
60+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
package probe
14+
15+
import (
16+
"strings"
17+
"testing"
18+
19+
"github.com/prometheus/client_golang/prometheus"
20+
"github.com/prometheus/client_golang/prometheus/testutil"
21+
)
22+
23+
func TestNetworkDnsLatency(t *testing.T) {
24+
c := newFakeClient()
25+
c.prepare("api/v2/monitor/network/dns/latency", "testdata/network-dns-latency.jsonnet")
26+
r := prometheus.NewPedanticRegistry()
27+
if !testProbe(probeNetworkDnsLatency, c, r) {
28+
t.Errorf("probeNetworkDnsLatency() returned non-success")
29+
}
30+
31+
em := `
32+
# HELP fortigate_network_dns_latency Network dns latency
33+
# TYPE fortigate_network_dns_latency gauge
34+
fortigate_network_dns_latency{ip="8.8.8.8",service="dns_server"} 10
35+
# HELP fortigate_network_dns_latest_update Network dns last update
36+
# TYPE fortigate_network_dns_latest_update gauge
37+
fortigate_network_dns_latest_update{ip="8.8.8.8",service="dns_server"} 1040
38+
`
39+
40+
if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil {
41+
t.Fatalf("metric compare: err %v", err)
42+
}
43+
}

pkg/probe/probe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (p *ProbeCollector) Probe(ctx context.Context, target map[string]string, hc
140140
{"Log/Fortianalyzer/Status", probeLogAnalyzer},
141141
{"Log/Fortianalyzer/Queue", probeLogAnalyzerQueue},
142142
{"Log/DiskUsage", probeLogCurrentDiskUsage},
143+
{"Network/Dns/Latency", probeNetworkDnsLatency},
143144
{"System/AvailableCertificates", probeSystemAvailableCertificates},
144145
{"System/Fortimanager/Status", probeSystemFortimanagerStatus},
145146
{"System/HAStatistics", probeSystemHAStatistics},

0 commit comments

Comments
 (0)