From 7144dca68a40322f9e0df312c0fc7a81c375c5aa Mon Sep 17 00:00:00 2001 From: blotus Date: Thu, 13 Oct 2022 15:49:41 +0200 Subject: [PATCH] Fix missing metrics cscli (#1809) --- cmd/crowdsec-cli/metrics_table.go | 2 +- cmd/crowdsec-cli/utils.go | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/crowdsec-cli/metrics_table.go b/cmd/crowdsec-cli/metrics_table.go index 5fe71e763..f55d89cd2 100644 --- a/cmd/crowdsec-cli/metrics_table.go +++ b/cmd/crowdsec-cli/metrics_table.go @@ -47,7 +47,6 @@ func metricsToTable(t *table.Table, stats map[string]map[string]int, keys []stri if t == nil { return 0, fmt.Errorf("nil table") } - // sort keys to keep consistent order when printing sortedKeys := []string{} for k := range stats { @@ -77,6 +76,7 @@ func metricsToTable(t *table.Table, stats map[string]map[string]int, keys []stri } } t.AddRow(row...) + numRows++ } return numRows, nil } diff --git a/cmd/crowdsec-cli/utils.go b/cmd/crowdsec-cli/utils.go index 414d2ebb1..89c20f69b 100644 --- a/cmd/crowdsec-cli/utils.go +++ b/cmd/crowdsec-cli/utils.go @@ -256,18 +256,23 @@ func InspectItem(name string, objecitemType string) { return } - if csConfig.Prometheus.Enabled { - if csConfig.Prometheus.ListenAddr == "" || csConfig.Prometheus.ListenPort == 0 { - log.Warningf("No prometheus address or port specified in '%s', can't show metrics", *csConfig.FilePath) - return + if prometheusURL == "" { + //This is technically wrong to do this, as the prometheus section contains a listen address, not an URL to query prometheus + //But for ease of use, we will use the listen address as the prometheus URL because it will be 127.0.0.1 in the default case + listenAddr := csConfig.Prometheus.ListenAddr + if listenAddr == "" { + listenAddr = "127.0.0.1" } - if prometheusURL == "" { - log.Debugf("No prometheus URL provided using: %s:%d", csConfig.Prometheus.ListenAddr, csConfig.Prometheus.ListenPort) - prometheusURL = fmt.Sprintf("http://%s:%d/metrics", csConfig.Prometheus.ListenAddr, csConfig.Prometheus.ListenPort) + listenPort := csConfig.Prometheus.ListenPort + if listenPort == 0 { + listenPort = 6060 } - fmt.Printf("\nCurrent metrics : \n") - ShowMetrics(hubItem) + prometheusURL = fmt.Sprintf("http://%s:%d/metrics", listenAddr, listenPort) + log.Debugf("No prometheus URL provided using: %s", prometheusURL) } + + fmt.Printf("\nCurrent metrics : \n") + ShowMetrics(hubItem) } func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *string) error {