-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-prober.go
399 lines (364 loc) · 10.9 KB
/
s3-prober.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var (
flagListenAddress = "listen"
envListenAddress = "LISTEN_ADDRESS"
defaultListenAddress = ":2112"
flagOpTimeout = "timeout"
envOpTimeout = "OP_TIMEOUT"
defaultOpTimeout = 10
flagAccessKey = "accesskey"
envAccessKey = "ACCESSKEY"
flagSecretKey = "secretkey"
envSecretKey = "SECRETKEY"
flagEndpoint = "endpoint"
envEndpoint = "ENDPOINT"
flagBucket = "bucket"
envBucket = "BUCKET"
flagSkipmakedeletebucket = "skipmakedeletebucket"
envSkipmakedeletebucket = "SKIPMAKEDELETEBUCKET"
flagFilename = "filename"
envFilename = "FILENAME"
s3Success = prometheus.NewDesc(
"probe_success",
"Displays whether or not the probe was a success",
[]string{"operation", "s3_endpoint"}, nil,
)
s3Duration = prometheus.NewDesc(
"probe_duration_seconds",
"Returns how long the probe took to complete in seconds",
[]string{"operation", "s3_endpoint"}, nil,
)
)
// Exporter is our exporter type
type Exporter struct {
bucket string
endpoint string
accessKey string
secretKey string
filename string
skipmakedeletebucket bool
mutex *sync.Mutex
opTimeout int
}
// Describe all the metrics we export
func (e Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- s3Success
ch <- s3Duration
}
// Collect metrics
func (e Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()
minioClient, err := minio.New(e.endpoint, &minio.Options{
Creds: credentials.NewStaticV4(e.accessKey, e.secretKey, ""),
Secure: true,
Region: "us-east-1",
Transport: &http.Transport{
ResponseHeaderTimeout: time.Second * time.Duration(e.opTimeout),
},
})
if err != nil {
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "connect", e.endpoint,
)
klog.Errorf("Could not create minioClient to endpoint %s, %v\n", e.endpoint, err)
return
}
_, object := filepath.Split(e.filename)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(e.opTimeout))
defer cancel()
// list buckets
success := 1.0
start := time.Now()
bs, err := minioClient.ListBuckets(ctx)
if err != nil {
success = 0
}
elapsed := time.Since(start)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, success, "listbuckets", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, elapsed.Seconds(), "listbuckets", e.endpoint,
)
if err != nil {
klog.Errorf("Failed to list buckets on endpoint %s, %v\n", e.endpoint, err)
if !e.skipmakedeletebucket {
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "makebucket", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "makebucket", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "removebucket", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "makebucket", e.endpoint,
)
}
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "put", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "put", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "get", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "get", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "stat", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "stat", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "remove", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "remove", e.endpoint,
)
return
}
found := false
for _, b := range bs {
if b.Name == e.bucket {
found = true
}
}
if !e.skipmakedeletebucket || !found {
err = measure(e, "makebucket", ch, func() error { return minioClient.MakeBucket(ctx, e.bucket, minio.MakeBucketOptions{}) })
if err != nil {
klog.Errorf("error during MakeBucket: %v", err)
return
}
}
err = measure(e, "put", ch, func() error {
_, err := minioClient.FPutObject(ctx, e.bucket, object, e.filename, minio.PutObjectOptions{})
if err != nil {
klog.Errorf("error during PutObject: %v", err)
}
return err
})
// only if put succeeded
if err == nil {
err = measure(e, "get", ch, func() error {
return minioClient.FGetObject(ctx, e.bucket, object, "/tmp/"+object, minio.GetObjectOptions{})
})
if err != nil {
klog.Errorf("error during GetObject: %v", err)
}
err = measure(e, "stat", ch, func() error {
_, err := minioClient.StatObject(ctx, e.bucket, object, minio.StatObjectOptions{})
return err
})
if err != nil {
klog.Errorf("error during StatObject: %v", err)
}
err = measure(e, "remove", ch, func() error {
return minioClient.RemoveObject(ctx, e.bucket, object, minio.RemoveObjectOptions{})
})
if err != nil {
klog.Errorf("error during RemoveObject: %v", err)
}
} else {
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "get", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "get", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "stat", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "stat", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, 0, "remove", e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, float64(e.opTimeout), "remove", e.endpoint,
)
}
if !e.skipmakedeletebucket {
err = measure(e, "removebucket", ch, func() error { return minioClient.RemoveBucket(ctx, e.bucket) })
if err != nil {
klog.Errorf("error during RemoveBucket: %v", err)
}
}
}
func measure(e Exporter, operation string, ch chan<- prometheus.Metric, f func() error) error {
// job = remove
success := 1.0
start := time.Now()
err := f()
if err != nil {
success = 0
}
elapsed := time.Since(start)
ch <- prometheus.MustNewConstMetric(
s3Success, prometheus.GaugeValue, success, operation, e.endpoint,
)
ch <- prometheus.MustNewConstMetric(
s3Duration, prometheus.GaugeValue, elapsed.Seconds(), operation, e.endpoint,
)
return err
}
func probeHandler(w http.ResponseWriter, r *http.Request, e Exporter) {
registry := prometheus.NewRegistry()
err := registry.Register(e)
if err != nil {
klog.Errorf("failed ot register prometheus: %v", err)
}
// Serve
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func init() {
prometheus.MustRegister(version.NewCollector("s3_prober"))
}
func startCmd() *cli.Command {
return &cli.Command{
Name: "start",
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagListenAddress,
Usage: "Optional. Specify listen address.",
EnvVars: []string{envListenAddress},
Value: defaultListenAddress,
},
&cli.IntFlag{
Name: flagOpTimeout,
Usage: "Optional. Timout in seconds after which an operation is considered as failed.",
EnvVars: []string{envOpTimeout},
Value: defaultOpTimeout,
},
&cli.StringFlag{
Name: flagAccessKey,
Usage: "Required. Specify s3 access key.",
EnvVars: []string{envAccessKey},
},
&cli.StringFlag{
Name: flagSecretKey,
Usage: "Required. Specify s3 secret key.",
EnvVars: []string{envSecretKey},
},
&cli.StringFlag{
Name: flagEndpoint,
Usage: "Required. Specify s3 endpoint url.",
EnvVars: []string{envEndpoint},
},
&cli.StringFlag{
Name: flagBucket,
Usage: "Required. Specify s3 bucket name.",
EnvVars: []string{envBucket},
},
&cli.StringFlag{
Name: flagFilename,
Usage: "Required. Specify filename.",
EnvVars: []string{envFilename},
},
&cli.BoolFlag{
Name: flagSkipmakedeletebucket,
Usage: "Optional. Measure skipmakedeletebucket operations",
EnvVars: []string{envSkipmakedeletebucket},
},
},
Action: func(c *cli.Context) error {
if err := startDaemon(c); err != nil {
klog.Fatalf("Error starting daemon: %v", err)
return err
}
return nil
},
}
}
func startDaemon(c *cli.Context) error {
listenAddress := c.String(flagListenAddress)
opTimeout := c.Int(flagOpTimeout)
bucket := c.String(flagBucket)
if bucket == "" {
return fmt.Errorf("invalid empty flag %v", flagBucket)
}
endpoint := c.String(flagEndpoint)
if endpoint == "" {
return fmt.Errorf("invalid empty flag %v", flagEndpoint)
}
accessKey := c.String(flagAccessKey)
if accessKey == "" {
return fmt.Errorf("invalid empty flag %v", flagAccessKey)
}
secretKey := c.String(flagSecretKey)
if secretKey == "" {
return fmt.Errorf("invalid empty flag %v", flagSecretKey)
}
filename := c.String(flagFilename)
if filename == "" {
return fmt.Errorf("invalid empty flag %v", flagFilename)
}
exporter := Exporter{
bucket: bucket,
accessKey: accessKey,
secretKey: secretKey,
endpoint: endpoint,
filename: filename,
skipmakedeletebucket: c.Bool(flagSkipmakedeletebucket),
mutex: &sync.Mutex{},
opTimeout: opTimeout,
}
klog.Infoln("Starting s3_prober")
klog.Infoln("Build context")
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
probeHandler(w, r, exporter)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>S3 Prober</title></head>
<body>
<h1>S3 Prober</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
if err != nil {
klog.Error(err)
}
})
klog.Infoln("Listening on", listenAddress)
server := &http.Server{
Addr: listenAddress,
ReadHeaderTimeout: 1 * time.Minute,
}
klog.Fatal(server.ListenAndServe())
return nil
}
func main() {
a := cli.NewApp()
a.Usage = "S3 Prober"
a.Commands = []*cli.Command{
startCmd(),
}
if err := a.Run(os.Args); err != nil {
klog.Fatalf("Critical error: %v", err)
}
}