forked from carlpett/zookeeper_exporter
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
82 lines (69 loc) · 2.21 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
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
)
func init() {
flag.Parse()
parsedLevel, err := log.ParseLevel(*rawLevel)
if err != nil {
log.Fatal(err)
}
logLevel = parsedLevel
if flag.Args() != nil {
for index, value := range flag.Args() {
log.Printf("flag.Args[%d]=%s", index, value)
}
if len(flag.Args()) > 1 {
//获取指定的上报端口
*bindAddr = flag.Args()[len(flag.Args())-1]
//默认第一个参数是zk信息
*zookeeperAddr = flag.Args()[0]
}
}
prometheus.MustRegister(version.NewCollector("zookeeper_exporter"))
}
var (
logLevel log.Level = log.InfoLevel
bindAddr = flag.String("bind-addr", ":9141", "bind address for the metrics server")
metricsPath = flag.String("metrics-path", "/metrics", "path to metrics endpoint")
zookeeperAddr = flag.String("zookeeper", "localhost:2181", "host:port for zookeeper socket")
rawLevel = flag.String("log-level", "info", "log level")
resetOnScrape = flag.Bool("reset-on-scrape", true, "should a reset command be sent to zookeeper on each scrape")
showVersion = flag.Bool("version", false, "show version and exit")
)
func main() {
log.Info(version.Print("zookeeper_exporter"))
log.SetLevel(logLevel)
if *showVersion {
return
}
log.Info("Starting zookeeper_exporter")
go serveMetrics()
exitChannel := make(chan os.Signal)
signal.Notify(exitChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
exitSignal := <-exitChannel
log.WithFields(log.Fields{"signal": exitSignal}).Infof("Caught %s signal, exiting", exitSignal)
}
func serveMetrics() {
log.Infof("Starting metric http endpoint on %s", *bindAddr)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(*bindAddr, nil))
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`<html>
<head><title>Zookeeper Exporter</title></head>
<body>
<h1>Zookeeper Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
}