forked from adhocteam/script_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_exporter.go
160 lines (123 loc) · 3.69 KB
/
script_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"os"
"os/exec"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
var (
showVersion = flag.Bool("version", false, "Print version information.")
configFile = flag.String("config.file", "script-exporter.yml", "Script exporter configuration file.")
listenAddress = flag.String("web.listen-address", ":9172", "The address to listen on for HTTP requests.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
shell = flag.String("config.shell", "/bin/sh", "Shell to execute script")
histogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "script_duration_seconds",
Help: "Duration for configured scripts with zero exit status",
}, []string{"script"})
failureHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "script_failure_duration_seconds",
Help: "Duration for configured scripts with non-zero exit status",
}, []string{"script"})
)
type Config struct {
Scripts []*Script `yaml:"scripts"`
}
type Script struct {
Name string `yaml:"name"`
Content string `yaml:"script"`
Timeout int64 `yaml:"timeout"`
}
func runScript(script *Script) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(script.Timeout)*time.Second)
defer cancel()
bashCmd := exec.CommandContext(ctx, *shell)
bashOut, err := bashCmd.Output()
bashIn, err := bashCmd.StdinPipe()
if err != nil {
return err
}
if err = bashCmd.Start(); err != nil {
return err
}
if _, err = bashIn.Write([]byte(script.Content)); err != nil {
return err
}
bashIn.Close()
return bashCmd.Wait()
}
func runScripts(config *Config) {
ch := make(chan bool)
for _, script := range config.Scripts {
go func(script *Script) {
start := time.Now()
err := runScript(script)
duration := time.Since(start).Seconds()
if err == nil {
log.Debugf("OK: %s (after %fs).", script.Name, duration)
} else {
log.Errorf("ERROR: %s: %s (failed after %fs).", script.Name, err, duration)
failureHistogram.WithLabelValues(script.Name).Observe(duration)
}
histogram.WithLabelValues(script.Name).Observe(duration)
ch <- true
}(script)
}
for i := 0; i < len(config.Scripts); i++ {
<-ch
}
}
func init() {
prometheus.MustRegister(version.NewCollector("script_exporter"))
prometheus.MustRegister(histogram)
prometheus.MustRegister(failureHistogram)
}
func main() {
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("script_exporter"))
os.Exit(0)
}
log.Infoln("Starting script_exporter", version.Info())
yamlFile, err := ioutil.ReadFile(*configFile)
if err != nil {
log.Fatalf("Error reading config file: %s", err)
}
config := Config{}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalf("Error parsing config file: %s", err)
}
log.Infof("Loaded %d script configurations", len(config.Scripts))
for _, script := range config.Scripts {
if script.Timeout == 0 {
script.Timeout = 15
}
}
promHandler := prometheus.Handler()
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
runScripts(&config)
promHandler.ServeHTTP(w, r)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Script Exporter</title></head>
<body>
<h1>Script Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Listening on", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatalf("Error starting HTTP server: %s", err)
}
}