-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
352 lines (288 loc) · 8.63 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"flag"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/jamiealquiza/envy"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/samuel/go-zookeeper/zk"
"github.com/sirupsen/logrus"
)
type brokerStorageFreeValue struct {
StorageFree float64
}
type brokerStorageFree map[string]brokerStorageFreeValue
type partitionSizeValue struct {
Size float64
}
type partitionSize map[string]partitionSizeValue
type topicPartitionSize map[string]partitionSize
var (
log *logrus.Logger
flPrometheusURL string
flPrometheusQueryTimeout time.Duration
flZkAddr string
flPartitionSizeQuery string
flBrokerStorageQuery string
flBrokerIDLabel string
flBrokerIDMapStr string
flBrokerIDMap map[string]string
flDryRun bool
flCompress bool
zkChroot string
apiClient api.Client
)
func init() {
// Setup logging
log = logrus.New()
log.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02T15:04:05.000Z",
})
log.SetOutput(os.Stdout)
flag.StringVar(&flPrometheusURL, "prometheus-url", "", "Prometheus URL")
flag.DurationVar(&flPrometheusQueryTimeout, "prometheus-query-timeout", 30*time.Second, "Timeout for Prometheus queries")
flag.StringVar(&flZkAddr, "zk-addr", "localhost:2181", "Zookeeper host, optional zkchroot after port. Eg. \"localhost:2181/my-chroot\"")
flag.StringVar(&flPartitionSizeQuery, "partition-size-query", "", "Prometheus query to get partition size by topic")
flag.StringVar(&flBrokerStorageQuery, "broker-storage-query", "", "Prometheus query to get broker storage free space")
flag.StringVar(&flBrokerIDLabel, "broker-id-label", "broker_id", "Prometheus label for broker ID")
flag.StringVar(&flBrokerIDMapStr, "broker-id-map", "", "Map value to broker ID. Eg.\"10.25.76.1=1004,10.53.32.1=1005\"")
flag.BoolVar(&flDryRun, "dry-run", false, "Fetch the metrics but don't write them to ZooKeeper, instead print them")
flag.BoolVar(&flCompress, "compress", false, "Compress the broker/partition metrics when writing them to ZooKeepeer")
envy.Parse("KAFKA_KIT_METRICSFETCHER")
flag.Parse()
flBrokerIDMap = make(map[string]string)
parseBrokerIdMap()
}
func promQuery(q string) (model.Value, error) {
ctx, cancel := context.WithTimeout(context.Background(), flPrometheusQueryTimeout)
defer cancel()
v1api := v1.NewAPI(apiClient)
result, warnings, err := v1api.Query(ctx, q, time.Now())
if err != nil {
return nil, err
}
if len(warnings) > 0 {
log.Warning(warnings)
}
return result, nil
}
func getBrokerFreeSpace() *brokerStorageFree {
log.Info("Getting broker storage stats from Prometheus")
m := make(brokerStorageFree)
result, err := promQuery(flBrokerStorageQuery)
if err != nil {
log.Fatalf("Error getting broker storage free space from Prometheus: %v", err)
}
if result.Type() == model.ValVector {
vectorVal := result.(model.Vector)
if flBrokerIDMap != nil {
log.Infof("Broker ID Map: %v", flBrokerIDMap)
if len(vectorVal) != len(flBrokerIDMap) {
log.Warn("Returned metrics not equal to broker ID override map")
}
for _, elem := range vectorVal {
bid := string(elem.Metric[model.LabelName(flBrokerIDLabel)])
if k, exists := flBrokerIDMap[bid]; exists {
bid = k
}
m[bid] = brokerStorageFreeValue{StorageFree: float64(elem.Value)}
}
} else {
for _, elem := range vectorVal {
bid := string(elem.Metric[model.LabelName(flBrokerIDLabel)])
m[bid] = brokerStorageFreeValue{StorageFree: float64(elem.Value)}
}
}
}
return &m
}
func getPartitionSizes() *topicPartitionSize {
log.Info("Getting partition sizes from Prometheus")
m := make(topicPartitionSize)
result, err := promQuery(flPartitionSizeQuery)
if err != nil {
log.Errorf("Error getting partition sizes from Prometheus: %v", err)
os.Exit(1)
}
if result.Type() == model.ValVector {
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
topic := string(elem.Metric["topic"])
partition := string(elem.Metric["partition"])
v, ok := m[topic]
if !ok {
v = make(partitionSize)
}
v[partition] = partitionSizeValue{Size: float64(elem.Value)}
m[topic] = v
}
}
return &m
}
func processData(zkConn *zk.Conn, brokerMetrics *brokerStorageFree, partitionMapping *topicPartitionSize) error {
defer zkConn.Close()
topicPartitionSizeData, err := json.Marshal(*partitionMapping)
if err != nil {
return err
}
brokerMetricsData, err := json.Marshal(*brokerMetrics)
if err != nil {
return err
}
if flDryRun {
// In dry-run don't do anything but display the information we retrieved and computed.
log.Println("partition mapping")
for topic, m := range *partitionMapping {
log.Printf("topic: %s", topic)
type el struct {
Partition int
Size uint64
}
var entries []el
for partition, obj := range m {
p, _ := strconv.Atoi(partition)
entries = append(entries, el{
Partition: p,
Size: uint64(obj.Size),
})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].Partition < entries[j].Partition
})
for _, entry := range entries {
log.Printf("partition %-4d size: %s", entry.Partition, humanize.Bytes(entry.Size))
}
}
log.Println("fetched metrics")
for brokerID, obj := range *brokerMetrics {
log.Printf("broker #%-4s %15s: %s", brokerID, "storage free", humanize.Bytes(uint64(obj.StorageFree)))
}
} else {
if err := writeToZookeeper(zkConn, "partitionmeta", topicPartitionSizeData); err != nil {
return err
}
if err := writeToZookeeper(zkConn, "brokermetrics", brokerMetricsData); err != nil {
return err
}
}
return nil
}
func writeToZookeeper(zkConn *zk.Conn, path string, data []byte) error {
const root = "/topicmappr"
// If our cluster is a zk chroot we need to use it too.
var dir string
if zkChroot != "" {
dir = zkChroot + root
} else {
dir = root
}
path = dir + "/" + path
// Remove the old node.
err := zkConn.Delete(path, 0)
if err != nil && err != zk.ErrNoNode {
return fmt.Errorf("unable to delete path %s. err: %v", path, err)
}
acl, _, err := zkConn.GetACL(dir)
if err != nil {
if err == zk.ErrNoNode {
// Create the directory node if it is missing
_, err = zkConn.Create(dir, nil, 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
return fmt.Errorf("unable to create node %s. err: %v", dir, err)
}
} else {
return fmt.Errorf("unable to get node %s acl. err: %v", dir, err)
}
} else {
// Ensure that we have WorldACL with PermAll
var waclExists bool
wacl := zk.WorldACL(zk.PermAll)[0]
for _, a := range acl {
if a == wacl {
waclExists = true
break
}
}
if !waclExists {
return fmt.Errorf("zookeeper node %s has wrong ACL: %v", dir, acl)
}
}
if flCompress {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, err = zw.Write(data)
if err != nil {
return fmt.Errorf("unable to compress data")
}
zw.Close()
data = buf.Bytes()
}
// Create the data node
log.Printf("writing data to %s", path)
_, err = zkConn.Create(path, data, 0, zk.WorldACL(zk.PermAll))
if err != nil {
return fmt.Errorf("unable to create path %s. err: %v", path, err)
}
return nil
}
func parseBrokerIdMap() {
var (
brokerName string
brokerId string
)
for _, chunk := range strings.Split(flBrokerIDMapStr, ",") {
brokerIdMappingChunks := strings.Split(chunk, "=")
brokerName = brokerIdMappingChunks[0]
brokerId = brokerIdMappingChunks[1]
flBrokerIDMap[brokerName] = brokerId
}
}
func main() {
// Prometheus client
if flPrometheusURL == "" {
log.Fatal("Please provide prometheus-url")
}
var err error
apiClient, err = api.NewClient(api.Config{
Address: flPrometheusURL,
})
if err != nil {
log.Fatalf("Error creating Prometheus client: %v", err)
}
// Zookeeper connection
if flZkAddr == "" {
log.Fatal("please provide the zookeeper host with --zk-addr")
}
var zkAddr string
if pos := strings.IndexByte(flZkAddr, '/'); pos >= 0 {
zkAddr = flZkAddr[:pos]
zkChroot = flZkAddr[pos:]
} else {
zkAddr = flZkAddr
}
zk.DefaultLogger = log.WithField("logger", "zk")
zkConn, _, err := zk.Connect([]string{zkAddr}, 20*time.Second)
if err != nil {
log.Fatalf("Error creating zookeeper connection: %v", err)
}
// Get data
brokerMetrics := getBrokerFreeSpace()
partitionMapping := getPartitionSizes()
err = processData(zkConn, brokerMetrics, partitionMapping)
if err != nil {
log.Fatalf("Failed to process data: %v", err)
}
}