Skip to content

Commit

Permalink
Merge pull request #6 from siebenmann/direct-run
Browse files Browse the repository at this point in the history
Support directly executing configured scripts and make this the default
  • Loading branch information
ricoberger authored Jul 4, 2019
2 parents ac0c72e + 1a7102f commit 16da63e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Usage of ./bin/script_exporter:
-config.file string
Configuration file in YAML format. (default "config.yaml")
-config.shell string
Shell to execute script (default "/bin/sh")
Set shell to execute scripts with; otherwise they are executed directly
-create-token
Create bearer token for authentication.
-version
Expand Down Expand Up @@ -87,9 +87,13 @@ scripts:
script: <string>
```
The `script` string will be split on spaces to generate the program name and any fixed arguments, then any arguments specified from the `params` parameter will be appended. If a shell has not been set, the program will be executed directly; if a shell has been set, the shell will be used to run the script, executed as `SHELL script-name [argument ...]`. If a shell is set, what it runs must be a shell script (and for that shell); it cannot be a binary executable and any `#!` line at the start is ignored.

## Prometheus configuration

The script_exporter needs to be passed the script name as a parameter (`script`). You can also pass a custom prefix (`prefix`) and additional parameters which should be passed to the script (`params`). If the `output` parameter is set to `ignore` then the script_exporter only return `script_success{}` and `script_duration_seconds{}`.
The script_exporter needs to be passed the script name as a parameter (`script`). You can also pass a custom prefix (`prefix`) which is prepended to metrics names and the names of additional parameters which should be passed to the script (`params` and then additional URL parameters). If the `output` parameter is set to `ignore` then the script_exporter only return `script_success{}` and `script_duration_seconds{}`.

The `params` parameter is a comma-separated list of additional URL query parameters that will be used to construct the additional list of arguments, in order. The value of each URL query parameter is not parsed or split; it is passed directly to the script as a single argument.

Example config:

Expand Down
10 changes: 8 additions & 2 deletions cmd/script_exporter/script_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ var (
showVersion = flag.Bool("version", false, "Show version information.")
createToken = flag.Bool("create-token", false, "Create bearer token for authentication.")
configFile = flag.String("config.file", "config.yaml", "Configuration file in YAML format.")
shell = flag.String("config.shell", "/bin/sh", "Shell to execute script")
shell = flag.String("config.shell", "", "Set shell to execute scripts with; otherwise they are executed directly")
)

func runScript(args []string) (string, error) {
output, err := exec.Command(*shell, args...).Output()
var output []byte
var err error
if *shell != "" {
output, err = exec.Command(*shell, args...).Output()
} else {
output, err = exec.Command(args[0], args[1:]...).Output()
}
if err != nil {
return "", err
}
Expand Down
1 change: 1 addition & 0 deletions examples/helloworld.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#!/bin/sh
echo "hello_world{param=\"$1\"} 1"
Empty file modified examples/ping.sh
100644 → 100755
Empty file.

0 comments on commit 16da63e

Please sign in to comment.