yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type MetricDataValue struct {
    28  	DoubleValue float64
    29  	Int64Value  float64
    30  }
    31  
    32  func (self *MetricDataValue) GetValue() float64 {
    33  	return self.DoubleValue + self.Int64Value
    34  }
    35  
    36  type MetricData struct {
    37  	Resource struct {
    38  		Labels struct {
    39  			InstanceId string
    40  		}
    41  	}
    42  	MetricKind string
    43  	ValueType  string
    44  	Points     []struct {
    45  		Interval struct {
    46  			StartTime time.Time
    47  			EndTime   time.Time
    48  		}
    49  		Value MetricDataValue
    50  	}
    51  }
    52  
    53  func (self *SGoogleClient) GetMonitorData(metricName string, since time.Time, until time.Time) (*jsonutils.JSONArray, error) {
    54  	params := map[string]string{
    55  		"filter":             fmt.Sprintf(`metric.type="%s"`, metricName),
    56  		"interval.startTime": since.Format(time.RFC3339),
    57  		"interval.endTime":   until.Format(time.RFC3339),
    58  	}
    59  	resource := fmt.Sprintf("%s/%s/%s", "projects", self.projectId, "timeSeries")
    60  	timeSeries, err := self.monitorListAll(resource, params)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return timeSeries, nil
    65  }
    66  
    67  func (self *SGoogleClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    68  	switch opts.ResourceType {
    69  	case cloudprovider.METRIC_RESOURCE_TYPE_SERVER:
    70  		return self.GetEcsMetrics(opts)
    71  	default:
    72  		return nil, errors.Wrapf(cloudprovider.ErrNotImplemented, "%s", opts.ResourceType)
    73  	}
    74  }
    75  
    76  func (self *SGoogleClient) GetEcsMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    77  	metricName := ""
    78  	switch opts.MetricType {
    79  	case cloudprovider.VM_METRIC_TYPE_CPU_USAGE:
    80  		metricName = "compute.googleapis.com/instance/cpu/utilization"
    81  	case cloudprovider.VM_METRIC_TYPE_NET_BPS_RX:
    82  		metricName = "compute.googleapis.com/instance/network/received_bytes_count"
    83  	case cloudprovider.VM_METRIC_TYPE_NET_BPS_TX:
    84  		metricName = "compute.googleapis.com/instance/network/sent_bytes_count"
    85  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_BPS:
    86  		metricName = "compute.googleapis.com/instance/disk/read_bytes_count"
    87  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_BPS:
    88  		metricName = "compute.googleapis.com/instance/disk/write_bytes_count"
    89  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_READ_IOPS:
    90  		metricName = "compute.googleapis.com/instance/disk/read_ops_count"
    91  	case cloudprovider.VM_METRIC_TYPE_DISK_IO_WRITE_IOPS:
    92  		metricName = "compute.googleapis.com/instance/disk/write_ops_count"
    93  	default:
    94  		return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", opts.MetricType)
    95  	}
    96  	metrics, err := self.GetMonitorData(metricName, opts.StartTime, opts.EndTime)
    97  	if err != nil {
    98  		return nil, errors.Wrapf(err, "GetMonitorData")
    99  	}
   100  	data := []MetricData{}
   101  	err = metrics.Unmarshal(&data)
   102  	if err != nil {
   103  		return nil, errors.Wrapf(err, "metrics.Unmarshal")
   104  	}
   105  	ret := []cloudprovider.MetricValues{}
   106  	for i := range data {
   107  		value := cloudprovider.MetricValues{}
   108  		value.Id = data[i].Resource.Labels.InstanceId
   109  		value.MetricType = opts.MetricType
   110  		for j := range data[i].Points {
   111  			metricValue := cloudprovider.MetricValue{}
   112  			metricValue.Timestamp = data[i].Points[j].Interval.StartTime
   113  			metricValue.Value = data[i].Points[j].Value.GetValue()
   114  			value.Values = append(value.Values, metricValue)
   115  		}
   116  		ret = append(ret, value)
   117  	}
   118  	return ret, nil
   119  }