Browse Source

add a prometheus_uri option for cscli's config (#625)

* add a prometheus_uri option for cscli's config, and update documentation

* specify min version
Thibault "bui" Koechlin 4 years ago
parent
commit
7d93302e05

+ 5 - 6
cmd/crowdsec-cli/metrics.go

@@ -382,17 +382,16 @@ func NewMetricsCmd() *cobra.Command {
 				os.Exit(1)
 			}
 
-			if csConfig.Prometheus.ListenAddr == "" || csConfig.Prometheus.ListenPort == 0 {
-				log.Warningf("No prometheus address or port specified in '%s', can't show metrics", *csConfig.Self)
-				os.Exit(1)
+			if prometheusURL == "" {
+				prometheusURL = csConfig.Cscli.PrometheusUrl
 			}
 
 			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)
+				log.Errorf("No prometheus url, please specify in %s or via -u", *csConfig.Self)
+				os.Exit(1)
 			}
 
-			ShowPrometheus(prometheusURL)
+			ShowPrometheus(prometheusURL + "/metrics")
 		},
 	}
 	cmdMetrics.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")

+ 6 - 0
docs/v1.X/docs/references/crowdsec-config.md

@@ -277,6 +277,7 @@ This section is only used by `cscli`.
 cscli:
   output: (human|json|raw)
   hub_branch: <hub_branch>
+  prometheus_uri: <uri>
 ```
 
 #### `output`
@@ -289,6 +290,11 @@ The default output format (human, json or raw).
 
 The git branch on which `cscli` is going to fetch configurations.
 
+#### `prometheus_uri`
+> uri
+
+(>1.0.7) An uri (without the trailing `/metrics`) that will be used by `cscli metrics` command, ie. `http://127.0.0.1:6060/`
+
 ## `db_config`
 
 Please refer to the [database configuration](/Crowdsec/v1/references/database).

+ 7 - 0
pkg/csconfig/config.go

@@ -113,6 +113,13 @@ func (c *GlobalConfig) LoadConfiguration() error {
 		c.Cscli.DataDir = c.ConfigPaths.DataDir
 		c.Cscli.HubDir = c.ConfigPaths.HubDir
 		c.Cscli.HubIndexFile = c.ConfigPaths.HubIndexFile
+		if c.Cscli.PrometheusUrl == "" {
+			port := 6060
+			if c.Prometheus.ListenPort != 0 {
+				port = c.Prometheus.ListenPort
+			}
+			c.Cscli.PrometheusUrl = fmt.Sprintf("http://127.0.0.1:%d/", port)
+		}
 	}
 
 	if c.API.Client != nil && c.API.Client.CredentialsFilePath != "" && !c.DisableAgent {

+ 1 - 0
pkg/csconfig/cscli.go

@@ -11,4 +11,5 @@ type CscliCfg struct {
 	ConfigDir          string            `yaml:"-"`
 	HubIndexFile       string            `yaml:"-"`
 	SimulationFilePath string            `yaml:"-"`
+	PrometheusUrl      string            `yaml:"prometheus_uri"`
 }