-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain.go
68 lines (60 loc) · 1.82 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
flags "github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var opts struct {
Listen string `short:"l" long:"listen" description:"Listen address" value-name:"[HOST]:PORT" default:":9605"`
Period uint `short:"p" long:"period" description:"Period in seconds, should match Prometheus scrape interval" value-name:"SECS" default:"60"`
Fping string `short:"f" long:"fping" description:"Fping binary path" value-name:"PATH" default:"/usr/bin/fping"`
Count uint `short:"c" long:"count" description:"Number of pings to send at each period" value-name:"N" default:"20"`
Version bool `long:"version" description:"Show version"`
}
var (
buildVersion = "dev"
buildCommit = "none"
buildDate = "unknown"
)
func probeHandler(w http.ResponseWriter, r *http.Request) {
targetParam := r.URL.Query().Get("target")
if targetParam == "" {
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(`<html>
<head><title>Fping Exporter</title></head>
<body>
<b>ERROR: missing target parameter</b>
</body>`))
return
}
target := GetTarget(
WorkerSpec{
period: time.Second * time.Duration(opts.Period),
},
TargetSpec{
host: targetParam,
},
)
h := promhttp.HandlerFor(target.registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func main() {
if _, err := flags.Parse(&opts); err != nil {
os.Exit(0)
}
if opts.Version {
fmt.Printf("fping-exporter %v (commit %v, built %v)\n", buildVersion, buildCommit, buildDate)
os.Exit(0)
}
if _, err := os.Stat(opts.Fping); os.IsNotExist(err) {
fmt.Printf("could not find fping at %q\n", opts.Fping)
os.Exit(1)
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", probeHandler)
log.Fatal(http.ListenAndServe(opts.Listen, nil))
}