crowdsec: update exporter

This commit is contained in:
2026-05-17 12:28:10 +04:00
parent 73829dd5a4
commit 4ebbe30b4a
+20 -11
View File
@@ -71,7 +71,12 @@ func getenv(k, def string) string {
} }
func fetchAndUpdate(client *http.Client, lapiURL, apiKey string) { func fetchAndUpdate(client *http.Client, lapiURL, apiKey string) {
req, _ := http.NewRequest("GET", lapiURL+"/v1/decisions", nil) req, err := http.NewRequest("GET", lapiURL+"/v1/decisions", nil)
if err != nil {
log.Printf("[error] build request failed: %v", err)
fetchErrors.Set(1)
return
}
req.Header.Set("X-Api-Key", apiKey) req.Header.Set("X-Api-Key", apiKey)
req.Header.Set("User-Agent", "crowdsec-exporter/1.0") req.Header.Set("User-Agent", "crowdsec-exporter/1.0")
@@ -100,6 +105,11 @@ func fetchAndUpdate(client *http.Client, lapiURL, apiKey string) {
counts := map[key]int{} counts := map[key]int{}
ips := map[string]map[string]struct{}{} ips := map[string]map[string]struct{}{}
allIPs := map[string]struct{}{} allIPs := map[string]struct{}{}
type localDecision struct {
origin, ip, scenario, dtype string
remaining float64
}
var locals []localDecision
for _, d := range data { for _, d := range data {
origin := d.Origin origin := d.Origin
@@ -122,6 +132,13 @@ func fetchAndUpdate(client *http.Client, lapiURL, apiKey string) {
ips[origin][d.Value] = struct{}{} ips[origin][d.Value] = struct{}{}
allIPs[d.Value] = struct{}{} allIPs[d.Value] = struct{}{}
} }
if origin == "crowdsec" {
dur, perr := time.ParseDuration(d.Duration)
if perr != nil {
dur = 0
}
locals = append(locals, localDecision{origin, d.Value, scenario, dtype, dur.Seconds()})
}
} }
decisionsActive.Reset() decisionsActive.Reset()
@@ -134,16 +151,8 @@ func fetchAndUpdate(client *http.Client, lapiURL, apiKey string) {
decisionsUniqueIPs.WithLabelValues(origin).Set(float64(len(s))) decisionsUniqueIPs.WithLabelValues(origin).Set(float64(len(s)))
} }
decisionsUniqueIPsTotal.Set(float64(len(allIPs))) decisionsUniqueIPsTotal.Set(float64(len(allIPs)))
for _, l := range locals {
for _, d := range data { decisionRemaining.WithLabelValues(l.origin, l.ip, l.scenario, l.dtype).Set(l.remaining)
if d.Origin != "crowdsec" {
continue
}
dur, err := time.ParseDuration(d.Duration)
if err != nil {
dur = 0
}
decisionRemaining.WithLabelValues(d.Origin, d.Value, d.Scenario, d.Type).Set(dur.Seconds())
} }
fetchErrors.Set(0) fetchErrors.Set(0)