-
Notifications
You must be signed in to change notification settings - Fork 3
/
collector.go
executable file
·87 lines (78 loc) · 2.36 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
package main
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type MetricsCollector struct {
AccessToken string `json:"access_token"`
OpsManCertificateListUrl string
Certificates []struct {
Configurable bool `json:"configurable"`
IsCa bool `json:"is_ca"`
PropertyReference string `json:"property_reference"`
PropertyType string `json:"property_type"`
ProductGUID string `json:"product_guid"`
Location string `json:"location"`
VariablePath string `json:"variable_path"`
Issuer string `json:"issuer"`
ValidFrom time.Time `json:"valid_from"`
ValidUntil time.Time `json:"valid_until"`
} `json:"certificates"`
}
// When you encounter error, increment the counter and display the error on stdout
func IncrementErrorCounter(error string) error {
Errorf(error)
ErrorTotal.WithLabelValues(cmdOptions.Environment).Inc()
return fmt.Errorf(error)
}
//
func (m *MetricsCollector) collector() {
// Authenticate the request
err := m.authenticate()
if err != nil {
return
}
// Get the data of list of certs
m.OpsManCertificateListUrl = fmt.Sprintf("https://%s/api/v0/deployed/certificates", cmdOptions.OpsManHostname)
c, err := m.opsmanRequestHandler()
if err != nil {
IncrementErrorCounter(fmt.Sprintf("error during extraction of certs from opsman: %v", err))
return
}
// Load the information to the cert struct
err = json.Unmarshal(c, &m)
if err != nil {
IncrementErrorCounter(fmt.Sprintf("error in unmarshal of certificates: %v", err))
return
}
// Convert to Prometheus metrics
m.metric()
}
func (m *MetricsCollector) metric() {
for _, c := range m.Certificates {
CertExpirySeconds.WithLabelValues(
cmdOptions.Environment,
strconv.FormatBool(c.IsCa),
strconv.FormatBool(c.Configurable),
c.PropertyReference,
c.ProductGUID,
c.Location,
c.VariablePath,
c.Issuer,
fmt.Sprintf("%v", c.ValidFrom),
fmt.Sprintf("%v", c.ValidUntil)).Set(time.Until(c.ValidUntil).Seconds())
CertExpiryUnixTimestamp.WithLabelValues(
cmdOptions.Environment,
strconv.FormatBool(c.IsCa),
strconv.FormatBool(c.Configurable),
c.PropertyReference,
c.ProductGUID,
c.Location,
c.VariablePath,
c.Issuer,
fmt.Sprintf("%v", c.ValidFrom),
fmt.Sprintf("%v", c.ValidUntil)).Set(float64(c.ValidUntil.Unix()))
}
}