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 probeSystemNtpStatus (c http.FortiHTTP , meta * TargetMetadata ) ([]prometheus.Metric , bool ) {
25+ var (
26+ ntpExpires = prometheus .NewDesc (
27+ "fortigate_system_ntp_expires_seconds" ,
28+ "NTP expire time, in seconds" ,
29+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
30+ )
31+ ntpStratum = prometheus .NewDesc (
32+ "fortigate_system_ntp_stratum" ,
33+ "NTP stratum value" ,
34+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
35+ )
36+ ntpRefTime = prometheus .NewDesc (
37+ "fortigate_system_ntp_reftime_seconds" ,
38+ "NTP reftime in epoch seconds" ,
39+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
40+ )
41+ ntpOffset = prometheus .NewDesc (
42+ "fortigate_system_ntp_offset_seconds" ,
43+ "NTP combined offset, in seconds" ,
44+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
45+ )
46+ ntpDelay = prometheus .NewDesc (
47+ "fortigate_system_ntp_delay_seconds" ,
48+ "NTP round trip delay, in seconds" ,
49+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
50+ )
51+ ntpDispersion = prometheus .NewDesc (
52+ "fortigate_system_ntp_dispersion_seconds" ,
53+ "NTP dispersion to primary clock, in seconds" ,
54+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
55+ )
56+ ntpPeerDispersion = prometheus .NewDesc (
57+ "fortigate_system_ntp_dispersion_peer_seconds" ,
58+ "NTP peer dispersion, in seconds" ,
59+ []string {"ip" , "server" , "reachable" , "selected" , "version" , "vdom" }, nil ,
60+ )
61+ )
62+
63+ type SystemNtpStatus struct {
64+ Ip string `json:"ip"`
65+ Server string `json:"server"`
66+ Reachable bool `json:"reachable"`
67+ Expires int `json:"expires"`
68+ Selected bool `json:"selected"`
69+ Version int `json:"version"`
70+ Stratum int `json:"stratum"`
71+ Reftime int `json:"reftime"`
72+ Offset float64 `json:"offset"`
73+ Delay float64 `json:"delay"`
74+ Dispersion float64 `json:"dispersion"`
75+ PeerDispersion int `json:"peer_dispersion"`
76+ }
77+
78+ type SystemNtpStatusResult struct {
79+ Results []SystemNtpStatus `json:"results"`
80+ VDOM string `json:"vdom"`
81+ }
82+
83+ var result []SystemNtpStatusResult
84+ if err := c .Get ("api/v2/monitor/system/ntp/status" ,"vdom=*" ,& result ); err != nil {
85+ log .Printf ("Error: %v" , err )
86+ return nil , false
87+ }
88+
89+ m := []prometheus.Metric {}
90+ if meta .VersionMajor >= 7 && meta .VersionMinor >= 4 {
91+ for _ , res := range result {
92+ for _ , r := range res .Results {
93+ m = append (m , prometheus .MustNewConstMetric (ntpExpires , prometheus .GaugeValue , float64 (r .Expires ), r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
94+ m = append (m , prometheus .MustNewConstMetric (ntpStratum , prometheus .GaugeValue , float64 (r .Stratum ), r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
95+ m = append (m , prometheus .MustNewConstMetric (ntpRefTime , prometheus .CounterValue , float64 (r .Reftime ), r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
96+ m = append (m , prometheus .MustNewConstMetric (ntpOffset , prometheus .GaugeValue , float64 (r .Offset ) * 0.001 , r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
97+ m = append (m , prometheus .MustNewConstMetric (ntpDelay , prometheus .GaugeValue , float64 (r .Delay ) * 0.001 , r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
98+ m = append (m , prometheus .MustNewConstMetric (ntpDispersion , prometheus .GaugeValue , float64 (r .Dispersion ) * 0.001 , r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
99+ m = append (m , prometheus .MustNewConstMetric (ntpPeerDispersion , prometheus .GaugeValue , float64 (r .PeerDispersion ) * 0.001 , r .Ip , r .Server , strconv .FormatBool (r .Reachable ), strconv .FormatBool (r .Reachable ), strconv .Itoa (r .Version ), res .VDOM ))
100+ }
101+ }
102+ } else {
103+ log .Printf ("Not implemented in versions under 7.4" )
104+ }
105+ return m , true
106+ }
0 commit comments