/
collector.go
109 lines (98 loc) · 2.75 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
package main
import (
"fmt"
"log"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type Collector struct {
config *Config
}
func NewCollector(cfg *Config) *Collector {
return &Collector{
config: cfg,
}
}
func decodeStatus(status string) float64 {
switch status {
case "Error":
return 0
case "OK":
return 1
default:
return 0
}
}
func (c *Collector) collectCertificates() {
log.Println("Collect Certificates")
checker := NewCertificatesChecker(c.config.GetCertificateDomains())
results := checker.Check()
for _, result := range results {
DomainCertificateStatus.With(
prometheus.Labels{"domain": result.Domain, "cname": result.CNAME},
).Set(decodeStatus(result.Status))
DomainCertificateExpireDays.With(
prometheus.Labels{"domain": result.Domain, "cname": result.CNAME},
).Set(float64(result.ExpireDays))
}
log.Println("Collect Certificates Finish")
}
func (c *Collector) collectDomains() {
log.Println("Collect Whois Informations")
checker := NewWhoisChecker(c.config.GetWhoisDomains())
results := checker.Check()
for _, result := range results {
DomainWhoisStatus.With(prometheus.Labels{"domain": result.Domain}).Set(decodeStatus(result.Status))
DomainWhoisExpireDays.With(prometheus.Labels{"domain": result.Domain}).Set(float64(result.ExpireDays))
}
log.Println("Collect Whois Informations Finish")
}
func (c *Collector) collectResolves() {
log.Println("Collect Resolve Informations")
checker := NewResolveChecker(c.config.GetResolveDomains())
results := checker.Check()
for _, result := range results {
DomainResolveStatus.With(prometheus.Labels{"domain": result.Domain}).Set(decodeStatus(result.Status))
DomainResolveIPs.With(prometheus.Labels{"domain": result.Domain}).Set(float64(len(result.IPs)))
}
log.Println("Collect Resolve Informations Finish")
}
func (c *Collector) collectRequest() {
log.Println("Collect Request Informations")
checker := NewRequestChecker(c.config.GetRequestDomains())
results := checker.Check()
for _, result := range results {
DomainRequestStatus.With(
prometheus.Labels{
"domain": result.Domain,
"host": result.Host,
"path": result.Path,
},
).Set(decodeStatus(result.Status))
if result.Status == "Error" {
DomainRequestError.With(
prometheus.Labels{
"domain": result.Domain,
"host": result.Host,
"path": result.Path,
"address": result.Address,
"status": fmt.Sprintf("%v", result.StatusCode),
},
).Inc()
}
}
log.Println("Collect Request Informations Finish")
}
func (c *Collector) CollectOnce() {
go c.collectCertificates()
go c.collectDomains()
go c.collectResolves()
go c.collectRequest()
}
func (c *Collector) Start() {
for {
c.CollectOnce()
sleepSecs := c.config.GetDuration()
time.Sleep(sleepSecs)
}
}