-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathexport_gauge.go
177 lines (167 loc) · 6.21 KB
/
export_gauge.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package export
import (
"encoding/json"
"fmt"
"sync"
"github.com/eryajf/cloud_dns_exporter/public/logger"
"github.com/eryajf/cloud_dns_exporter/pkg/provider"
"github.com/eryajf/cloud_dns_exporter/public"
"github.com/prometheus/client_golang/prometheus"
)
// 指标结构体
type Metrics struct {
metrics map[string]*prometheus.Desc
mutex sync.Mutex
}
// newGlobalMetric 创建指标描述符
func newGlobalMetric(namespace string, metricName string, docString string, labels []string) *prometheus.Desc {
if namespace == "" {
return prometheus.NewDesc(metricName, docString, labels, nil)
} else {
return prometheus.NewDesc(namespace+"_"+metricName, docString, labels, nil)
}
}
// NewMetrics 初始化指标信息,即Metrics结构体
func NewMetrics(namespace string) *Metrics {
return &Metrics{
metrics: map[string]*prometheus.Desc{
public.DomainList: newGlobalMetric(namespace,
public.DomainList,
"Cloud Domain List",
[]string{
"cloud_provider",
"cloud_name",
"domain_id",
"domain_name",
"domain_remark",
"domain_status",
"created_date",
"expiry_date",
}),
public.RecordList: newGlobalMetric(namespace,
public.RecordList,
"Cloud Doamin Record List",
[]string{
"cloud_provider",
"cloud_name",
"domain_name",
"record_id",
"record_type",
"record_name",
"record_value",
"record_ttl",
"record_weight",
"record_status",
"record_remark",
"update_time",
"full_record",
}),
public.RecordCertInfo: newGlobalMetric(namespace,
public.RecordCertInfo,
"Cloud Doamin Record Cert Info",
[]string{
"cloud_provider",
"cloud_name",
"domain_name",
"record_id",
"full_record",
"subject_common_name",
"subject_organization",
"subject_organizational_unit",
"issuer_common_name",
"issuer_organization",
"issuer_organizational_unit",
"created_date",
"expiry_date",
"cert_matched",
"error_msg",
}),
},
}
}
// Describe 传递结构体中的指标描述符到channel
func (c *Metrics) Describe(ch chan<- *prometheus.Desc) {
for _, m := range c.metrics {
ch <- m
}
}
// Collect 抓取最新的数据,传递给channel
func (c *Metrics) Collect(ch chan<- prometheus.Metric) {
c.mutex.Lock()
defer c.mutex.Unlock()
for cloudProvider, accounts := range public.Config.CloudProviders {
for _, cloudAccount := range accounts.Accounts {
cloudName := cloudAccount["name"]
// get domain list from cache
domainListCacheKey := public.DomainList + "_" + cloudProvider + "_" + cloudName
var domains []provider.Domain
domainListCacheValue, err := public.Cache.Get(domainListCacheKey)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] get domain list failed: %v", domainListCacheKey, err))
continue
}
err = json.Unmarshal(domainListCacheValue, &domains)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] json.Unmarshal error: %v", domainListCacheKey, err))
continue
}
for _, v := range domains {
ch <- prometheus.MustNewConstMetric(
c.metrics[public.DomainList], prometheus.GaugeValue, float64(v.DaysUntilExpiry), v.CloudProvider, v.CloudName, v.DomainID, v.DomainName, v.DomainRemark, v.DomainStatus, v.CreatedDate, v.ExpiryDate)
}
// get record list from cache
recordListCacheKey := public.RecordList + "_" + cloudProvider + "_" + cloudName
var records []provider.Record
recordListCacheValue, err := public.Cache.Get(recordListCacheKey)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] get record list failed: %v", domainListCacheKey, err))
continue
}
err = json.Unmarshal(recordListCacheValue, &records)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] json.Unmarshal error: %v", domainListCacheKey, err))
continue
}
for _, v := range records {
if v.RecordName == "@" && v.RecordType == "NS" { // Special Record, Skip it.
continue
}
ch <- prometheus.MustNewConstMetric(
c.metrics[public.RecordList], prometheus.GaugeValue, 1, v.CloudProvider, v.CloudName, v.DomainName, v.RecordID, v.RecordType, v.RecordName, v.RecordValue, v.RecordTTL, v.RecordWeight, v.RecordStatus, v.RecordRemark, v.UpdateTime, v.FullRecord)
}
// get record cert info list from cache
recordCertInfoCacheKey := public.RecordCertInfo + "_" + cloudProvider + "_" + cloudName
var recordCerts []provider.RecordCert
recordCertInfoCacheValue, err := public.CertCache.Get(recordCertInfoCacheKey)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] get record list failed: %v", recordCertInfoCacheKey, err))
continue
}
err = json.Unmarshal(recordCertInfoCacheValue, &recordCerts)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] json.Unmarshal error: %v", recordCertInfoCacheKey, err))
continue
}
for _, v := range recordCerts {
if v.RecordID == "" {
continue
}
ch <- prometheus.MustNewConstMetric(c.metrics[public.RecordCertInfo], prometheus.GaugeValue, float64(v.DaysUntilExpiry), v.CloudProvider, v.CloudName, v.DomainName, v.RecordID, v.FullRecord, v.SubjectCommonName, v.SubjectOrganization, v.SubjectOrganizationalUnit, v.IssuerCommonName, v.IssuerOrganization, v.IssuerOrganizationalUnit, v.CreatedDate, v.ExpiryDate, fmt.Sprintf("%t", v.CertMatched), v.ErrorMsg)
}
}
}
// get custom record cert info list from cache
recordCertInfoCacheKey := public.RecordCertInfo + "_" + public.CustomRecords
var recordCerts []provider.RecordCert
recordCertInfoCacheValue, err := public.CertCache.Get(recordCertInfoCacheKey)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] get record list failed: %v", recordCertInfoCacheKey, err))
}
err = json.Unmarshal(recordCertInfoCacheValue, &recordCerts)
if err != nil {
logger.Error(fmt.Sprintf("[ %s ] json.Unmarshal error: %v", recordCertInfoCacheKey, err))
}
for _, v := range recordCerts {
ch <- prometheus.MustNewConstMetric(c.metrics[public.RecordCertInfo], prometheus.GaugeValue, float64(v.DaysUntilExpiry), v.CloudProvider, v.CloudName, v.DomainName, v.RecordID, v.FullRecord, v.SubjectCommonName, v.SubjectOrganization, v.SubjectOrganizationalUnit, v.IssuerCommonName, v.IssuerOrganization, v.IssuerOrganizationalUnit, v.CreatedDate, v.ExpiryDate, fmt.Sprintf("%t", v.CertMatched), v.ErrorMsg)
}
}