From 62da407bc2792e5c543e9bf04f7bd9bed5a24ed3 Mon Sep 17 00:00:00 2001 From: Neil Schelly Date: Wed, 19 Jul 2023 15:56:29 -0400 Subject: [PATCH] feat(probe): status of SDN Connectors (#210) * Adding functionality for querying the status of SDN Connectors --- README.md | 3 + pkg/probe/probe.go | 1 + pkg/probe/system_sdn_connector.go | 62 +++++++++++++++++++ pkg/probe/system_sdn_connector_test.go | 33 ++++++++++ .../testdata/system-sdn-connector.jsonnet | 43 +++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 pkg/probe/system_sdn_connector.go create mode 100644 pkg/probe/system_sdn_connector_test.go create mode 100644 pkg/probe/testdata/system-sdn-connector.jsonnet diff --git a/README.md b/README.md index 6831ae7..0e4cc46 100755 --- a/README.md +++ b/README.md @@ -75,6 +75,9 @@ Per-VDOM: * `fortigate_interface_receive_bytes_total` * `fortigate_interface_transmit_errors_total` * `fortigate_interface_receive_errors_total` + * _System/SDNConnector_ + * `fortigate_system_sdn_connector_status` + * `fortigate_system_sdn_connector_last_update_seconds` * _User/Fsso_ * `fortigate_user_fsso_info` * _VPN/Ssl/Connections_ diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go index e9f9e60..a44ab69 100644 --- a/pkg/probe/probe.go +++ b/pkg/probe/probe.go @@ -132,6 +132,7 @@ func (p *ProbeCollector) Probe(ctx context.Context, target map[string]string, hc {"System/Interface", probeSystemInterface}, {"System/LinkMonitor", probeSystemLinkMonitor}, {"System/Resource/Usage", probeSystemResourceUsage}, + {"System/SDNConnector", probeSystemSDNConnector}, {"System/SensorInfo", probeSystemSensorInfo}, {"System/Status", probeSystemStatus}, {"System/VDOMResources", probeSystemVDOMResources}, diff --git a/pkg/probe/system_sdn_connector.go b/pkg/probe/system_sdn_connector.go new file mode 100644 index 0000000..e34e722 --- /dev/null +++ b/pkg/probe/system_sdn_connector.go @@ -0,0 +1,62 @@ +package probe + +import ( + "log" + + "github.com/bluecmd/fortigate_exporter/pkg/http" + "github.com/prometheus/client_golang/prometheus" +) + +type SystemSDNConnectorResults struct { + Name string `json:"name"` + Type string `json:"type"` + Status string `json:"status"` + Updating bool `json:"updating"` + LastUpdate int `json:"last_update"` +} + +type SystemSDNConnector struct { + Results []SystemSDNConnectorResults `json:"results"` + VDOM string `json:"vdom"` +} + +func probeSystemSDNConnector(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) { + var ( + SDNConnectorsStatus = prometheus.NewDesc( + "fortigate_system_sdn_connector_status", + "Status of SDN connectors (0=Disabled, 1=Down, 2=Unknown, 3=Up, 4=Updating)", + []string{"vdom", "name", "type"}, nil, + ) + SDNConnectorsLastUpdate = prometheus.NewDesc( + "fortigate_system_sdn_connector_last_update_seconds", + "Last update time for SDN connectors (in seconds from epoch)", + []string{"vdom", "name", "type"}, nil, + ) + ) + + var res []SystemSDNConnector + if err := c.Get("api/v2/monitor/system/sdn-connector/status", "vdom=*", &res); err != nil { + log.Printf("Error: %v", err) + return nil, false + } + + m := []prometheus.Metric{} + for _, r := range res { + for _, sdnConn := range r.Results { + if sdnConn.Status == "Disabled" { + m = append(m, prometheus.MustNewConstMetric(SDNConnectorsStatus, prometheus.GaugeValue, float64(0), r.VDOM, sdnConn.Name, sdnConn.Type)) + } else if sdnConn.Status == "Down" { + m = append(m, prometheus.MustNewConstMetric(SDNConnectorsStatus, prometheus.GaugeValue, float64(1), r.VDOM, sdnConn.Name, sdnConn.Type)) + } else if sdnConn.Status == "Unknown" { + m = append(m, prometheus.MustNewConstMetric(SDNConnectorsStatus, prometheus.GaugeValue, float64(2), r.VDOM, sdnConn.Name, sdnConn.Type)) + } else if sdnConn.Status == "Up" { + m = append(m, prometheus.MustNewConstMetric(SDNConnectorsStatus, prometheus.GaugeValue, float64(3), r.VDOM, sdnConn.Name, sdnConn.Type)) + } else if sdnConn.Status == "Updating" { + m = append(m, prometheus.MustNewConstMetric(SDNConnectorsStatus, prometheus.GaugeValue, float64(4), r.VDOM, sdnConn.Name, sdnConn.Type)) + } + m = append(m, prometheus.MustNewConstMetric(SDNConnectorsLastUpdate, prometheus.GaugeValue, float64(sdnConn.LastUpdate), r.VDOM, sdnConn.Name, sdnConn.Type)) + } + } + + return m, true +} diff --git a/pkg/probe/system_sdn_connector_test.go b/pkg/probe/system_sdn_connector_test.go new file mode 100644 index 0000000..30bb071 --- /dev/null +++ b/pkg/probe/system_sdn_connector_test.go @@ -0,0 +1,33 @@ +package probe + +import ( + "strings" + "testing" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" +) + +func TestSystemSDNConnector(t *testing.T) { + c := newFakeClient() + c.prepare("api/v2/monitor/system/sdn-connector/status", "testdata/system-sdn-connector.jsonnet") + r := prometheus.NewPedanticRegistry() + if !testProbe(probeSystemSDNConnector, c, r) { + t.Errorf("probeSystemSDNConnector() returned non-success") + } + + em := ` + # HELP fortigate_system_sdn_connector_status Status of SDN connectors (0=Disabled, 1=Down, 2=Unknown, 3=Up, 4=Updating) + # TYPE fortigate_system_sdn_connector_status gauge + fortigate_system_sdn_connector_status{name="AWS Infra",type="aws",vdom="root"} 3 + fortigate_system_sdn_connector_status{name="GCP Infra",type="gcp",vdom="google"} 1 + # HELP fortigate_system_sdn_connector_last_update_seconds Last update time for SDN connectors (in seconds from epoch) + # TYPE fortigate_system_sdn_connector_last_update_seconds gauge + fortigate_system_sdn_connector_last_update_seconds{name="AWS Infra",type="aws",vdom="root"} 1680708575 + fortigate_system_sdn_connector_last_update_seconds{name="GCP Infra",type="gcp",vdom="google"} 1680708001 + ` + + if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil { + t.Fatalf("metric compare: err %v", err) + } +} diff --git a/pkg/probe/testdata/system-sdn-connector.jsonnet b/pkg/probe/testdata/system-sdn-connector.jsonnet new file mode 100644 index 0000000..ac64a05 --- /dev/null +++ b/pkg/probe/testdata/system-sdn-connector.jsonnet @@ -0,0 +1,43 @@ +# api/v2/system/sdn-connector/status?vdom=* +[ + { + "http_method":"GET", + "results":[ + { + "name":"AWS Infra", + "type":"aws", + "status":"Up", + "updating":false, + "last_update":1680708575 + } + ], + "vdom":"root", + "path":"system", + "name":"sdn-connector", + "action":"status", + "status":"success", + "serial":"FGABCDEF12345678", + "version":"v7.0.9", + "build":444 + }, + { + "http_method":"GET", + "results":[ + { + "name":"GCP Infra", + "type":"gcp", + "status":"Down", + "updating":false, + "last_update":1680708001 + } + ], + "vdom":"google", + "path":"system", + "name":"sdn-connector", + "action":"status", + "status":"success", + "serial":"FGABCDEF12345678", + "version":"v7.0.9", + "build":444 + } +]