|
| 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/client_golang/prometheus" |
| 21 | + |
| 22 | + "github.com/prometheus-community/fortigate_exporter/pkg/http" |
| 23 | +) |
| 24 | + |
| 25 | +func probeSystemCentralManagementStatus(c http.FortiHTTP, _ *TargetMetadata) ([]prometheus.Metric, bool) { |
| 26 | + var ( |
| 27 | + mode = prometheus.NewDesc( |
| 28 | + "fortigate_system_central_management_mode", |
| 29 | + "Operating mode of the central management.", |
| 30 | + []string{"mode", "server", "mgmt_ip", "mgmt_port", "sn", "pendfortman"}, nil, |
| 31 | + ) |
| 32 | + status = prometheus.NewDesc( |
| 33 | + "fortigate_system_central_management_status", |
| 34 | + "Status of the connection from FortiGate to the central management server.", |
| 35 | + []string{"status", "server", "mgmt_ip", "mgmt_port", "sn", "pendfortman"}, nil, |
| 36 | + ) |
| 37 | + registrationStatus = prometheus.NewDesc( |
| 38 | + "fortigate_system_central_management_registration_status", |
| 39 | + "Status of the registration from FortiGate to the central management server.", |
| 40 | + []string{"status", "server", "mgmt_ip", "mgmt_port", "sn", "pendfortman"}, nil, |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + type centralManagementStatus struct { |
| 45 | + Mode string `json:"mode"` |
| 46 | + Server string `json:"server"` |
| 47 | + Status string `json:"status"` |
| 48 | + RegStat string `json:"registration_status"` |
| 49 | + MgmtIP string `json:"mgmt_ip"` |
| 50 | + MgmtPort float64 `json:"mgmt_port"` |
| 51 | + Sn string `json:"sn"` |
| 52 | + PenFortMan string `json:"pending_fortimanager"` |
| 53 | + } |
| 54 | + |
| 55 | + type centralManagementStatusResult struct { |
| 56 | + Result centralManagementStatus `json:"results"` |
| 57 | + } |
| 58 | + |
| 59 | + var res centralManagementStatusResult |
| 60 | + if err := c.Get("api/v2/monitor/system/central-management/status", "skip_detect=true", &res); err != nil { |
| 61 | + log.Printf("Error: %v", err) |
| 62 | + return nil, false |
| 63 | + } |
| 64 | + |
| 65 | + m := []prometheus.Metric{} |
| 66 | + var normal, backup, down, up, handshake, inProgress, registered, unregistered, defaultValue float64 |
| 67 | + if res.Result.Mode == "normal" { |
| 68 | + normal = 1 |
| 69 | + } else { |
| 70 | + backup = 1 |
| 71 | + } |
| 72 | + switch res.Result.Status { |
| 73 | + case "down": |
| 74 | + down = 1 |
| 75 | + case "up": |
| 76 | + up = 1 |
| 77 | + case "handshake": |
| 78 | + handshake = 1 |
| 79 | + } |
| 80 | + switch res.Result.RegStat { |
| 81 | + case "in_progress": |
| 82 | + inProgress = 1 |
| 83 | + case "registered": |
| 84 | + registered = 1 |
| 85 | + case "unregistered": |
| 86 | + unregistered = 1 |
| 87 | + default: |
| 88 | + defaultValue = 1 |
| 89 | + } |
| 90 | + m = append(m, prometheus.MustNewConstMetric(mode, prometheus.GaugeValue, normal, "normal", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 91 | + m = append(m, prometheus.MustNewConstMetric(mode, prometheus.GaugeValue, backup, "backup", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 92 | + m = append(m, prometheus.MustNewConstMetric(status, prometheus.GaugeValue, down, "down", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 93 | + m = append(m, prometheus.MustNewConstMetric(status, prometheus.GaugeValue, up, "up", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 94 | + m = append(m, prometheus.MustNewConstMetric(status, prometheus.GaugeValue, handshake, "handshake", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 95 | + m = append(m, prometheus.MustNewConstMetric(registrationStatus, prometheus.GaugeValue, inProgress, "inprogress", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 96 | + m = append(m, prometheus.MustNewConstMetric(registrationStatus, prometheus.GaugeValue, registered, "registered", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 97 | + m = append(m, prometheus.MustNewConstMetric(registrationStatus, prometheus.GaugeValue, unregistered, "unregistered", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 98 | + m = append(m, prometheus.MustNewConstMetric(registrationStatus, prometheus.GaugeValue, defaultValue, "unknown", res.Result.Server, res.Result.MgmtIP, strconv.FormatFloat(res.Result.MgmtPort, 'f', -1, 64), res.Result.Sn, res.Result.PenFortMan)) |
| 99 | + |
| 100 | + return m, true |
| 101 | +} |
0 commit comments