Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ MAINTAINER James Kassemi (Ad Hoc, LLC) <james.kassemi@adhocteam.us>

COPY script-exporter /bin/script-exporter
COPY script-exporter.yml /etc/script-exporter/config.yml
COPY runThis.sh /etc/script-exporter/runThis.sh

EXPOSE 9172
ENTRYPOINT [ "/bin/script-exporter" ]
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ scripts:
- name: timeout
script: sleep 5
timeout: 1

- name: scriptFile
file: /etc/script-exporter/runThis.sh
```

## Running
Expand All @@ -30,10 +33,10 @@ You can run via docker with:
```
docker run -d -p 9172:9172 --name script-exporter \
-v `pwd`/config.yml:/etc/script-exporter/config.yml:ro \
-config.file=/etc/script-exporter/config.yml
-web.listen-address=":9172" \
-web.telemetry-path="/metrics" \
-config.shell="/bin/sh" \
-e config.file=/etc/script-exporter/config.yml \
-e web.listen-address=":9172" \
-e web.telemetry-path="/metrics" \
-e config.shell="/bin/sh" \
adhocteam/script-exporter:master
```

Expand Down
3 changes: 3 additions & 0 deletions runThis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "This script is run while probing for the script"
exit 0 # This will fail the script
3 changes: 3 additions & 0 deletions script-exporter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ scripts:
- name: 'timeout'
script: sleep 5
timeout: 1

- name: 'script'
file: /etc/script-exporter/runThis.sh
25 changes: 22 additions & 3 deletions script_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"bytes"
)

var (
Expand All @@ -34,6 +36,7 @@ type Script struct {
Name string `yaml:"name"`
Content string `yaml:"script"`
Timeout int64 `yaml:"timeout"`
File string `yaml:"file"`
}

type Measurement struct {
Expand All @@ -47,8 +50,15 @@ func runScript(script *Script) error {
defer cancel()

bashCmd := exec.CommandContext(ctx, *shell)
if script.File != "" {
bashCmd.Args = append(bashCmd.Args, script.File)
}

bashIn, err := bashCmd.StdinPipe()
// Stdout buffer
cmdOutput := &bytes.Buffer{}
// Attach buffer to command
bashCmd.Stdout = cmdOutput

if err != nil {
return err
Expand All @@ -64,7 +74,16 @@ func runScript(script *Script) error {

bashIn.Close()

return bashCmd.Wait()
err = bashCmd.Wait()

log.Infof("output of script: %s", cmdOutput.Bytes())

if err != nil {
return err
} else {
return nil
}

}

func runScripts(scripts []*Script) []*Measurement {
Expand All @@ -80,7 +99,7 @@ func runScripts(scripts []*Script) []*Measurement {
duration := time.Since(start).Seconds()

if err == nil {
log.Debugf("OK: %s (after %fs).", script.Name, duration)
log.Infof("OK: %s (after %fs).", script.Name, duration)
success = 1
} else {
log.Infof("ERROR: %s: %s (failed after %fs).", script.Name, err, duration)
Expand Down Expand Up @@ -182,7 +201,7 @@ func main() {
}
}

http.Handle("/metrics", prometheus.Handler())
http.Handle("/metrics", promhttp.Handler())

http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
scriptRunHandler(w, r, &config)
Expand Down
12 changes: 7 additions & 5 deletions script_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

var config = &Config{
Scripts: []*Script{
{"success", "exit 0", 1},
{"failure", "exit 1", 1},
{"timeout", "sleep 5", 2},
{"success", "exit 0", 1, ""},
{"failure", "exit 1", 1, ""},
{"timeout", "sleep 5", 2, ""},
{"script", "", 1, "./runThis.sh"},
},
}

Expand All @@ -22,6 +23,7 @@ func TestRunScripts(t *testing.T) {
"success": {1, 0},
"failure": {0, 0},
"timeout": {0, 2},
"script": {1, 0},
}

for _, measurement := range measurements {
Expand Down Expand Up @@ -77,8 +79,8 @@ func TestScriptFilter(t *testing.T) {
t.Errorf("Unexpected: %s", err.Error())
}

if len(scripts) != 3 {
t.Fatalf("Expected 3 scripts, received %d", len(scripts))
if len(scripts) != 4 {
t.Fatalf("Expected 4 scripts, received %d", len(scripts))
}

for i, script := range config.Scripts {
Expand Down