|
| 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 | + "encoding/json" |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + |
| 22 | + "github.com/prometheus-community/fortigate_exporter/pkg/http" |
| 23 | +) |
| 24 | + |
| 25 | +func probeSystemGlobalLocation(c http.FortiHTTP, _ *TargetMetadata) ([]prometheus.Metric, bool) { |
| 26 | + location := prometheus.NewDesc( |
| 27 | + "fortigate_location_info", |
| 28 | + "System geographic location (static metadata)", |
| 29 | + []string{"latitude", "longitude"}, |
| 30 | + nil, |
| 31 | + ) |
| 32 | + |
| 33 | + type SystemGlobalLocation struct { |
| 34 | + Latitude string `json:"gui-device-latitude"` |
| 35 | + Longitude string `json:"gui-device-longitude"` |
| 36 | + } |
| 37 | + |
| 38 | + type systemGlobalLocationResponse struct { |
| 39 | + Results json.RawMessage `json:"results"` |
| 40 | + } |
| 41 | + |
| 42 | + var resp systemGlobalLocationResponse |
| 43 | + |
| 44 | + if err := c.Get("api/v2/cmdb/system/global", "vdom=root", &resp); err != nil { |
| 45 | + log.Printf("Error: %v", err) |
| 46 | + return nil, false |
| 47 | + } |
| 48 | + |
| 49 | + var loc SystemGlobalLocation |
| 50 | + |
| 51 | + // Try object first |
| 52 | + if err := json.Unmarshal(resp.Results, &loc); err != nil { |
| 53 | + // Fallback to array |
| 54 | + var arr []SystemGlobalLocation |
| 55 | + if err := json.Unmarshal(resp.Results, &arr); err != nil || len(arr) == 0 { |
| 56 | + return nil, true |
| 57 | + } |
| 58 | + loc = arr[0] |
| 59 | + } |
| 60 | + |
| 61 | + if loc.Latitude == "" || loc.Longitude == "" { |
| 62 | + return nil, true |
| 63 | + } |
| 64 | + |
| 65 | + m := []prometheus.Metric{ |
| 66 | + prometheus.MustNewConstMetric( |
| 67 | + location, |
| 68 | + prometheus.GaugeValue, |
| 69 | + 1, |
| 70 | + loc.Latitude, |
| 71 | + loc.Longitude, |
| 72 | + ), |
| 73 | + } |
| 74 | + return m, true |
| 75 | +} |
0 commit comments