-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
224 lines (200 loc) · 6.63 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
es "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es/v20180416"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"os"
)
const (
NameSpace = "tc_info"
)
type EsExporter struct {
logger log.Logger
rateLimit int
credential common.CredentialIface
esInstance *prometheus.Desc
}
func NewEsExporter(rateLimit int, logger log.Logger, credential common.CredentialIface) *EsExporter {
return &EsExporter{
logger: logger,
rateLimit: rateLimit,
credential: credential,
esInstance: prometheus.NewDesc(
prometheus.BuildFQName(NameSpace, "es", "instance"),
"elastic instance on tencent cloud",
[]string{"instance_id", "name", "es_version"},
nil,
),
}
}
func (e *EsExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.esInstance
}
func (e *EsExporter) Collect(ch chan<- prometheus.Metric) {
// es collect
esClient, err := es.NewClient(e.credential, regions.Beijing, profile.NewClientProfile())
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to get tencent client")
panic(err)
}
esRequest := es.NewDescribeInstancesRequest()
esResponse, err := esClient.DescribeInstances(esRequest)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
// 暴露指标
for _, ins := range esResponse.Response.InstanceList {
ch <- prometheus.MustNewConstMetric(e.esInstance, prometheus.GaugeValue, 1,
[]string{*ins.InstanceId, *ins.InstanceName, *ins.EsVersion}...)
}
}
type CbsExporter struct {
logger log.Logger
rateLimit int
pageLimit uint64
credential common.CredentialIface
cbsInstance *prometheus.Desc
}
func NewCbsExporter(rateLimit int, logger log.Logger, pageLimit uint64, credential common.CredentialIface) *CbsExporter {
return &CbsExporter{
logger: logger,
rateLimit: rateLimit,
pageLimit: pageLimit,
credential: credential,
cbsInstance: prometheus.NewDesc(
prometheus.BuildFQName(NameSpace, "cbs", "instance"),
"cbs instance on tencent cloud",
[]string{"instance_id", "disk_id", "type", "name", "state"},
nil,
),
}
}
func (e *CbsExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.cbsInstance
}
func (e *CbsExporter) Collect(ch chan<- prometheus.Metric) {
// cbs collect
cbsClient, err := cbs.NewClient(e.credential, regions.Beijing, profile.NewClientProfile())
if err != nil {
_ = level.Error(e.logger).Log("msg", "Failed to get tencent client")
panic(err)
}
cbsRequest := cbs.NewDescribeDisksRequest()
cbsRequest.Limit = common.Uint64Ptr(e.pageLimit)
cbsResponse, err := cbsClient.DescribeDisks(cbsRequest)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
if cbsResponse != nil {
fmt.Printf("The API request id: %s ", *cbsResponse.Response.RequestId)
}
panic(err)
}
cbsTotal := *cbsResponse.Response.TotalCount
var count = uint64(0)
for {
if count > cbsTotal {
break
}
cbsResponse, err = cbsClient.DescribeDisks(cbsRequest)
if err != nil {
var retry = 3
for {
if retry == 0 {
break
}
cbsResponse, err = cbsClient.DescribeDisks(cbsRequest)
if err == nil {
break
}
retry--
}
}
if err != nil {
if cbsResponse != nil {
fmt.Printf("The API request id: %s ", *cbsResponse.Response.RequestId)
}
panic(err)
}
for _, disk := range cbsResponse.Response.DiskSet {
ch <- prometheus.MustNewConstMetric(e.cbsInstance, prometheus.GaugeValue, 1,
[]string{*disk.InstanceId, *disk.DiskId, *disk.InstanceType, *disk.DiskName, *disk.DiskState}...)
}
count += e.pageLimit
cbsRequest.Offset = common.Uint64Ptr(count)
}
}
func main() {
var (
webConfig = webflag.AddFlags(kingpin.CommandLine)
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9150").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
enableEs = kingpin.Flag("metrics.es", "Enable metric es").Bool()
enableCbs = kingpin.Flag("metrics.cbs", "Enable metric cbs").Bool()
cbsPageLimit = kingpin.Flag("cbs.page-limit", "CBS page limit, max 100").Default("100").Uint64()
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Version(version.Print("tc_info_exporter"))
kingpin.Parse()
logger := promlog.New(promlogConfig)
err := godotenv.Load()
if err != nil {
_ = level.Warn(logger).Log("msg", "Error loading .env file")
}
_ = level.Info(logger).Log("msg", "Starting tc_info_exporter", "version", version.Info())
_ = level.Info(logger).Log("msg", "Build context", "context", version.BuildContext())
// 连接腾讯云
provider := common.DefaultEnvProvider()
credential, err := provider.GetCredential()
if err != nil {
_ = level.Error(logger).Log("msg", "Failed to get credential")
panic(err)
}
prometheus.MustRegister(version.NewCollector(NameSpace))
if *enableCbs {
prometheus.MustRegister(NewCbsExporter(15, logger, *cbsPageLimit, credential))
}
if *enableEs {
prometheus.MustRegister(NewEsExporter(15, logger, credential))
}
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Tecent cloud info Exporter</title></head>
<body>
<h1>Tencent cloud info exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
_ = level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
srv := &http.Server{Addr: *listenAddress}
if err := web.ListenAndServe(srv, *webConfig, logger); err != nil {
_ = level.Error(logger).Log("msg", "Error running HTTP server", "err", err)
os.Exit(1)
}
}