Skip to content

Commit ebeaf94

Browse files
authored
Define Additional Scripts via Separate Config Files (#116)
It is now possible to define additional scripts for the exporter via separate config files. For that a new option `scripts_configs` was added, which can be used to set a list of files, which are containing additional script configuration. The configuration must look as follows: ``` script_configs: - /path/to/file.yaml ``` The content of the additional scripts configuration file, can only contain a list of scripts, e.g. ```yaml - name: test command: ./examples/test.sh ```
1 parent 44ddcaf commit ebeaf94

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ scripts:
104104
prefix: <string>
105105
scrape_interval: <duration>
106106
scrape_timeout: <duration>
107+
108+
scripts_configs:
109+
- <string>
107110
```
108111
109112
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 (
146149

147150
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.
148151

149-
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.
152+
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.
150153
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`).
151154

152155
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.

examples/config.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ discovery:
1919
# path: /prefix-path
2020

2121
scripts:
22-
- name: test
23-
command: ./examples/test.sh
2422
- name: ping
2523
command: ./examples/ping.sh
2624
cacheDuration: 1m
@@ -45,3 +43,6 @@ scripts:
4543
args:
4644
- test1
4745
- test2
46+
47+
scripts_configs:
48+
- ./examples/scripts_*.yaml

examples/scripts_test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- name: test
2+
command: ./examples/test.sh

pkg/config/config.go

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"strings"
89
"time"
910

@@ -47,6 +48,8 @@ type Config struct {
4748

4849
Scripts []ScriptConfig `yaml:"scripts"`
4950

51+
ScriptsConfigs []string `yaml:"scripts_configs"`
52+
5053
Discovery struct {
5154
Host string `yaml:"host"`
5255
Port string `yaml:"port"`
@@ -82,6 +85,32 @@ func (c *Config) LoadConfig(file string) error {
8285
return err
8386
}
8487

88+
// Additional scripts can also be defined via the `scripts_configs` field. This field can contain a list of glob
89+
// patterns that will be expanded to a list of files. Each file contains a list of additional script configurations.
90+
for _, scriptsConfig := range c.ScriptsConfigs {
91+
files, err := filepath.Glob(scriptsConfig)
92+
if err != nil {
93+
return err
94+
}
95+
96+
for _, file := range files {
97+
data, err := os.ReadFile(file)
98+
if err != nil {
99+
return err
100+
}
101+
102+
data = []byte(expandEnv(string(data)))
103+
104+
var additionalScriptConfigs []ScriptConfig
105+
err = yaml.Unmarshal(data, &additionalScriptConfigs)
106+
if err != nil {
107+
return err
108+
}
109+
110+
c.Scripts = append(c.Scripts, additionalScriptConfigs...)
111+
}
112+
}
113+
85114
return nil
86115
}
87116

0 commit comments

Comments
 (0)