forked from prometheus/blackbox_exporter
/
main.go
185 lines (159 loc) · 4.73 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
"gopkg.in/yaml.v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
var (
addr = flag.String("web.listen-address", ":9115", "The address to listen on for HTTP requests.")
configFile = flag.String("config.file", "blackbox.yml", "Blackbox exporter configuration file.")
)
var (
probeLatencies = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "probe_latency_summary_millis",
Help: "Latency of probes by type",
},
[]string{"module", "success"},
)
probeHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "probe_latency_histogram_millis",
Help: "Latency of probes by type",
Buckets: prometheus.ExponentialBuckets(1, 2, 20),
},
[]string{"module", "success"},
)
probeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "probe_count",
Help: "Latency of probes by type",
},
[]string{"module", "success"},
)
)
func init() {
prometheus.MustRegister(probeLatencies)
prometheus.MustRegister(probeHistogram)
prometheus.MustRegister(probeCounter)
}
type Config struct {
Modules map[string]Module `yaml:"modules"`
}
type Module struct {
Prober string `yaml:"prober"`
Timeout time.Duration `yaml:"timeout"`
HTTP HTTPProbe `yaml:"http"`
TCP TCPProbe `yaml:"tcp"`
ICMP ICMPProbe `yaml:"icmp"`
}
type HTTPProbe struct {
// Defaults to 2xx.
ValidStatusCodes []int `yaml:"valid_status_codes"`
NoFollowRedirects bool `yaml:"no_follow_redirects"`
FailIfSSL bool `yaml:"fail_if_ssl"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl"`
Method string `yaml:"method"`
FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp"`
FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"`
Path string `yaml:"path"`
}
type QueryResponse struct {
Expect string `yaml:"expect"`
Send string `yaml:"send"`
}
type TCPProbe struct {
QueryResponse []QueryResponse `yaml:"query_response"`
}
type ICMPProbe struct {
}
type Metric struct {
Name string
FloatValue float64
}
var Probers = map[string]func(string, Module, chan<- Metric) bool{
"http": probeHTTP,
"tcp": probeTCP,
"icmp": probeICMP,
}
func probeHandler(w http.ResponseWriter, r *http.Request, config *Config) {
params := r.URL.Query()
target := params.Get("target")
moduleName := params.Get("module")
if target == "" {
http.Error(w, "Target parameter is missing", 400)
return
}
if moduleName == "" {
moduleName = "http2xx"
}
module, ok := config.Modules[moduleName]
if !ok {
http.Error(w, fmt.Sprintf("Unkown module %s", moduleName), 400)
return
}
prober, ok := Probers[module.Prober]
if !ok {
http.Error(w, fmt.Sprintf("Unkown prober %s", module.Prober), 400)
return
}
// Warning: magic number here. This must be big enough to collect all the metrics.
metrics := make(chan Metric, 30)
start := time.Now()
success := prober(target, module, metrics)
latency := float64(time.Now().Sub(start).Nanoseconds()) / 1e6
metrics <- Metric{"probe_duration_seconds", latency / 1e3}
var successString string
if success {
metrics <- Metric{"probe_success", 1}
successString = "true"
} else {
metrics <- Metric{"probe_success", 0}
successString = "false"
}
// Close the metric buffer and dump it.
close(metrics)
for metric := range metrics {
fmt.Fprintf(w, "%s %f\n", metric.Name, metric.FloatValue)
}
probeLatencies.WithLabelValues(moduleName, successString).Observe(latency)
probeHistogram.WithLabelValues(moduleName, successString).Observe(latency)
probeCounter.WithLabelValues(moduleName, successString).Inc()
}
func main() {
flag.Parse()
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("Configuration loaded from: %s", *configFile)
http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/probe",
func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, &config)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Blackbox Exporter</title></head>
<body>
<h1>Blackbox Exporter</h1>
<p><a href="/probe?target=prometheus.io&module=http_2xx">Probe prometheus.io for http_2xx</a></p>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
log.Infof("Listening for connections on %s", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatalf("Error starting HTTP server: %s", err)
}
}