From 89f6d8c886a43faf4b095383c4376f376a1f344b Mon Sep 17 00:00:00 2001 From: Eugenio Grosso Date: Tue, 22 Nov 2022 15:24:46 +0000 Subject: [PATCH] Fixed issues with alerts collector and with unreachable/wrong endpoints --- cmd/fb-om-exporter/main.go | 6 +++- .../openmetrics-exporter/alerts_collector.go | 12 ++++++-- internal/rest-client/flashblade_client.go | 28 ++++++++++++++++--- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/cmd/fb-om-exporter/main.go b/cmd/fb-om-exporter/main.go index b45fa45..fb951f0 100644 --- a/cmd/fb-om-exporter/main.go +++ b/cmd/fb-om-exporter/main.go @@ -78,7 +78,11 @@ func metricsHandler(w http.ResponseWriter, r *http.Request) { registry := prometheus.NewRegistry() fbclient := client.NewRestClient(endpoint, apitoken, apiver, debug) - _ = collectors.Collector(context.TODO(), metrics, registry, fbclient) + if fbclient.Error != nil { + http.Error(w, "Error connecting to FlashBlade. Check your management endpoint and/or api token are correct.", http.StatusBadRequest) + return + } + collectors.Collector(context.TODO(), metrics, registry, fbclient) h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) h.ServeHTTP(w, r) diff --git a/internal/openmetrics-exporter/alerts_collector.go b/internal/openmetrics-exporter/alerts_collector.go index 0d52482..ad29fe1 100644 --- a/internal/openmetrics-exporter/alerts_collector.go +++ b/internal/openmetrics-exporter/alerts_collector.go @@ -1,6 +1,9 @@ package collectors import ( + "fmt" + "strings" + "github.com/prometheus/client_golang/prometheus" "purestorage/fb-openmetrics-exporter/internal/rest-client" ) @@ -19,12 +22,17 @@ func (c *AlertsCollector) Collect(ch chan<- prometheus.Metric) { if len(alerts.Items) == 0 { return } + al := make(map[string]float64) for _, alert := range alerts.Items { + al[fmt.Sprintf("%s,%s,%s", alert.Severity, alert.ComponentType, alert.ComponentName)] += 1 + } + for a, n := range al { + alert := strings.Split(a, ",") ch <- prometheus.MustNewConstMetric( c.AlertsDesc, prometheus.GaugeValue, - 1.0, - alert.Severity, alert.ComponentType, alert.ComponentName, + n, + alert[0], alert[1], alert[2], ) } } diff --git a/internal/rest-client/flashblade_client.go b/internal/rest-client/flashblade_client.go index a6c0ca3..4aa2df2 100644 --- a/internal/rest-client/flashblade_client.go +++ b/internal/rest-client/flashblade_client.go @@ -1,6 +1,8 @@ package client import ( +// "log" + "errors" "crypto/tls" "github.com/go-resty/resty/v2" ) @@ -33,6 +35,7 @@ type FBClient struct { RestClient *resty.Client ApiVersion string XAuthToken string + Error error } func NewRestClient(endpoint string, apitoken string, apiversion string, debug bool) *FBClient { @@ -61,13 +64,19 @@ func NewRestClient(endpoint string, apitoken string, apiversion string, debug bo // }) result := new(ApiVersions) - res, _ := fb.RestClient.R(). + res, err := fb.RestClient.R(). SetResult(&result). Get("/api_version") + if err != nil { + fb.Error = err + return fb + } if res.StatusCode() != 200 { + fb.Error = errors.New("Not a valid FlashBlade REST API server") return fb } if len(result.Versions) == 0 { + fb.Error = errors.New("Not a valid FlashBlade REST API version") return fb } if apiversion == "latest" { @@ -75,9 +84,13 @@ func NewRestClient(endpoint string, apitoken string, apiversion string, debug bo } else { fb.ApiVersion = apiversion } - res, _ = fb.RestClient.R(). + res, err = fb.RestClient.R(). SetHeader("api-token", apitoken). Post("/login") + if err != nil { + fb.Error = err + return fb + } fb.XAuthToken = res.Header().Get("x-auth-token") fb.RestClient.SetBaseURL("https://" + endpoint + "/api/" + fb.ApiVersion) fb.RestClient.SetHeader("x-auth-token", fb.XAuthToken) @@ -88,16 +101,23 @@ func (fb *FBClient) Close() *FBClient { if fb.XAuthToken == "" { return fb } - fb.RestClient.R(). + _, err := fb.RestClient.R(). SetHeader("x-auth-token", fb.XAuthToken). Post("/logout") + if err != nil { + fb.Error = err + } return fb } func (fb *FBClient) RefreshSession() *FBClient { - res, _ := fb.RestClient.R(). + res, err := fb.RestClient.R(). SetHeader("api-token", fb.ApiToken). Post("/login") + if err != nil { + fb.Error = err + return fb + } fb.XAuthToken = res.Header().Get("x-auth-token") fb.RestClient.SetHeader("x-auth-token", fb.XAuthToken) return fb