-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblackduck_exporter.go
696 lines (612 loc) · 17 KB
/
blackduck_exporter.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
"log"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
)
const (
namespace = "blackduck"
)
var (
listeningAddress = flag.String("telemetry.address", ":9125", "Address on which to expose metrics")
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics")
blackduckURL = flag.String("blackduck.url", "https://blackduck/", "URL of blackduck server to scrape")
blackduckUsername = flag.String("blackduck.username", "", "BlackDuck username to use for API authentication")
blackduckPasswordFile = flag.String("blackduck.password.file", "", "File (secret) containing BlackDuck password")
blackduckPassword = flag.String("blackduck.password", "", "BlackDuck password in plain text (blackduck.password.file is recommended instead)")
blackduckAPIToken = flag.String("blackduck.api.token", "", "API token to use instead of username/password")
insecure = flag.Bool("insecure", false, "Don't validate ssl")
sslServerName = flag.String("ssl.server.name", "", "Server Name of the Black Duck SSL cert")
showVersion = flag.Bool("version", false, "Print version information")
debug = flag.Bool("debug", false, "Print debugging information")
hc *http.Client
)
// Exporter : Exported metrics data
type Exporter struct {
URI string
mutex sync.Mutex
client *http.Client
scrapeFailures prometheus.Counter
jobsFailed prometheus.Gauge
longestJobRunning prometheus.Gauge
averageDurationOfRunningJobs prometheus.Gauge
jobs *prometheus.GaugeVec
scans *prometheus.GaugeVec
jobLastSeenRunning *prometheus.GaugeVec
jobTypesRunning *prometheus.GaugeVec
scrapeTime prometheus.Gauge
}
// NewExporter : Creates a new collector/exporter using a blackduck url
func NewExporter(uri string) *Exporter {
return &Exporter{
URI: uri,
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures_total",
Help: "Number of errors while scraping blackduck.",
}),
jobsFailed: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "jobs_failed_total",
Help: "Number of jobs that have ever failed.",
}),
jobs: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "jobs",
Help: "Number of jobs currently in queue.",
},
[]string{"state"},
),
jobTypesRunning: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "job_types_running",
Help: "Number of jobs currently running by type.",
},
[]string{"type"},
),
scans: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "scans",
Help: "Scans currently pending completion.",
},
[]string{"state"},
),
jobLastSeenRunning: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "job_last_seen_running",
Help: "Time of the jobs last seen running.",
},
[]string{"guid", "type"},
),
longestJobRunning: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "longest_running_job_duration",
Help: "Longest duration of currently running jobs",
}),
averageDurationOfRunningJobs: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "average_duration_of_running_jobs",
Help: "Average duration of all currently running jobs",
}),
scrapeTime: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "scrape_duration_seconds",
Help: "Time taken in seconds to scrape metrics from Black Duck.",
}),
}
}
// Describe : Collector implementation for Prometheus
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.scrapeFailures.Describe(ch)
e.jobsFailed.Describe(ch)
e.jobs.Describe(ch)
e.scans.Describe(ch)
e.jobLastSeenRunning.Describe(ch)
e.jobTypesRunning.Describe(ch)
e.averageDurationOfRunningJobs.Describe(ch)
e.longestJobRunning.Describe(ch)
}
// Collect : Collector implementation for Prometheus
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
err := e.collect(ch)
if err != nil {
log.Printf("error collecting stats: %v", err)
e.scrapeFailures.Inc()
}
e.scrapeFailures.Collect(ch)
}
func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
start := time.Now()
e.mutex.Lock()
defer e.mutex.Unlock()
auth, err := getAuthTokens()
if err != nil {
return err
}
jobStats, err := getJobStats(auth)
if err != nil {
return err
}
jobs, err := getJobs(auth)
if err != nil {
return err
}
jobStatusCounts := make(map[string]int)
for _, job := range jobs.Items {
if _, ok := jobStatusCounts[job.Status]; !ok {
jobStatusCounts[job.Status] = 0
}
jobStatusCounts[job.Status]++
}
for _, key := range []string{
"RUNNING",
"SCHEDULED",
"DISPATCHED",
"ERROR",
} {
e.jobs.WithLabelValues(key).Set(0.0)
}
for key, count := range jobStatusCounts {
e.jobs.WithLabelValues(key).Set(float64(count))
}
e.jobs.Collect(ch)
jobTypeCounts := make(map[string]int)
var supportedTypes []string
for _, item := range jobStats.Items {
supportedTypes = append(supportedTypes, item.JobType)
}
sort.Strings(supportedTypes)
for _, job := range jobs.Items {
i := sort.SearchStrings(supportedTypes, job.JobSpec.Type)
if job.Status == "RUNNING" && i < len(supportedTypes) && supportedTypes[i] == job.JobSpec.Type {
if _, ok := jobTypeCounts[job.JobSpec.Type]; !ok {
jobTypeCounts[job.JobSpec.Type] = 0
}
jobTypeCounts[job.JobSpec.Type]++
}
}
for _, key := range supportedTypes {
e.jobTypesRunning.WithLabelValues(key).Set(0.0)
}
for key, count := range jobTypeCounts {
e.jobTypesRunning.WithLabelValues(key).Set(float64(count))
}
e.jobTypesRunning.Collect(ch)
for _, job := range jobs.Items {
if job.Status == "RUNNING" {
e.jobLastSeenRunning.WithLabelValues(job.ID, job.JobSpec.Type).SetToCurrentTime()
}
}
e.jobLastSeenRunning.Collect(ch)
var highestDuration float64
var totalDuration float64
highestDuration = 0.0
totalDuration = 0.0
totalRunning := 0
for _, job := range jobs.Items {
if job.Status == "RUNNING" {
duration := time.Since(job.StartedAt.Time).Seconds()
if duration > highestDuration {
highestDuration = duration
}
totalDuration += duration
totalRunning += 1
}
}
var averageDuration float64
if totalRunning > 0 {
averageDuration = totalDuration / float64(totalRunning)
}
e.longestJobRunning.Set(highestDuration)
e.longestJobRunning.Collect(ch)
e.averageDurationOfRunningJobs.Set(averageDuration)
e.averageDurationOfRunningJobs.Collect(ch)
numFailedJobs := jobStats.TotalFailures()
e.jobsFailed.Set(float64(numFailedJobs))
e.jobsFailed.Collect(ch)
scans, err := getScans(auth)
if err != nil {
return err
}
for _, key := range []string{
"IN_PROGRESS",
"UNSTARTED",
"COMPLETED",
"ERROR",
} {
e.scans.WithLabelValues(key).Set(0.0)
}
for _, scan := range scans.Items {
for _, status := range scan.Status {
if status.OperationNameCode == "ServerScanning" {
e.scans.WithLabelValues(status.Status).Inc()
}
}
}
e.scans.Collect(ch)
elapsed := time.Now().Sub(start)
e.scrapeTime.Set(elapsed.Seconds())
e.scrapeTime.Collect(ch)
return nil
}
type scanJSON struct {
Items []struct {
Name string `json:"name"`
Status []struct {
OperationNameCode string `json:"operationNameCode"`
Status string `json:"status"`
} `json:"status"`
} `json:"items"`
TotalCount int `json:"totalCount"`
ErrorMessage string `json:"errorMessage"`
}
func getScans(auth *authTokens) (scanJSON, error) {
var j scanJSON
if *debug {
log.Print("fetching scans")
}
form := url.Values{}
form.Add("limit", "1000")
form.Add("offset", "0")
form.Add("sort", "updatedAt DESC")
form.Add("filter", "codeLocationStatus:in_progress")
form.Add("filter", "codeLocationStatus:in_progress")
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/api/codelocations?%s", *blackduckURL, form.Encode()),
nil)
if err != nil {
return j, err
}
auth.Auth(req)
resp, err := hc.Do(req)
if err != nil {
return j, err
}
if *debug {
log.Printf("scan response: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read scan response body: %v", err)
return j, err
}
if *debug {
log.Printf("scan response body: %s", body)
}
err = json.Unmarshal(body, &j)
if err != nil {
log.Printf("could not get scans: %v", err)
return j, err
}
if j.ErrorMessage != "" {
log.Printf("server error when fetching scans: %s", j.ErrorMessage)
return j, fmt.Errorf("Problem fetching scans: %s", j.ErrorMessage)
}
return j, nil
}
type BDTime struct {
Time time.Time
}
const bdTimeLayout = "2006-01-02T15:04:05.999Z"
var _ json.Unmarshaler = &BDTime{}
func (bdt *BDTime) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "null" {
bdt.Time = time.Time{}
return nil
}
t, err := time.Parse(bdTimeLayout, s)
bdt.Time = t
return err
}
type jobStatsJSON struct {
Items []struct {
JobType string `json:"jobType"`
TotalFailures int `json:"totalFailures"`
TotalInProgress int `json:"totalInProgress"`
TotalRuns int `json:"totalRuns"`
TotalSuccesses int `json:"totalSuccesses`
} `json:"items"`
ErrorMessage string `json:"errorMessage"`
}
func (stats jobStatsJSON) TotalFailures() int {
failures := 0
for _, item := range stats.Items {
failures += item.TotalFailures
}
return failures
}
func getJobStats(auth *authTokens) (jobStatsJSON, error) {
var j jobStatsJSON
if *debug {
log.Print("fetching job stats")
}
form := url.Values{}
form.Add("limit", "1000")
form.Add("sortField", "jobType")
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/api/job-statistics?%s", *blackduckURL, form.Encode()),
nil)
if err != nil {
return j, err
}
auth.Auth(req)
resp, err := hc.Do(req)
if err != nil {
return j, err
}
if *debug {
log.Printf("job stats response: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read job stats response body: %v", err)
return j, err
}
if *debug {
log.Printf("job stats response body: %s", body)
}
err = json.Unmarshal(body, &j)
if err != nil {
log.Printf("could not get job stats: %v", err)
return j, err
}
if j.ErrorMessage != "" {
log.Printf("server error when fetching job stats: %s", j.ErrorMessage)
return j, fmt.Errorf("Problem fetching job stats: %s", j.ErrorMessage)
}
return j, nil
}
type jobJSON struct {
Items []struct {
ID string `json:"id"`
Status string `json:"status"`
JobSpec struct {
Type string `json:"jobType"`
} `json:"jobSpec"`
StartedAt BDTime `json:"startedAt"`
} `json:"items"`
TotalCount int `json:"totalCount"`
ErrorMessage string `json:"errorMessage"`
}
func getJobs(auth *authTokens) (jobJSON, error) {
var j jobJSON
if *debug {
log.Print("fetching jobs")
}
form := url.Values{}
form.Add("limit", "1000")
form.Add("offset", "0")
form.Add("sortField", "scheduledAt")
form.Add("ascending", "false")
form.Add("filter", "jobStatus:scheduled")
form.Add("filter", "jobStatus:dispatched")
form.Add("filter", "jobStatus:running")
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/api/v1/jobs?%s", *blackduckURL, form.Encode()),
nil)
if err != nil {
return j, err
}
auth.Auth(req)
resp, err := hc.Do(req)
if err != nil {
return j, err
}
if *debug {
log.Printf("job response: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read job response body: %v", err)
return j, err
}
if *debug {
log.Printf("job response body: %s", body)
}
err = json.Unmarshal(body, &j)
if err != nil {
log.Printf("could not get jobs: %v", err)
return j, err
}
if j.ErrorMessage != "" {
log.Printf("server error when fetching jobs: %s", j.ErrorMessage)
return j, fmt.Errorf("Problem fetching jobs: %s", j.ErrorMessage)
}
return j, nil
}
func getNumJobsFailed(auth *authTokens) (int, error) {
var j jobJSON
if *debug {
log.Print("fetching job failed count")
}
form := url.Values{}
form.Add("limit", "1")
form.Add("offset", "0")
form.Add("filter", "jobStatus:failed")
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/api/v1/jobs?%s", *blackduckURL, form.Encode()),
nil)
if err != nil {
return -1, err
}
auth.Auth(req)
resp, err := hc.Do(req)
if err != nil {
return -1, err
}
if *debug {
log.Printf("job failed count response: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read job failed count response body: %v", err)
return -1, err
}
if *debug {
log.Printf("job failed count response body: %s", body)
}
err = json.Unmarshal(body, &j)
if err != nil {
log.Printf("could not get job error count: %v", err)
return -1, err
}
if j.ErrorMessage != "" {
log.Printf("server error when fetching job error count: %s", j.ErrorMessage)
return -1, fmt.Errorf("Problem fetching job error count: %s", j.ErrorMessage)
}
return j.TotalCount, nil
}
// getPassword : Returns the password from either the plain text string or the specified password file
func getPassword() string {
var password string
password = ""
if *blackduckPassword != "" {
password = *blackduckPassword
}
if *blackduckPasswordFile != "" {
buf, err := ioutil.ReadFile(*blackduckPasswordFile)
if err == nil {
password = strings.TrimSpace(string(buf))
}
}
return password
}
type authTokens struct {
Cookie *http.Cookie
BearerToken string `json:"bearerToken"`
}
func getAuthTokens() (*authTokens, error) {
if *blackduckUsername != "" && getPassword() != "" {
return getAuthTokensBasic()
}
if *blackduckAPIToken != "" {
return getAuthTokensAPIKey()
}
return nil, fmt.Errorf("No authentication information available!")
}
// getCookie : Uses credentials to get cookie from BlackDuck
func getAuthTokensBasic() (*authTokens, error) {
var a authTokens
form := url.Values{}
form.Add("j_username", *blackduckUsername)
form.Add("j_password", getPassword())
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/j_spring_security_check", *blackduckURL),
strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("Could not log in with user '%s' and provided password : error code %d", *blackduckUsername, resp.StatusCode)
}
for _, cookie := range resp.Cookies() {
if strings.Contains(cookie.String(), "JSESSIONID=") || strings.Contains(cookie.String(), "AUTHORIZATION_BEARER=") {
a.Cookie = cookie
return &a, nil
}
}
err = errors.New("Could not get cookie from blackduck using credentials provided")
return nil, err
}
func getAuthTokensAPIKey() (*authTokens, error) {
var a authTokens
if *debug {
log.Println("authenticating with api key")
}
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/api/tokens/authenticate", *blackduckURL),
nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", *blackduckAPIToken))
resp, err := hc.Do(req)
if err != nil {
log.Printf("could not authenticate: %v", err)
return nil, err
}
defer resp.Body.Close()
if *debug {
log.Printf("auth request status: %s", resp.Status)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read auth tokens: %v", err)
return nil, err
}
if *debug {
log.Printf("auth response body: %s", body)
}
err = json.Unmarshal(body, &a)
if err != nil {
log.Printf("could not decode auth response json: %v", err)
return nil, err
}
return &a, nil
}
func (a *authTokens) Auth(req *http.Request) {
if a.Cookie != nil {
req.AddCookie(a.Cookie)
}
if a.BearerToken != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.BearerToken))
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("blackduck_exporter"))
os.Exit(0)
}
tlsConfig := &tls.Config{InsecureSkipVerify: *insecure}
if *sslServerName != "" {
tlsConfig.ServerName = *sslServerName
}
hc = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = tlsConfig
prometheus.MustRegister(NewExporter(*blackduckURL))
prometheus.MustRegister(version.NewCollector("blackduck_exporter"))
http.Handle(*metricsEndpoint, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>BlackDuck Exporter</title></head>
<body>
<h1>BlackDuck Exporter</h1>
<p><a href='` + *metricsEndpoint + `'>Metrics</a></p>
</body>
</html>`))
})
glog.Fatal(http.ListenAndServe(*listeningAddress, nil))
}