forked from kbudde/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter_ecs.go
324 lines (295 loc) · 11.9 KB
/
exporter_ecs.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
func init() {
RegisterExporter("ecs", newExporterEcsRabbitmq)
}
type AliEcsMonitorResponse struct {
RequestID string `json:"RequestId"`
MonitorData struct {
InstanceMonitorData []struct {
IOPSRead int `json:"IOPSRead"`
IntranetBandwidth int `json:"IntranetBandwidth"`
IOPSWrite int `json:"IOPSWrite"`
InstanceID string `json:"InstanceId"`
IntranetTX int `json:"IntranetTX"`
CPU int `json:"CPU"`
BPSRead int `json:"BPSRead"`
IntranetRX int `json:"IntranetRX"`
TimeStamp time.Time `json:"TimeStamp"`
InternetBandwidth int `json:"InternetBandwidth"`
InternetTX int `json:"InternetTX"`
InternetRX int `json:"InternetRX"`
BPSWrite int `json:"BPSWrite"`
} `json:"InstanceMonitorData"`
} `json:"MonitorData"`
}
type ListAliEcsResponse struct {
Instances struct {
Instance []struct {
ResourceGroupID string `json:"ResourceGroupId"`
Memory int `json:"Memory"`
InstanceChargeType string `json:"InstanceChargeType"`
CPU int `json:"Cpu"`
OSName string `json:"OSName"`
InstanceNetworkType string `json:"InstanceNetworkType"`
InnerIpddress struct {
IPAddress []interface{} `json:"IpAddress"`
} `json:"InnerIpddress,omitempty"`
ExpiredTime string `json:"ExpiredTime"`
ImageID string `json:"ImageId"`
EipAddress struct {
AllocationID string `json:"AllocationId"`
IPAddress string `json:"IpAddress"`
InternetChargeType string `json:"InternetChargeType"`
} `json:"EipAddress"`
HostName string `json:"HostName"`
Tags struct {
Tag []struct {
TagKey string `json:"TagKey"`
TagValue string `json:"TagValue"`
} `json:"Tag"`
} `json:"Tags"`
VlanID string `json:"VlanId"`
Status string `json:"Status"`
HibernationOptions struct {
Configured bool `json:"Configured"`
} `json:"HibernationOptions"`
MetadataOptions struct {
HTTPTokens string `json:"HttpTokens"`
HTTPEndpoint string `json:"HttpEndpoint"`
} `json:"MetadataOptions"`
InstanceID string `json:"InstanceId"`
StoppedMode string `json:"StoppedMode"`
CPUOptions struct {
ThreadsPerCore int `json:"ThreadsPerCore"`
Numa string `json:"Numa"`
CoreCount int `json:"CoreCount"`
} `json:"CpuOptions"`
StartTime string `json:"StartTime"`
DeletionProtection bool `json:"DeletionProtection"`
SecurityGroupIds struct {
SecurityGroupID []string `json:"SecurityGroupId"`
} `json:"SecurityGroupIds"`
VpcAttributes struct {
PrivateIPAddress struct {
IPAddress []string `json:"IpAddress"`
} `json:"PrivateIpAddress"`
VpcID string `json:"VpcId"`
VSwitchID string `json:"VSwitchId"`
NatIPAddress string `json:"NatIpAddress"`
} `json:"VpcAttributes"`
InternetChargeType string `json:"InternetChargeType"`
InstanceName string `json:"InstanceName"`
DeploymentSetID string `json:"DeploymentSetId"`
InternetMaxBandwidthOut int `json:"InternetMaxBandwidthOut"`
SerialNumber string `json:"SerialNumber"`
OSType string `json:"OSType"`
CreationTime string `json:"CreationTime"`
AutoReleaseTime string `json:"AutoReleaseTime"`
Description string `json:"Description"`
InstanceTypeFamily string `json:"InstanceTypeFamily"`
DedicatedInstanceAttribute struct {
Tenancy string `json:"Tenancy"`
Affinity string `json:"Affinity"`
} `json:"DedicatedInstanceAttribute"`
PublicIPAddress struct {
IPAddress []interface{} `json:"IpAddress"`
} `json:"PublicIpAddress"`
GPUSpec string `json:"GPUSpec"`
NetworkInterfaces struct {
NetworkInterface []struct {
Type string `json:"Type"`
PrimaryIPAddress string `json:"PrimaryIpAddress"`
MacAddress string `json:"MacAddress"`
NetworkInterfaceID string `json:"NetworkInterfaceId"`
PrivateIPSets struct {
PrivateIPSet []struct {
PrivateIPAddress string `json:"PrivateIpAddress"`
Primary bool `json:"Primary"`
} `json:"PrivateIpSet"`
} `json:"PrivateIpSets"`
} `json:"NetworkInterface"`
} `json:"NetworkInterfaces"`
SpotPriceLimit float64 `json:"SpotPriceLimit"`
DeviceAvailable bool `json:"DeviceAvailable"`
SaleCycle string `json:"SaleCycle"`
InstanceType string `json:"InstanceType"`
SpotStrategy string `json:"SpotStrategy"`
OSNameEn string `json:"OSNameEn"`
IoOptimized bool `json:"IoOptimized"`
ZoneID string `json:"ZoneId"`
ClusterID string `json:"ClusterId"`
EcsCapacityReservationAttr struct {
CapacityReservationPreference string `json:"CapacityReservationPreference"`
CapacityReservationID string `json:"CapacityReservationId"`
} `json:"EcsCapacityReservationAttr"`
DedicatedHostAttribute struct {
DedicatedHostID string `json:"DedicatedHostId"`
DedicatedHostName string `json:"DedicatedHostName"`
DedicatedHostClusterID string `json:"DedicatedHostClusterId"`
} `json:"DedicatedHostAttribute"`
GPUAmount int `json:"GPUAmount"`
OperationLocks struct {
LockReason []interface{} `json:"LockReason"`
} `json:"OperationLocks"`
InternetMaxBandwidthIn int `json:"InternetMaxBandwidthIn"`
Recyclable bool `json:"Recyclable"`
RegionID string `json:"RegionId"`
CreditSpecification string `json:"CreditSpecification"`
InnerIPAddress struct {
IPAddress []interface{} `json:"IpAddress"`
} `json:"InnerIpAddress,omitempty"`
} `json:"Instance"`
} `json:"Instances"`
TotalCount int `json:"TotalCount"`
NextToken string `json:"NextToken"`
PageSize int `json:"PageSize"`
RequestID string `json:"RequestId"`
PageNumber int `json:"PageNumber"`
}
var (
EcsRabbitmqLabels = []string{"aliecs"}
ecsRabbitmqGaugeVec = map[string]*prometheus.GaugeVec{
"ecs.rabbitmq.cpu": newGaugeVec("ecs_cpu", "ecs rabbit cpu", EcsRabbitmqLabels),
}
ecsRabbitmqMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ecs_rabbitmq_cpu",
Help: "A metric ecs rabbitmq labeled by instanceName, cpu",
},
[]string{"instanceName", "cpu"},
)
)
type exporterEcsRabbitmq struct {
ecsRabbitmqMetrics map[string]*prometheus.GaugeVec
ecsRabbitmqInfo EcsRabbitmqInfo
}
type EcsRabbitmqInfo struct {
instanceId string
instanceName string
cpu int
iOPSRead int
intranetBandwidth int
iOPSWrite int
intranetTX int
bPSRead int
intranetRX int
timeStamp time.Time
internetBandwidth int
internetTX int
internetRX int
bPSWrite int
}
func newExporterEcsRabbitmq() Exporter {
ecsRabbitmqGaugeVecActual := ecsRabbitmqGaugeVec
if len(config.ExcludeMetrics) > 0 {
for _, metric := range config.ExcludeMetrics {
if ecsRabbitmqGaugeVecActual[metric] != nil {
delete(ecsRabbitmqGaugeVecActual, metric)
}
}
}
return &exporterEcsRabbitmq{
ecsRabbitmqMetrics: ecsRabbitmqGaugeVecActual,
ecsRabbitmqInfo: EcsRabbitmqInfo{},
}
}
func (e *exporterEcsRabbitmq) Collect(ctx context.Context, ch chan<- prometheus.Metric) error {
aliEcsRabbitInfos, err := aliEcsRequest(config)
if err != nil {
return err
}
ecsRabbitmqMetric.Reset()
for _, info := range aliEcsRabbitInfos {
e.ecsRabbitmqInfo = info
ecsRabbitmqMetric.WithLabelValues(e.ecsRabbitmqInfo.instanceName, "CPU").Set(float64(e.ecsRabbitmqInfo.cpu))
}
if ch != nil {
ecsRabbitmqMetric.Collect(ch)
for _, gauge := range e.ecsRabbitmqMetrics {
gauge.Collect(ch)
}
}
return nil
}
func aliEcsRequest(config rabbitExporterConfig) ([]EcsRabbitmqInfo, error) {
accessKey := config.AliAccessKey
accessSecret := config.AliAccessSecret
regionId := config.RegionId
client, err := ecs.NewClientWithAccessKey(regionId, accessKey, accessSecret)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Error while create con to ali")
return nil, errors.New("new client with ali ecs is error")
}
requestEcsList := ecs.CreateDescribeInstancesRequest()
requestEcsList.Scheme = "https"
requestEcsList.InstanceName = "rabbit*"
response, err := client.DescribeInstances(requestEcsList)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Error while DescribeInstances request")
return nil, errors.New("error while DescribeInstances request")
}
var listAliEcsResp ListAliEcsResponse
err = json.Unmarshal(response.GetHttpContentBytes(), &listAliEcsResp)
instances := listAliEcsResp.Instances.Instance
if len(instances) < 3 {
log.WithFields(log.Fields{"error": err}).Error("Error rabbitmq instance count")
return nil, errors.New("error rabbitmq instance count")
}
requestEcsMonitor := ecs.CreateDescribeInstanceMonitorDataRequest()
requestEcsMonitor.Scheme = "https"
var ecsRabbitmqInfos []EcsRabbitmqInfo
for _, instance := range instances {
instanceId := instance.InstanceID
instanceName := instance.InstanceName
requestEcsMonitor.InstanceId = instanceId
// 该接口只支持粒度为分钟
requestEcsMonitor.StartTime = time.Now().Add(-2 * time.Minute).UTC().Format(time.RFC3339)
requestEcsMonitor.EndTime = time.Now().UTC().Format(time.RFC3339)
requestEcsMonitor.Period = requests.NewInteger(60)
response, err := client.DescribeInstanceMonitorData(requestEcsMonitor)
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Error API DescribeInstanceMonitorData")
return nil, errors.New("error API DescribeInstanceMonitorData")
}
var aliEcsMonitorResp AliEcsMonitorResponse
err = json.Unmarshal(response.GetHttpContentBytes(), &aliEcsMonitorResp)
l := len(aliEcsMonitorResp.MonitorData.InstanceMonitorData)
if l == 0 {
log.WithFields(log.Fields{"error": err}).Error("Error get ali ecs monitor data")
return nil, errors.New("error get ali ecs monitor data")
}
var ecsRabbitmqInfo EcsRabbitmqInfo
ecsRabbitmqInfo.instanceName = instanceName
ecsRabbitmqInfo.instanceId = instanceId
ecsRabbitmqInfo.iOPSRead = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].IOPSRead
ecsRabbitmqInfo.intranetBandwidth = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].IntranetBandwidth
ecsRabbitmqInfo.iOPSWrite = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].IOPSWrite
ecsRabbitmqInfo.intranetTX = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].IntranetTX
ecsRabbitmqInfo.cpu = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].CPU
ecsRabbitmqInfo.bPSRead = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].BPSRead
ecsRabbitmqInfo.intranetRX = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].IntranetRX
ecsRabbitmqInfo.timeStamp = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].TimeStamp
ecsRabbitmqInfo.internetBandwidth = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].InternetBandwidth
ecsRabbitmqInfo.internetTX = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].InternetTX
ecsRabbitmqInfo.internetRX = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].InternetRX
ecsRabbitmqInfo.bPSWrite = aliEcsMonitorResp.MonitorData.InstanceMonitorData[l-1].BPSWrite
ecsRabbitmqInfos = append(ecsRabbitmqInfos, ecsRabbitmqInfo)
}
return ecsRabbitmqInfos, err
}
func (e exporterEcsRabbitmq) Describe(ch chan<- *prometheus.Desc) {
ecsRabbitmqMetric.Describe(ch)
for _, gauge := range e.ecsRabbitmqMetrics {
gauge.Describe(ch)
}
}