Compare commits
No commits in common. "4ceb2023dbcfa71d7fd0d4bae975ade4fbe0edf8" and "0a3d0c0191236ca0e0c7380f08b7aaa4925d4c25" have entirely different histories.
4ceb2023db
...
0a3d0c0191
32
main.go
32
main.go
@ -28,7 +28,7 @@ type CertificateMetrics struct {
|
|||||||
IsValid prometheus.Gauge
|
IsValid prometheus.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCertificateMetrics(domain string, port int, alpn []string, name string) CertificateMetrics {
|
func NewCertificateMetrics(domain string, port int, alpn []string) CertificateMetrics {
|
||||||
metrics := CertificateMetrics{
|
metrics := CertificateMetrics{
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
Port: port,
|
Port: port,
|
||||||
@ -36,13 +36,13 @@ func NewCertificateMetrics(domain string, port int, alpn []string, name string)
|
|||||||
ExpiryIn: prometheus.NewGauge(
|
ExpiryIn: prometheus.NewGauge(
|
||||||
prometheus.GaugeOpts{
|
prometheus.GaugeOpts{
|
||||||
Name: "cert_status_expiry_in",
|
Name: "cert_status_expiry_in",
|
||||||
ConstLabels: prometheus.Labels{"name": name},
|
ConstLabels: prometheus.Labels{"domain": domain},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
IsValid: prometheus.NewGauge(
|
IsValid: prometheus.NewGauge(
|
||||||
prometheus.GaugeOpts{
|
prometheus.GaugeOpts{
|
||||||
Name: "cert_status_is_valid",
|
Name: "cert_status_is_valid",
|
||||||
ConstLabels: prometheus.Labels{"name": name},
|
ConstLabels: prometheus.Labels{"domain": domain},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@ -88,9 +88,8 @@ func (c *CertificateMetrics) checkTls() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateMetrics(metrics *[]CertificateMetrics, lastUpdated prometheus.Gauge) {
|
func updateMetrics(metrics *[]CertificateMetrics) {
|
||||||
log.Debugf("Updating metrics for %d domains...", len(*metrics))
|
log.Debugf("Updating metrics for %d domains...", len(*metrics))
|
||||||
lastUpdated.SetToCurrentTime()
|
|
||||||
for _, metric := range *metrics {
|
for _, metric := range *metrics {
|
||||||
metric.checkTls()
|
metric.checkTls()
|
||||||
}
|
}
|
||||||
@ -109,29 +108,15 @@ func run(ctx *cli.Context) error {
|
|||||||
|
|
||||||
// Setup metrics
|
// Setup metrics
|
||||||
registry := prometheus.NewRegistry()
|
registry := prometheus.NewRegistry()
|
||||||
lastUpdatedMetric := prometheus.NewGauge(
|
|
||||||
prometheus.GaugeOpts{
|
|
||||||
Name: "certs_status_last_updated",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
registry.MustRegister(lastUpdatedMetric)
|
|
||||||
|
|
||||||
metrics := make([]CertificateMetrics, 0)
|
metrics := make([]CertificateMetrics, 0)
|
||||||
for _, d := range domains {
|
for _, d := range domains {
|
||||||
log.Debugf("Parsing '%s'...", d)
|
log.Debugf("Parsing '%s'...", d)
|
||||||
parts := strings.Split(d, ":")
|
parts := strings.Split(d, ":")
|
||||||
if len(parts) < 3 {
|
if len(parts) != 3 {
|
||||||
log.Errorf("Invalid domain format for '%s'", d)
|
log.Errorf("Invalid domain format for '%s'", d)
|
||||||
return errors.New("Invalid domain format: Expects <domain>:<port>:<alpn>")
|
return errors.New("Invalid domain format: Expects <domain>:<port>:<alpn>")
|
||||||
}
|
}
|
||||||
|
|
||||||
name := ""
|
|
||||||
if len(parts) == 4 {
|
|
||||||
name = parts[3]
|
|
||||||
} else {
|
|
||||||
name = parts[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
port, err := strconv.Atoi(parts[1])
|
port, err := strconv.Atoi(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to parse port of '%s'", d)
|
log.Errorf("Failed to parse port of '%s'", d)
|
||||||
@ -141,12 +126,11 @@ func run(ctx *cli.Context) error {
|
|||||||
// Create the metric, and register it
|
// Create the metric, and register it
|
||||||
// TODO: Make this prettier
|
// TODO: Make this prettier
|
||||||
alpn := strings.Split(parts[2], ";")
|
alpn := strings.Split(parts[2], ";")
|
||||||
log.Debugf("Registering: domain='%s' port='%d' alpn=%v name='%s'", parts[0], port, alpn, name)
|
log.Debugf("Using ALPNs: %v", alpn)
|
||||||
metric := NewCertificateMetrics(
|
metric := NewCertificateMetrics(
|
||||||
parts[0],
|
parts[0],
|
||||||
port,
|
port,
|
||||||
alpn,
|
alpn,
|
||||||
name,
|
|
||||||
)
|
)
|
||||||
metric.register(registry)
|
metric.register(registry)
|
||||||
metrics = append(metrics, metric)
|
metrics = append(metrics, metric)
|
||||||
@ -163,7 +147,7 @@ func run(ctx *cli.Context) error {
|
|||||||
gocron.DurationJob(24*time.Hour),
|
gocron.DurationJob(24*time.Hour),
|
||||||
gocron.NewTask(
|
gocron.NewTask(
|
||||||
func() {
|
func() {
|
||||||
updateMetrics(&metrics, lastUpdatedMetric)
|
updateMetrics(&metrics)
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@ -174,7 +158,7 @@ func run(ctx *cli.Context) error {
|
|||||||
|
|
||||||
// Perform an initial run to populate the metrics
|
// Perform an initial run to populate the metrics
|
||||||
log.Info("Performing initial requests...")
|
log.Info("Performing initial requests...")
|
||||||
updateMetrics(&metrics, lastUpdatedMetric)
|
updateMetrics(&metrics)
|
||||||
log.Debug("Done")
|
log.Debug("Done")
|
||||||
|
|
||||||
http.Handle(
|
http.Handle(
|
||||||
|
Loading…
Reference in New Issue
Block a user