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 probeSystemPerformanceStatus (c http.FortiHTTP , meta * TargetMetadata ) ([]prometheus.Metric , bool ) {
25+ var (
26+ cpuCoresUser = prometheus .NewDesc (
27+ "fortigate_system_performance_status_cpu_cores_user" ,
28+ "Percentage of CPU utilization that occurred at the user level." ,
29+ []string {"label" }, nil ,
30+ )
31+ cpuCoresSystem = prometheus .NewDesc (
32+ "fortigate_system_performance_status_cpu_cores_system" ,
33+ "Percentage of CPU utilization that occurred while executing at the system level." ,
34+ []string {"label" }, nil ,
35+ )
36+ cpuCoresNice = prometheus .NewDesc (
37+ "fortigate_system_performance_status_cpu_cores_nice" ,
38+ "Percentage of CPU utilization that occurred while executing at the user level with nice priority." ,
39+ []string {"label" }, nil ,
40+ )
41+ cpuCoresIdle = prometheus .NewDesc (
42+ "fortigate_system_performance_status_cpu_cores_idle" ,
43+ "Percentage of time that the CPU was idle and the system did not have an outstanding disk I/O request." ,
44+ []string {"label" }, nil ,
45+ )
46+ cpuCoresIowait = prometheus .NewDesc (
47+ "fortigate_system_performance_status_cpu_cores_iowait" ,
48+ "Percentage of time that the CPU was idle during which the system had an outstanding disk I/O request." ,
49+ []string {"label" }, nil ,
50+ )
51+ cpuUser = prometheus .NewDesc (
52+ "fortigate_system_performance_status_cpu_user" ,
53+ "Percentage of CPU utilization that occurred at the user level." ,
54+ []string {"label" }, nil ,
55+ )
56+ cpuSystem = prometheus .NewDesc (
57+ "fortigate_system_performance_status_cpu_system" ,
58+ "Percentage of CPU utilization that occurred while executing at the system level." ,
59+ []string {"label" }, nil ,
60+ )
61+ cpuNice = prometheus .NewDesc (
62+ "fortigate_system_performance_status_cpu_nice" ,
63+ "Percentage of CPU utilization that occurred while executing at the user level with nice priority." ,
64+ []string {"label" }, nil ,
65+ )
66+ cpuIdle = prometheus .NewDesc (
67+ "fortigate_system_performance_status_cpu_idle" ,
68+ "Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request." ,
69+ []string {"label" }, nil ,
70+ )
71+ cpuIowait = prometheus .NewDesc (
72+ "fortigate_system_performance_status_cpu_iowait" ,
73+ "Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request." ,
74+ []string {"label" }, nil ,
75+ )
76+ memTotal = prometheus .NewDesc (
77+ "fortigate_system_performance_status_mem_total" ,
78+ "All the installed memory in RAM, in bytes." ,
79+ []string {"label" }, nil ,
80+ )
81+ memUsed = prometheus .NewDesc (
82+ "fortigate_system_performance_status_mem_used" ,
83+ "Memory are being used, in bytes." ,
84+ []string {"label" }, nil ,
85+ )
86+ memFree = prometheus .NewDesc (
87+ "fortigate_system_performance_status_mem_free" ,
88+ "All the memory in RAM that is not being used for anything (even caches), in bytes." ,
89+ []string {"label" }, nil ,
90+ )
91+ memFreeable = prometheus .NewDesc (
92+ "fortigate_system_performance_status_mem_freeable" ,
93+ "Freeable buffers/caches memory, in bytes." ,
94+ []string {"label" }, nil ,
95+ )
96+ )
97+
98+ type SystemPerformanceStatusCores struct {
99+ User int `json:"user"`
100+ System int `json:"system"`
101+ Nice int `json:"nice"`
102+ Idle int `json:"idle"`
103+ Iowait int `json:"iowait"`
104+ }
105+
106+ type SystemPerformanceStatusCpu struct {
107+ Cores []SystemPerformanceStatusCores `json:"cores"`
108+ User int `json:"user"`
109+ System int `json:"system"`
110+ Nice int `json:"nice"`
111+ Idle int `json:"idle"`
112+ Iowait int `json:"iowait"`
113+ }
114+
115+ type SystemPerformanceStatusMem struct {
116+ Total int `json:"total"`
117+ Used int `json:"used"`
118+ Free int `json:"free"`
119+ Freeable int `json:"freeable"`
120+ }
121+
122+ type SystemPerformanceStatus struct {
123+ Cpu SystemPerformanceStatusCpu `json:"cpu"`
124+ Mem SystemPerformanceStatusMem `json:"mem"`
125+ }
126+
127+ type SystemPerformanceStatusResult struct {
128+ Results []SystemPerformanceStatus `json:"results"`
129+ }
130+
131+ var res SystemPerformanceStatusResult
132+ if err := c .Get ("api/v2/monitor/system/performance/status" , "" , & res ); err != nil {
133+ log .Printf ("Error: %v" , err )
134+ return nil , false
135+ }
136+ m := []prometheus.Metric {}
137+ var cpu_num , mem_num , core_num string
138+ for n , r := range res .Results {
139+ cpu_num = "cpu_" + strconv .Itoa (n )
140+ mem_num = "mem_" + strconv .Itoa (n )
141+ for i , core := range r .Cpu .Cores {
142+ core_num = "core_" + strconv .Itoa (i )
143+ m = append (m , prometheus .MustNewConstMetric (cpuCoresUser , prometheus .GaugeValue , float64 (core .User ), cpu_num + "_" + core_num ))
144+ m = append (m , prometheus .MustNewConstMetric (cpuCoresSystem , prometheus .GaugeValue , float64 (core .System ), cpu_num + "_" + core_num ))
145+ m = append (m , prometheus .MustNewConstMetric (cpuCoresNice , prometheus .GaugeValue , float64 (core .Nice ), cpu_num + "_" + core_num ))
146+ m = append (m , prometheus .MustNewConstMetric (cpuCoresIdle , prometheus .GaugeValue , float64 (core .Idle ), cpu_num + "_" + core_num ))
147+ m = append (m , prometheus .MustNewConstMetric (cpuCoresIowait , prometheus .GaugeValue , float64 (core .Iowait ), cpu_num + "_" + core_num ))
148+ }
149+ m = append (m , prometheus .MustNewConstMetric (cpuUser ,prometheus .GaugeValue , float64 (r .Cpu .User ), cpu_num ))
150+ m = append (m , prometheus .MustNewConstMetric (cpuSystem ,prometheus .GaugeValue , float64 (r .Cpu .System ), cpu_num ))
151+ m = append (m , prometheus .MustNewConstMetric (cpuNice ,prometheus .GaugeValue , float64 (r .Cpu .Nice ), cpu_num ))
152+ m = append (m , prometheus .MustNewConstMetric (cpuIdle ,prometheus .GaugeValue , float64 (r .Cpu .Idle ), cpu_num ))
153+ m = append (m , prometheus .MustNewConstMetric (cpuIowait ,prometheus .GaugeValue , float64 (r .Cpu .Iowait ), cpu_num ))
154+ m = append (m , prometheus .MustNewConstMetric (memTotal ,prometheus .GaugeValue , float64 (r .Mem .Total ), mem_num ))
155+ m = append (m , prometheus .MustNewConstMetric (memUsed ,prometheus .GaugeValue , float64 (r .Mem .Used ), mem_num ))
156+ m = append (m , prometheus .MustNewConstMetric (memFree ,prometheus .GaugeValue , float64 (r .Mem .Free ), mem_num ))
157+ m = append (m , prometheus .MustNewConstMetric (memFreeable ,prometheus .GaugeValue , float64 (r .Mem .Freeable ), mem_num ))
158+ }
159+
160+ return m , true
161+ }
0 commit comments