forked from prometheus/blackbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (87 loc) · 2.34 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
package main
import (
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"time"
"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.")
var configFile = flag.String("config.file", "blackbox.yml", "Blackbox exporter configuration file.")
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"`
}
type TCPProbe struct {
}
type ICMPProbe struct {
}
var Probers = map[string]func(string, http.ResponseWriter, Module) 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
}
start := time.Now()
success := prober(target, w, module)
fmt.Fprintf(w, "probe_duration_seconds %f\n", float64(time.Now().Sub(start))/1e9)
if success {
fmt.Fprintf(w, "probe_success %d\n", 1)
} else {
fmt.Fprintf(w, "probe_success %d\n", 0)
}
}
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)
}
http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/probe",
func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, &config)
})
http.ListenAndServe(*addr, nil)
}