-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (102 loc) · 3.29 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
package main
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"os"
"strings"
"time"
)
var (
// version set by ldflags
Version string
app = kingpin.New("zookeeper_exporter", "A zookeeper metrics exporter for prometheus, with zk_version and leaderServes=no support, with optional consul registration baked in.")
bindHostPort = app.Flag(
"web.listen-address",
"Address on which to expose metrics",
).Default("127.0.0.1:9898").String()
zkHostString = app.Flag(
"zk.hosts",
"list of ip:port of ZK hosts, comma separated",
).Required().String()
pollInterval = app.Flag(
"zk.poll-interval",
"How often to poll the ZK servers",
).Default("30").Int()
zkTimeout = app.Flag(
"zk.connect-timeout",
"Timeout value for opening socket to ZK (s)",
).Default("4").Int()
zkRWDeadLine = app.Flag(
"zk.connect-deadline",
"Connection deadline for read & write operations (s)",
).Default("3").Float()
metricsNamespace = app.Flag(
"metrics.namespace",
"string to prepend to all metric names",
).Default("zookeeper__").String()
consulName = app.Flag(
"consul.service-name",
"If defined, register zookeeper_exporter with local consul agent",
).Default("").String()
consulTags = app.Flag(
"consul.service-tags",
"Comma separated list of tags for consul service",
).Default("scrapeme").String()
consulTTL = app.Flag(
"consul.service-ttl",
"consul service TTL - consul will mark service unhealthy if zookeeper_exporter is down for this long (s). Consul will also unregister the service entirely after this service has been unhealthy for this long * 10",
).Default("60").Int()
log = logrus.New()
)
func setup() {
log.SetOutput(os.Stdout)
log.SetLevel(logrus.DebugLevel)
app.Version(Version)
app.HelpFlag.Short('h')
if _, err := app.Parse(os.Args[1:]); err != nil {
log.Fatal("Couldn't parse command line args")
}
// Register w/ consul if *consulName defined on cmd line
if *consulName != "" {
if err := registerWithConsulAgent(*consulName, *consulTags, *bindHostPort, *consulTTL); err != nil {
log.Fatalf("failed to register with consul: %s", err)
}
}
}
func main() {
setup()
zkHosts := strings.Split(*zkHostString, ",")
if *zkHostString == "" || len(zkHosts) < 1 {
log.Fatal("Need to define zookeeper servers to monitor")
}
log.Printf("Starting zookeeper_exporter v%v", Version)
log.Printf("Listening on http://%v", *bindHostPort)
log.Printf("Polling %v zookeeper servers every %ds", len(zkHosts), *pollInterval)
log.Debugf("ZK Servers: %q", zkHosts)
// convert int to time.Duration
intervalDuration := time.Duration(*pollInterval) * time.Second
// Create new metrics interface
metrics := newMetrics()
// Start one poller per server
for _, ipport := range zkHosts {
if !strings.Contains(ipport, ":") {
log.Fatalf("zookeeper host \"%s\" is not ip:port format", ipport)
}
p := newPoller(intervalDuration, *metrics, *newZKServer(ipport))
go p.pollForMetrics()
}
// Start http handler & server
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Addr: *bindHostPort,
Handler: mux,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}