|
| 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 | + |
| 19 | + "github.com/prometheus-community/fortigate_exporter/pkg/http" |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | +) |
| 22 | + |
| 23 | +func probeSystemVdomResource(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) { |
| 24 | + |
| 25 | + vdomDesc := make(map[string]*prometheus.Desc) |
| 26 | + vdomDesc["cpu"] = prometheus.NewDesc( |
| 27 | + "fortigate_vdom_resource_cpu_usage_ratio", |
| 28 | + "Current VDOM CPU usage in percentage", |
| 29 | + []string{"vdom"}, nil, |
| 30 | + ) |
| 31 | + vdomDesc["memory"] = prometheus.NewDesc( |
| 32 | + "fortigate_vdom_resource_memory_usage_ratio", |
| 33 | + "Current VDOM memory usage in percentage", |
| 34 | + []string{"vdom"}, nil, |
| 35 | + ) |
| 36 | + vdomDesc["setup_rate"] = prometheus.NewDesc( |
| 37 | + "fortigate_vdom_resource_setup_ratio", |
| 38 | + "Current VDOM memory usage in percentage", |
| 39 | + []string{"vdom"}, nil, |
| 40 | + ) |
| 41 | + vdomDesc["is_deletable"] = prometheus.NewDesc( |
| 42 | + "fortigate_vdom_resource_deletable", |
| 43 | + "1 if VDOM is deletable", |
| 44 | + []string{"vdom"}, nil, |
| 45 | + ) |
| 46 | + vdomDesc["id"] = prometheus.NewDesc( |
| 47 | + "fortigate_vdom_resource_object_id", |
| 48 | + "Object Resource ID", |
| 49 | + []string{"vdom", "object"},nil, |
| 50 | + ) |
| 51 | + vdomDesc["custom_max"] = prometheus.NewDesc( |
| 52 | + "fortigate_vdom_resource_object_custom_max", |
| 53 | + "Object Custom Max", |
| 54 | + []string{"vdom", "object"},nil, |
| 55 | + ) |
| 56 | + vdomDesc["min_custom_value"] = prometheus.NewDesc( |
| 57 | + "fortigate_vdom_resource_object_custom_min_value", |
| 58 | + "Object Minimum custom value", |
| 59 | + []string{"vdom", "object"}, nil, |
| 60 | + ) |
| 61 | + vdomDesc["max_custom_value"] = prometheus.NewDesc( |
| 62 | + "fortigate_vdom_resource_object_custom_max_value", |
| 63 | + "Object Maximum custom value", |
| 64 | + []string{"vdom", "object"},nil, |
| 65 | + ) |
| 66 | + vdomDesc["guaranteed"] = prometheus.NewDesc( |
| 67 | + "fortigate_vdom_resource_object_guaranteed", |
| 68 | + "Object Guaranteed", |
| 69 | + []string{"vdom", "object"},nil, |
| 70 | + ) |
| 71 | + vdomDesc["min_guaranteed_value"] = prometheus.NewDesc( |
| 72 | + "fortigate_vdom_resource_object_guaranteed_max_value", |
| 73 | + "Object Minimum guaranteed value", |
| 74 | + []string{"vdom", "object"},nil, |
| 75 | + ) |
| 76 | + vdomDesc["max_guaranteed_value"] = prometheus.NewDesc( |
| 77 | + "fortigate_vdom_resource_object_guaranteed_min_value", |
| 78 | + "Object Maximum guaranteed value", |
| 79 | + []string{"vdom", "object"},nil, |
| 80 | + ) |
| 81 | + vdomDesc["global_max"] = prometheus.NewDesc( |
| 82 | + "fortigate_vdom_resource_object_global_max", |
| 83 | + "Object Global max", |
| 84 | + []string{"vdom", "object"},nil, |
| 85 | + ) |
| 86 | + vdomDesc["current_usage"] = prometheus.NewDesc( |
| 87 | + "fortigate_vdom_resource_object_current_usage", |
| 88 | + "Object Current usage", |
| 89 | + []string{"vdom", "object"},nil, |
| 90 | + ) |
| 91 | + vdomDesc["usage_percent"] = prometheus.NewDesc( |
| 92 | + "fortigate_vdom_resource_object_usage_ratio", |
| 93 | + "Object Usage percentage", |
| 94 | + []string{"vdom", "object"},nil, |
| 95 | + ) |
| 96 | + |
| 97 | + type VDOMResourceResult struct { |
| 98 | + Result interface{} `json:"results"` |
| 99 | + Vdom string `json:"vdom"` |
| 100 | + } |
| 101 | + |
| 102 | + var res []VDOMResourceResult |
| 103 | + if err := c.Get("api/v2/monitor/system/vdom-resource", "vdom=*", &res); err != nil { |
| 104 | + log.Printf("Error: %v", err) |
| 105 | + return nil, false |
| 106 | + } |
| 107 | + |
| 108 | + m := []prometheus.Metric{} |
| 109 | + for _, result := range res { |
| 110 | + for k, elem := range result.Result.(map[string]interface{}) { |
| 111 | + switch k { |
| 112 | + case "cpu", "memory", "setup_rate": |
| 113 | + m = append(m, prometheus.MustNewConstMetric(vdomDesc[k], prometheus.GaugeValue, elem.(float64), result.Vdom)) |
| 114 | + case "is_deletable": |
| 115 | + if elem.(bool) { |
| 116 | + m = append(m, prometheus.MustNewConstMetric(vdomDesc[k], prometheus.GaugeValue, 1, result.Vdom)) |
| 117 | + } else { |
| 118 | + m = append(m, prometheus.MustNewConstMetric(vdomDesc[k], prometheus.GaugeValue, 0, result.Vdom)) |
| 119 | + } |
| 120 | + case "session", |
| 121 | + "ipsec-phase1", |
| 122 | + "ipsec-phase2", |
| 123 | + "ipsec-phase1-interface", |
| 124 | + "ipsec-phase2-interface", |
| 125 | + "dialup-tunnel", |
| 126 | + "firewall-policy", |
| 127 | + "firewall-address", |
| 128 | + "firewall-addrgrp", |
| 129 | + "custom-service", |
| 130 | + "service-group", |
| 131 | + "onetime-schedule", |
| 132 | + "recurring-schedule", |
| 133 | + "user", |
| 134 | + "user-group", |
| 135 | + "sslvpn", |
| 136 | + "proxy", |
| 137 | + "log-disk-quota": |
| 138 | + for val, e := range elem.(map[string]interface{}) { |
| 139 | + m = append(m, prometheus.MustNewConstMetric(vdomDesc[val], prometheus.GaugeValue, e.(float64), result.Vdom, k)) |
| 140 | + } |
| 141 | + default: |
| 142 | + log.Printf("Missing handler for: %s", k) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return m, true |
| 147 | +} |
0 commit comments