yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/monitor.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bingocloud
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type MetricOutput struct {
    28  	Datapoints Datapoints
    29  	ObjName    string
    30  	Period     int64
    31  }
    32  
    33  type DatapointMember struct {
    34  	Average     float64
    35  	Maximum     float64
    36  	Minimum     float64
    37  	SampleCount float64
    38  	Sum         float64
    39  	Timestamp   time.Time
    40  	Unit        string
    41  }
    42  
    43  func (self DatapointMember) GetValue() float64 {
    44  	return self.Average + self.Maximum + self.Minimum + self.Sum
    45  }
    46  
    47  type Datapoints struct {
    48  	Member []DatapointMember
    49  }
    50  
    51  func (self *SBingoCloudClient) DescribeMetricList(ns, metricNm, dimensionName, dimensionValue string, since time.Time, until time.Time) (*MetricOutput, error) {
    52  	params := map[string]string{}
    53  	params["Namespace"] = ns
    54  	params["MetricName"] = metricNm
    55  	params["Dimensions.member.1.Name"] = dimensionName
    56  	params["Dimensions.member.1.Value"] = dimensionValue
    57  	params["StartTime"] = since.Format(time.RFC3339)
    58  	params["EndTime"] = until.Format(time.RFC3339)
    59  	params["Statistics.member.1"] = "Average"
    60  	params["Period"] = "60"
    61  	resp, err := self.invoke("GetMetricStatistics", params)
    62  	if err != nil {
    63  		return nil, errors.Wrap(err, "GetMetricStatistics err")
    64  	}
    65  	ret := &MetricOutput{}
    66  	return ret, resp.Unmarshal(ret, "GetMetricStatisticsResult")
    67  }
    68  
    69  func (self *SBingoCloudClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    70  	if len(opts.ResourceId) == 0 {
    71  		return nil, fmt.Errorf("missing resourceId")
    72  	}
    73  	switch opts.ResourceType {
    74  	case cloudprovider.METRIC_RESOURCE_TYPE_SERVER:
    75  		return self.GetEcsMetrics(opts)
    76  	case cloudprovider.METRIC_RESOURCE_TYPE_HOST:
    77  		return self.GetHostMetrics(opts)
    78  	default:
    79  		return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", opts.ResourceType)
    80  	}
    81  }
    82  
    83  func (self *SBingoCloudClient) GetEcsMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    84  	ret := []cloudprovider.MetricValues{}
    85  	for metricType, metricName := range map[cloudprovider.TMetricType]string{
    86  		cloudprovider.VM_METRIC_TYPE_CPU_USAGE:          "CPUUtilization",
    87  		cloudprovider.VM_METRIC_TYPE_MEM_USAGE:          "MemeryUsage",
    88  		cloudprovider.VM_METRIC_TYPE_NET_BPS_RX:         "NetworkIn",
    89  		cloudprovider.VM_METRIC_TYPE_NET_BPS_TX:         "NetworkOut",
    90  		cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS:   "DiskReadBytes",
    91  		cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS:  "DiskWriteBytes",
    92  		cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS:  "DiskReadOps",
    93  		cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS: "DiskWriteOps",
    94  	} {
    95  		data, err := self.DescribeMetricList("AWS/EC2", metricName, "InstanceId", opts.ResourceId, opts.StartTime, opts.EndTime)
    96  		if err != nil {
    97  			log.Errorf("DescribeMetricList error: %v", err)
    98  			continue
    99  		}
   100  		metric := cloudprovider.MetricValues{}
   101  		metric.Id = opts.ResourceId
   102  		metric.MetricType = metricType
   103  		for _, value := range data.Datapoints.Member {
   104  			metricValue := cloudprovider.MetricValue{}
   105  			metricValue.Timestamp = value.Timestamp
   106  			metricValue.Value = value.GetValue()
   107  			metric.Values = append(metric.Values, metricValue)
   108  		}
   109  		ret = append(ret, metric)
   110  	}
   111  	return ret, nil
   112  }
   113  
   114  func (self *SBingoCloudClient) GetHostMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   115  	ret := []cloudprovider.MetricValues{}
   116  	for metricType, metricName := range map[cloudprovider.TMetricType]string{
   117  		cloudprovider.VM_METRIC_TYPE_CPU_USAGE:          "CPUUtilization",
   118  		cloudprovider.VM_METRIC_TYPE_MEM_USAGE:          "MemeryUsage",
   119  		cloudprovider.VM_METRIC_TYPE_NET_BPS_RX:         "NetworkIn",
   120  		cloudprovider.VM_METRIC_TYPE_NET_BPS_TX:         "NetworkOut",
   121  		cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS:   "DiskReadBytes",
   122  		cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS:  "DiskWriteBytes",
   123  		cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS:  "DiskReadOps",
   124  		cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS: "DiskWriteOps",
   125  	} {
   126  		data, err := self.DescribeMetricList("AWS/HOST", metricName, "HostId", opts.ResourceId, opts.StartTime, opts.EndTime)
   127  		if err != nil {
   128  			log.Errorf("DescribeMetricList error: %v", err)
   129  			continue
   130  		}
   131  		metric := cloudprovider.MetricValues{}
   132  		metric.Id = opts.ResourceId
   133  		metric.MetricType = metricType
   134  		for _, value := range data.Datapoints.Member {
   135  			metricValue := cloudprovider.MetricValue{}
   136  			metricValue.Timestamp = value.Timestamp
   137  			metricValue.Value = value.GetValue()
   138  			metric.Values = append(metric.Values, metricValue)
   139  		}
   140  		ret = append(ret, metric)
   141  	}
   142  	return ret, nil
   143  
   144  }