|
| 1 | +// Copyright 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 probeSystemHaPeer(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) { |
| 26 | + info := prometheus.NewDesc( |
| 27 | + "fortigate_ha_peer_info", |
| 28 | + "Information about the ha peer.", |
| 29 | + []string{"serial", "vcluster", "hostname", "priority"}, nil, |
| 30 | + ) |
| 31 | + |
| 32 | + primary := prometheus.NewDesc( |
| 33 | + "fortigate_ha_peer_primary", |
| 34 | + "True when the peer device is the HA primary.", |
| 35 | + []string{"vcluster", "hostname"}, nil, |
| 36 | + ) |
| 37 | + |
| 38 | + master := prometheus.NewDesc( |
| 39 | + "fortigate_ha_peer_master", |
| 40 | + "True when the peer device is the HA master.", |
| 41 | + []string{"vcluster", "hostname"}, nil, |
| 42 | + ) |
| 43 | + |
| 44 | + type SystemHaPeer struct { |
| 45 | + Serial string `json:"serial_no"` |
| 46 | + Vcluster int64 `json:"vcluster_id"` |
| 47 | + Priority float64 `json:"priority"` |
| 48 | + Hostname string `json:"hostname"` |
| 49 | + Master bool `json:"master"` |
| 50 | + Primary bool `json:"primary"` |
| 51 | + } |
| 52 | + |
| 53 | + type SystemHaPeerResult struct { |
| 54 | + Result []SystemHaPeer `json:"results"` |
| 55 | + } |
| 56 | + |
| 57 | + var res SystemHaPeerResult |
| 58 | + if err := c.Get("api/v2/monitor/system/ha-peer", "", &res); err != nil { |
| 59 | + log.Printf("Warning: %v", err) |
| 60 | + return nil, false |
| 61 | + } |
| 62 | + m := []prometheus.Metric{} |
| 63 | + for _, r := range res.Result { |
| 64 | + if meta.VersionMajor >= 7 && meta.VersionMinor >= 4 { |
| 65 | + m = append(m, prometheus.MustNewConstMetric(info, prometheus.GaugeValue, 1, r.Serial, strconv.FormatInt(r.Vcluster, 10), r.Hostname, strconv.FormatFloat(r.Priority, 'f', -1, 64))) |
| 66 | + if r.Primary { |
| 67 | + m = append(m, prometheus.MustNewConstMetric(primary, prometheus.GaugeValue, 1, strconv.FormatInt(r.Vcluster, 10), r.Hostname)) |
| 68 | + } else { |
| 69 | + m = append(m, prometheus.MustNewConstMetric(primary, prometheus.GaugeValue, 0, strconv.FormatInt(r.Vcluster, 10), r.Hostname)) |
| 70 | + } |
| 71 | + if meta.VersionMinor == 4 { |
| 72 | + if r.Master { |
| 73 | + m = append(m, prometheus.MustNewConstMetric(master, prometheus.GaugeValue, 1, strconv.FormatInt(r.Vcluster, 10), r.Hostname)) |
| 74 | + } else { |
| 75 | + m = append(m, prometheus.MustNewConstMetric(master, prometheus.GaugeValue, 0, strconv.FormatInt(r.Vcluster, 10), r.Hostname)) |
| 76 | + } |
| 77 | + } |
| 78 | + } else { |
| 79 | + m = append(m, prometheus.MustNewConstMetric(info, prometheus.GaugeValue, -1, "None", "0", "None", "false")) |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + return m, true |
| 84 | +} |
0 commit comments