Skip to content

Commit

Permalink
feat(probe): status of SDN Connectors (#210)
Browse files Browse the repository at this point in the history
* Adding functionality for querying the status of SDN Connectors
  • Loading branch information
neilschelly authored Jul 19, 2023
1 parent 22a6ee8 commit 62da407
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
1 change: 1 addition & 0 deletions pkg/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
62 changes: 62 additions & 0 deletions pkg/probe/system_sdn_connector.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions pkg/probe/system_sdn_connector_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
43 changes: 43 additions & 0 deletions pkg/probe/testdata/system-sdn-connector.jsonnet
Original file line number Diff line number Diff line change
@@ -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
}
]

0 comments on commit 62da407

Please sign in to comment.