forked from discordianfish/blackbox_prober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (101 loc) · 2.88 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
package main
import (
"errors"
"flag"
"log"
"net/http"
"net/url"
"os"
"./pingers"
"github.com/prometheus/client_golang/prometheus"
)
var (
// set at build time
Version = "0.0.0.dev"
listenAddress = flag.String("web.listen-address", ":9110", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
errURLNotAbsolute = errors.New("URL not absolute")
errNoPinger = errors.New("No pinger for schema")
)
type pingCollector struct {
targets targets
metrics pingers.Metrics
}
// NewPingCollector returns a new pingCollector
func NewPingCollector(targets targets) *pingCollector {
return &pingCollector{
targets: targets,
metrics: pingers.Metrics{
Up: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: pingers.Namespace,
Name: "up",
Help: "1 if url is reachable, 0 if not",
}, []string{"url"}),
Latency: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: pingers.Namespace,
Name: "latency_seconds",
Help: "Latency of request for url",
}, []string{"url"}),
Size: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: pingers.Namespace,
Name: "size_bytes",
Help: "Size of request for url",
}, []string{"url"}),
Code: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: pingers.Namespace,
Name: "response_code",
Help: "Response code for url",
}, []string{"url"}),
},
}
}
// Collect implements prometheus.Collector.
func (c pingCollector) Collect(ch chan<- prometheus.Metric) {
for _, target := range c.targets {
log.Printf("collect %s", target)
if err := pingers.Ping(target, c.metrics); err != nil {
panic(err)
}
}
c.metrics.Up.Collect(ch)
c.metrics.Latency.Collect(ch)
c.metrics.Size.Collect(ch)
c.metrics.Code.Collect(ch)
}
// Describe implements prometheus.Collector.
func (c pingCollector) Describe(ch chan<- *prometheus.Desc) {
c.metrics.Up.Describe(ch)
c.metrics.Latency.Describe(ch)
c.metrics.Size.Describe(ch)
c.metrics.Code.Describe(ch)
}
type targets []*url.URL
func (t *targets) String() string { return "" }
func (t *targets) Set(str string) error {
url, err := url.Parse(str)
if err != nil {
return err
}
if !url.IsAbs() {
return errURLNotAbsolute
}
if !pingers.CanHandle(url) {
return errNoPinger
}
*t = append(*t, url)
return nil
}
func main() {
targets := targets{}
flag.Var(&targets, "u", "URL to provide metrics for, can be repeated")
flag.Parse()
if len(targets) == 0 {
log.Printf("Please provide urls to ping (-u)")
flag.PrintDefaults()
os.Exit(1)
}
log.Printf("blackbox_prober v%s providing metrics for %v on %s%s", Version, targets, *listenAddress, *metricsPath)
prometheus.MustRegister(NewPingCollector(targets))
http.Handle(*metricsPath, prometheus.Handler())
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}