yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/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 zstack
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  )
    25  
    26  type SDataPoint struct {
    27  	DataPoints []DataPoint `json:"data"`
    28  }
    29  
    30  type DataPoint struct {
    31  	Value  float64 `json:"value"`
    32  	Time   int64   `json:"time"`
    33  	Labels Label   `json:"labels"`
    34  }
    35  
    36  type Label struct {
    37  	VMUuid   string `json:"VMUuid"`
    38  	HostUuid string `json:"HostUuid"`
    39  }
    40  
    41  func (self *SZStackClient) GetMonitorData(name string, namespace string, since time.Time, until time.Time) ([]DataPoint, error) {
    42  	datas := SDataPoint{}
    43  	param := jsonutils.NewDict()
    44  	param.Add(jsonutils.NewString(namespace), "namespace")
    45  	param.Add(jsonutils.NewString(name), "metricName")
    46  	param.Add(jsonutils.NewString("60"), "period")
    47  	param.Add(jsonutils.NewInt(since.Unix()), "startTime")
    48  	param.Add(jsonutils.NewInt(until.Unix()), "endTime")
    49  	rep, err := self.getMonitor("zwatch/metrics", param)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return datas.DataPoints, rep.Unmarshal(&datas)
    54  }
    55  
    56  func (self *SZStackClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    57  	switch opts.ResourceType {
    58  	case cloudprovider.METRIC_RESOURCE_TYPE_SERVER:
    59  		return self.GetEcsMetrics(opts)
    60  	case cloudprovider.METRIC_RESOURCE_TYPE_HOST:
    61  		return self.GetHostMetrics(opts)
    62  	default:
    63  		return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "%s", opts.ResourceType)
    64  	}
    65  }
    66  
    67  func (self *SZStackClient) GetHostMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    68  	ns, metricName := "ZStack/Host", ""
    69  	switch opts.MetricType {
    70  	case cloudprovider.VM_METRIC_TYPE_CPU_USAGE:
    71  		metricName = "CPUAverageUsedUtilization"
    72  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS:
    73  		metricName = "DiskReadBytes"
    74  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS:
    75  		metricName = "DiskWriteBytes"
    76  	case cloudprovider.VM_METRIC_TYPE_NET_BPS_RX:
    77  		metricName = "NetworkInBytes"
    78  	case cloudprovider.VM_METRIC_TYPE_NET_BPS_TX:
    79  		metricName = "NetworkOutBytes"
    80  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS:
    81  		metricName = "DiskReadOps"
    82  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS:
    83  		metricName = "DiskWriteOps"
    84  	default:
    85  		return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", opts.MetricType)
    86  	}
    87  	data, err := self.GetMonitorData(metricName, ns, opts.StartTime, opts.EndTime)
    88  	if err != nil {
    89  		return nil, errors.Wrapf(err, "GetMonitorData")
    90  	}
    91  	ret := []cloudprovider.MetricValues{}
    92  	for i := range data {
    93  		metric := cloudprovider.MetricValues{}
    94  		metric.MetricType = opts.MetricType
    95  		metric.Id = data[i].Labels.HostUuid
    96  		metric.Values = append(metric.Values, cloudprovider.MetricValue{
    97  			Timestamp: time.Unix(data[i].Time, 0),
    98  			Value:     data[i].Value,
    99  		})
   100  		ret = append(ret, metric)
   101  	}
   102  	return ret, nil
   103  }
   104  
   105  func (self *SZStackClient) GetEcsMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   106  	ns, metricName := "ZStack/VM", ""
   107  	switch opts.MetricType {
   108  	case cloudprovider.VM_METRIC_TYPE_CPU_USAGE:
   109  		metricName = "CPUAverageUsedUtilization"
   110  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS:
   111  		metricName = "DiskReadBytes"
   112  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS:
   113  		metricName = "DiskWriteBytes"
   114  	case cloudprovider.VM_METRIC_TYPE_NET_BPS_RX:
   115  		metricName = "NetworkInBytes"
   116  	case cloudprovider.VM_METRIC_TYPE_NET_BPS_TX:
   117  		metricName = "NetworkOutBytes"
   118  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS:
   119  		metricName = "DiskReadOps"
   120  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS:
   121  		metricName = "DiskWriteOps"
   122  	default:
   123  		return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", opts.MetricType)
   124  	}
   125  	data, err := self.GetMonitorData(metricName, ns, opts.StartTime, opts.EndTime)
   126  	if err != nil {
   127  		return nil, errors.Wrapf(err, "GetMonitorData")
   128  	}
   129  	ret := []cloudprovider.MetricValues{}
   130  	for i := range data {
   131  		metric := cloudprovider.MetricValues{}
   132  		metric.MetricType = opts.MetricType
   133  		metric.Id = data[i].Labels.VMUuid
   134  		metric.Values = append(metric.Values, cloudprovider.MetricValue{
   135  			Timestamp: time.Unix(data[i].Time, 0),
   136  			Value:     data[i].Value,
   137  		})
   138  		ret = append(ret, metric)
   139  	}
   140  	return ret, nil
   141  }