-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
96 lines (85 loc) · 2.35 KB
/
metrics.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
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron/v3"
logger "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/steffakasid/trivy-scanner/internal"
"net/http"
"strconv"
)
var (
registeredGauges []prometheus.Gauge
trivyResults internal.TrivyResults
reg *prometheus.Registry
)
func startDaemon() {
reg = prometheus.NewRegistry()
initCron()
fetchResults()
promHandler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
http.Handle("/metrics", promHandler)
logger.Infoln("Starting metrics daemon...")
err := http.ListenAndServe(fmt.Sprintf(":%d", viper.GetInt(internal.METRICS_PORT)), nil)
if err != nil {
logger.Fatal(err)
}
}
func fetchResults() {
projs, err := scan.GitLabClient.GetProjects(scan.ID)
if err != nil {
logger.Errorf("failed getting projects: %v", err)
}
trivyResults, err = scan.ScanProjects(projs)
if err != nil {
logger.Errorf("failed scan projects: %v", err)
}
updateRegister()
}
func updateRegister() {
unregisterOldGauges()
registeredGauges = []prometheus.Gauge{}
for _, trivy := range trivyResults {
var trivyIgnore string
if len(trivy.Ignore) > 0 {
trivyIgnore = "true"
} else {
trivyIgnore = "false"
}
labels := map[string]string{
"Project": trivy.ProjName,
"Id": strconv.Itoa(trivy.ProjId),
"Vulnerabilities": strconv.Itoa(trivy.Vulnerabilities.Count),
"High": strconv.Itoa(trivy.Vulnerabilities.High),
"Critical": strconv.Itoa(trivy.Vulnerabilities.Critical),
"ScannedJobName": viper.GetString(internal.JOB_NAME),
"JobState": trivy.State,
"trivyignore": trivyIgnore,
}
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "trivy",
Subsystem: "exporter",
Name: "findings",
Help: "this is a cached result and will updated every hour",
ConstLabels: labels,
})
gauge.Set(float64(trivy.Vulnerabilities.Count))
registeredGauges = append(registeredGauges, gauge)
reg.MustRegister(gauge)
}
}
func unregisterOldGauges() {
for _, gauge := range registeredGauges {
reg.Unregister(gauge)
}
}
func initCron() {
c := cron.New()
_, err := c.AddFunc(viper.GetString(internal.METRICS_CRON), fetchResults)
if err != nil {
logger.Fatal(err)
}
c.Start()
}