diff --git a/README.md b/README.md index 1e69d66..0aad105 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,9 @@ scripts: prefix: scrape_interval: scrape_timeout: + +scripts_configs: + - ``` The `name` of the script must be a valid Prometheus label value. The `command` string is the script which is executed with all arguments specified in `args`. To add dynamic arguments you can pass the `params` query parameter with a list of query parameters which values should be added as argument. The program will be executed directly, without a shell being invoked, and it is recommended that it be specified by path instead of relying on ``$PATH``. @@ -146,7 +149,7 @@ For testing purposes, the timeout can be specified directly as a URL parameter ( The `cacheDuration` config can be used to cache the results from an execution of the script for the provided time. The provided duration must be parsable by the [`time.ParseDuration`](https://pkg.go.dev/time#ParseDuration) function. If no cache duration is provided or the provided cache duration can not be parsed, the output of an script will not be cached. -You can fine tune the script discovery options via optional script `discovery`. All these options will go through prometheus configuration where you can change them via relabel mechanism. +You can fine tune the script discovery options via optional script `discovery`. All these options will go through prometheus configuration where you can change them via relabel mechanism. There are `params` to define dynamic script parameters (with reserved keys: `params`, `prefix`, `script` and `timeout`) where only value will be used during script invoking (similar to `args`), `prefix` to define prefix for all script metrics, `scrape_interval` to define how often the script scrape should run and `scrape_timeout` to define the scrape timeout for prometheus (similar to `timeout`). The global `discovery` configures the main discovery parameters. If not defined, the exporter will use `Host:` header from the request to decide how to present a `target` to prometheus. diff --git a/examples/config.yaml b/examples/config.yaml index 9a552f3..4e4c5b4 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -19,8 +19,6 @@ discovery: # path: /prefix-path scripts: - - name: test - command: ./examples/test.sh - name: ping command: ./examples/ping.sh cacheDuration: 1m @@ -45,3 +43,6 @@ scripts: args: - test1 - test2 + +scripts_configs: + - ./examples/scripts_*.yaml diff --git a/examples/scripts_test.yaml b/examples/scripts_test.yaml new file mode 100644 index 0000000..a4204b2 --- /dev/null +++ b/examples/scripts_test.yaml @@ -0,0 +1,2 @@ +- name: test + command: ./examples/test.sh diff --git a/pkg/config/config.go b/pkg/config/config.go index 208c1b4..06bc45e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "strings" "time" @@ -47,6 +48,8 @@ type Config struct { Scripts []ScriptConfig `yaml:"scripts"` + ScriptsConfigs []string `yaml:"scripts_configs"` + Discovery struct { Host string `yaml:"host"` Port string `yaml:"port"` @@ -82,6 +85,32 @@ func (c *Config) LoadConfig(file string) error { return err } + // Additional scripts can also be defined via the `scripts_configs` field. This field can contain a list of glob + // patterns that will be expanded to a list of files. Each file contains a list of additional script configurations. + for _, scriptsConfig := range c.ScriptsConfigs { + files, err := filepath.Glob(scriptsConfig) + if err != nil { + return err + } + + for _, file := range files { + data, err := os.ReadFile(file) + if err != nil { + return err + } + + data = []byte(expandEnv(string(data))) + + var additionalScriptConfigs []ScriptConfig + err = yaml.Unmarshal(data, &additionalScriptConfigs) + if err != nil { + return err + } + + c.Scripts = append(c.Scripts, additionalScriptConfigs...) + } + } + return nil }