-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.go
140 lines (118 loc) 路 3.8 KB
/
collector.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
package collector
import (
"os"
"strconv"
"time"
"github.com/go-kit/log/level"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)
const namespace = "digicert"
type DigicertCollector struct {
digicertAPIEndpoint string
digicertAPIKey string
showExpiredCertificates bool
sandboxMode bool
up *prometheus.Desc
scrapeDuration *prometheus.Desc
certificateExpire *prometheus.Desc
logger log.Logger
}
func (c *DigicertCollector) Collect(ch chan<- prometheus.Metric) {
c.UpdateMetrics(ch)
}
func (c *DigicertCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.up
ch <- c.scrapeDuration
ch <- c.certificateExpire
}
func NewDigicertCollector(logger log.Logger,
digicertURL string,
digicertAPIKey string,
sandboxMode bool,
digicertShowExpiredCertificates bool) (*DigicertCollector, error) {
// Build the collector
c := &DigicertCollector{
digicertAPIEndpoint: digicertURL,
digicertAPIKey: digicertAPIKey,
showExpiredCertificates: digicertShowExpiredCertificates,
sandboxMode: sandboxMode,
logger: logger,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "api", "up"),
"Was the last Digicert API query successful.",
nil, nil,
),
scrapeDuration: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "scrape_duration", "seconds"),
"Exporter scrape duration in seconds.",
nil, nil,
),
certificateExpire: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "certificate_expire", "timestamp_seconds"),
"Certificate expiration date.",
[]string{"order_id", "certificate_id", "certificate_common_name", "organization"}, nil,
),
}
if !c.sandboxMode && (digicertURL == "" || digicertAPIKey == "") {
level.Error(logger).
Log("msg", "Either DIGICERT_URL or DIGICERT_API_KEY is missing. Exiting")
os.Exit(1)
} else {
level.Info(logger).Log("msg", "Exporter started correctly")
}
if c.sandboxMode {
level.Warn(logger).Log("msg", "Using sandbox mode configuration (mock.json will be used)")
}
return c, nil
}
func (c *DigicertCollector) UpdateMetrics(ch chan<- prometheus.Metric) {
start := time.Now()
orderList, err := c.FetchDigicertData()
if err != nil {
ch <- prometheus.MustNewConstMetric(
c.up, prometheus.GaugeValue, 0,
)
return
}
seenCertificationCommonName := make(map[string]Order)
for i := 0; i < len(orderList.Orders); i++ {
order := orderList.Orders[i]
certificateCommonName := order.Certificate.CommonName
certificateExpireDate := order.FormatDateTimestamp()
// A valid date must be in the future, or show all if showExpiredCertificates = true
if certificateExpireDate.After(time.Now()) || c.showExpiredCertificates {
seenOrder := seenCertificationCommonName[certificateCommonName]
// Test if the collector already encounter this cert common name
if seenOrder.FormatDateTimestamp().IsZero() {
// If no, insert into the map
seenCertificationCommonName[certificateCommonName] = orderList.Orders[i]
} else {
// If yes AND the new date is after the current one, replace it
if certificateExpireDate.After(seenOrder.FormatDateTimestamp()) {
seenCertificationCommonName[certificateCommonName] = orderList.Orders[i]
}
}
}
}
for name, order := range seenCertificationCommonName {
if err == nil {
ch <- prometheus.MustNewConstMetric(
c.certificateExpire,
prometheus.UntypedValue,
float64(order.FormatDateTimestamp().Unix()),
strconv.Itoa(order.ID),
strconv.Itoa(order.Certificate.ID),
name,
order.Organization.Name,
)
}
}
end := time.Now()
ch <- prometheus.MustNewConstMetric(
c.scrapeDuration, prometheus.GaugeValue, end.Sub(start).Seconds(),
)
ch <- prometheus.MustNewConstMetric(
c.up, prometheus.GaugeValue, 1,
)
}