Skip to content

Commit

Permalink
Fixed issues with alerts collector and with unreachable/wrong endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
genegr committed Nov 22, 2022
1 parent c4f1d64 commit 89f6d8c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 5 additions & 1 deletion cmd/fb-om-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions internal/openmetrics-exporter/alerts_collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package collectors

import (
"fmt"
"strings"

"github.com/prometheus/client_golang/prometheus"
"purestorage/fb-openmetrics-exporter/internal/rest-client"
)
Expand All @@ -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],
)
}
}
Expand Down
28 changes: 24 additions & 4 deletions internal/rest-client/flashblade_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
// "log"
"errors"
"crypto/tls"
"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -61,23 +64,33 @@ 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" {
fb.ApiVersion = result.Versions[len(result.Versions)-1]
} 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)
Expand All @@ -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
Expand Down

0 comments on commit 89f6d8c

Please sign in to comment.