forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
144 lines (131 loc) · 5.07 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
// 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 bingocloud
import (
"fmt"
"time"
"github.com/nyl1001/pkg/errors"
"yunion.io/x/log"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type MetricOutput struct {
Datapoints Datapoints
ObjName string
Period int64
}
type DatapointMember struct {
Average float64
Maximum float64
Minimum float64
SampleCount float64
Sum float64
Timestamp time.Time
Unit string
}
func (self DatapointMember) GetValue() float64 {
return self.Average + self.Maximum + self.Minimum + self.Sum
}
type Datapoints struct {
Member []DatapointMember
}
func (self *SBingoCloudClient) DescribeMetricList(ns, metricNm, dimensionName, dimensionValue string, since time.Time, until time.Time) (*MetricOutput, error) {
params := map[string]string{}
params["Namespace"] = ns
params["MetricName"] = metricNm
params["Dimensions.member.1.Name"] = dimensionName
params["Dimensions.member.1.Value"] = dimensionValue
params["StartTime"] = since.UTC().Format(time.RFC3339)
params["EndTime"] = until.UTC().Format(time.RFC3339)
params["Statistics.member.1"] = "Average"
params["Period"] = "60"
resp, err := self.invoke("GetMetricStatistics", params)
if err != nil {
return nil, errors.Wrap(err, "GetMetricStatistics err")
}
ret := &MetricOutput{}
return ret, resp.Unmarshal(ret, "GetMetricStatisticsResult")
}
func (self *SBingoCloudClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
if len(opts.ResourceId) == 0 {
return nil, fmt.Errorf("missing resourceId")
}
switch opts.ResourceType {
case cloudprovider.METRIC_RESOURCE_TYPE_SERVER:
return self.GetEcsMetrics(opts)
case cloudprovider.METRIC_RESOURCE_TYPE_HOST:
return self.GetHostMetrics(opts)
default:
return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", opts.ResourceType)
}
}
func (self *SBingoCloudClient) GetEcsMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
var ret []cloudprovider.MetricValues
for metricType, metricName := range map[cloudprovider.TMetricType]string{
cloudprovider.VM_METRIC_TYPE_CPU_USAGE: "CPUUtilization",
cloudprovider.VM_METRIC_TYPE_MEM_USAGE: "MemoryUsage",
cloudprovider.VM_METRIC_TYPE_NET_BPS_RX: "NetworkIn",
cloudprovider.VM_METRIC_TYPE_NET_BPS_TX: "NetworkOut",
cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS: "DiskReadBytes",
cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS: "DiskWriteBytes",
cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS: "DiskReadOps",
cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS: "DiskWriteOps",
} {
data, err := self.DescribeMetricList("AWS/EC2", metricName, "InstanceId", opts.ResourceId, opts.StartTime, opts.EndTime)
if err != nil {
log.Errorf("DescribeMetricList error: %v", err)
continue
}
metric := cloudprovider.MetricValues{}
metric.Id = opts.ResourceId
metric.MetricType = metricType
for _, value := range data.Datapoints.Member {
metricValue := cloudprovider.MetricValue{}
metricValue.Timestamp = value.Timestamp
metricValue.Value = value.GetValue()
metric.Values = append(metric.Values, metricValue)
}
ret = append(ret, metric)
}
return ret, nil
}
func (self *SBingoCloudClient) GetHostMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
var ret []cloudprovider.MetricValues
for metricType, metricName := range map[cloudprovider.TMetricType]string{
cloudprovider.HOST_METRIC_TYPE_CPU_USAGE: "CPUUtilization",
cloudprovider.HOST_METRIC_TYPE_MEM_USAGE: "MemeryUsage",
cloudprovider.HOST_METRIC_TYPE_NET_BPS_RX: "NetworkIn",
cloudprovider.HOST_METRIC_TYPE_NET_BPS_TX: "NetworkOut",
cloudprovider.HOST_METRIC_TYPE_DISK_IO_READ_BPS: "DiskReadBytes",
cloudprovider.HOST_METRIC_TYPE_DISK_IO_WRITE_BPS: "DiskWriteBytes",
cloudprovider.HOST_METRIC_TYPE_DISK_IO_READ_IOPS: "DiskReadOps",
cloudprovider.HOST_METRIC_TYPE_DISK_IO_WRITE_IOPS: "DiskWriteOps",
} {
data, err := self.DescribeMetricList("AWS/HOST", metricName, "HostId", opts.ResourceId, opts.StartTime, opts.EndTime)
if err != nil {
log.Errorf("DescribeMetricList error: %v", err)
continue
}
metric := cloudprovider.MetricValues{}
metric.Id = opts.ResourceId
metric.MetricType = metricType
for _, value := range data.Datapoints.Member {
metricValue := cloudprovider.MetricValue{}
metricValue.Timestamp = value.Timestamp
metricValue.Value = value.GetValue()
metric.Values = append(metric.Values, metricValue)
}
ret = append(ret, metric)
}
return ret, nil
}