forked from yunionio/cloudmux
/
monitor.go
193 lines (180 loc) · 6.38 KB
/
monitor.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
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aws
import (
"fmt"
"strings"
"time"
"github.com/nyl1001/pkg/errors"
"yunion.io/x/log"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
func (self *SAwsClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
if len(opts.ResourceId) == 0 {
return nil, fmt.Errorf("missing resource id")
}
switch opts.ResourceType {
case cloudprovider.METRIC_RESOURCE_TYPE_SERVER:
return self.GetEcsMetrics(opts)
case cloudprovider.METRIC_RESOURCE_TYPE_RDS:
return self.GetRdsMetrics(opts)
case cloudprovider.METRIC_RESOURCE_TYPE_REDIS:
return self.GetRedisMetrics(opts)
default:
return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "%s", opts.ResourceType)
}
}
func (self *SAwsClient) GetEcsMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
ret := []cloudprovider.MetricValues{}
for metricType, metricNames := range map[cloudprovider.TMetricType]map[string]string{
cloudprovider.VM_METRIC_TYPE_CPU_USAGE: {
"CPUUtilization": "",
},
cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS: {
"DiskReadOps": "",
"EBSReadOps": cloudprovider.METRIC_TAG_TYPE_DISK_TYPE + ":" + cloudprovider.METRIC_TAG_TYPE_DISK_TYPE,
},
cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS: {
"DiskWriteOps": "",
"EBSWriteOps": cloudprovider.METRIC_TAG_TYPE_DISK_TYPE + ":" + cloudprovider.METRIC_TAG_TYPE_DISK_TYPE,
},
cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS: {
"DiskReadBytes": "",
"EBSReadBytes": cloudprovider.METRIC_TAG_TYPE_DISK_TYPE + ":" + cloudprovider.METRIC_TAG_TYPE_DISK_TYPE,
},
cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS: {
"DiskWriteBytes": "",
"EBSWriteBytes": cloudprovider.METRIC_TAG_TYPE_DISK_TYPE + ":" + cloudprovider.METRIC_TAG_TYPE_DISK_TYPE,
},
cloudprovider.VM_METRIC_TYPE_NET_BPS_RX: {
"NetworkIn": "",
},
cloudprovider.VM_METRIC_TYPE_NET_BPS_TX: {
"NetworkOut": "",
},
} {
part, err := self.getMetrics(opts.RegionExtId, "AWS/EC2", metricType, metricNames, "InstanceId", opts.ResourceId, opts.StartTime, opts.EndTime)
if err != nil {
log.Errorf("getMetrics error: %v", err)
continue
}
ret = append(ret, part...)
}
return ret, nil
}
type Datapoint struct {
Average float64 `xml:"Average"`
ExtendedStatistics struct {
Key string `xml:"Key"`
Value string `xml:"Value"`
} `xml:"ExtendedStatistics>entry"`
Maximum float64 `xml:"Maximum"`
Minimum float64 `xml:"Minimum"`
SampleCount float64 `xml:"SampleCount"`
Sum float64 `xml:"Sum"`
Timestamp time.Time `xml:"Timestamp"`
Unit string `xml:"Unit"`
}
func (self Datapoint) GetValue() float64 {
return self.Average + self.Average + self.Minimum + self.Sum
}
type Datapoints struct {
Datapoints []Datapoint `xml:"Datapoints>member"`
Label string `xml:"Label"`
}
func (self *SAwsClient) getMetrics(regionId, ns string, metricType cloudprovider.TMetricType, metrics map[string]string, dimensionName, dimensionValue string, start, end time.Time) ([]cloudprovider.MetricValues, error) {
result := []cloudprovider.MetricValues{}
for metricName, tagValue := range metrics {
params := map[string]string{
"EndTime": end.Format(time.RFC3339),
"MetricName": metricName,
"Dimensions.member.1.Name": dimensionName,
"Dimensions.member.1.Value": dimensionValue,
"Namespace": ns,
"Period": "1",
"StartTime": start.Format(time.RFC3339),
"Statistics.member.1": "Average",
}
ret := Datapoints{}
err := self.monitorRequest(regionId, "GetMetricStatistics", params, &ret)
if err != nil {
log.Errorf("GetMetricStatistics error: %v", err)
continue
}
metric := cloudprovider.MetricValues{}
metric.Id = dimensionValue
metric.MetricType = metricType
tags := map[string]string{}
idx := strings.Index(tagValue, ":")
if idx > 0 {
tags[tagValue[:idx]] = tagValue[idx+1:]
}
for _, data := range ret.Datapoints {
metricValue := cloudprovider.MetricValue{}
metricValue.Tags = tags
metricValue.Timestamp = data.Timestamp
metricValue.Value = data.GetValue()
metric.Values = append(metric.Values, metricValue)
}
result = append(result, metric)
}
return result, nil
}
func (self *SAwsClient) GetRdsMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
ret := []cloudprovider.MetricValues{}
for metricType, metricNames := range map[cloudprovider.TMetricType]map[string]string{
cloudprovider.RDS_METRIC_TYPE_CPU_USAGE: {
"CPUUtilization": "",
},
cloudprovider.RDS_METRIC_TYPE_NET_BPS_RX: {
"NetworkReceiveThroughput": "",
},
cloudprovider.RDS_METRIC_TYPE_NET_BPS_TX: {
"NetworkTransmitThroughput": "",
},
cloudprovider.RDS_METRIC_TYPE_CONN_COUNT: {
"DatabaseConnections": "",
},
} {
part, err := self.getMetrics(opts.RegionExtId, "AWS/RDS", metricType, metricNames, "DBInstanceIdentifier", opts.ResourceId, opts.StartTime, opts.EndTime)
if err != nil {
log.Errorf("getMetrics error: %v", err)
continue
}
ret = append(ret, part...)
}
return ret, nil
}
func (self *SAwsClient) GetRedisMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
ret := []cloudprovider.MetricValues{}
for metricType, metricNames := range map[cloudprovider.TMetricType]map[string]string{
cloudprovider.REDIS_METRIC_TYPE_CPU_USAGE: {
"CPUUtilization": "",
},
cloudprovider.REDIS_METRIC_TYPE_USED_CONN: {
"CurrConnections": "",
},
cloudprovider.REDIS_METRIC_TYPE_CACHE_EXP_KEYS: {
"Reclaimed": "",
},
} {
part, err := self.getMetrics(opts.RegionExtId, "AWS/ElastiCache", metricType, metricNames, "CacheClusterId", opts.ResourceId, opts.StartTime, opts.EndTime)
if err != nil {
log.Errorf("getMetrics error: %v", err)
continue
}
ret = append(ret, part...)
}
return ret, nil
}