yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/log"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/modules"
    25  )
    26  
    27  func (r *SRegion) GetMetrics() ([]modules.SMetricMeta, error) {
    28  	return r.ecsClient.CloudEye.ListMetrics()
    29  }
    30  
    31  func (r *SRegion) GetMetricsData(metrics []modules.SMetricMeta, since time.Time, until time.Time) ([]modules.SMetricData, error) {
    32  	return r.ecsClient.CloudEye.GetMetricsData(metrics, since, until)
    33  }
    34  
    35  func (self *SHuaweiClient) getModelartsPoolMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
    36  	resp, err := self.modelartsPoolMonitor(opts.ResourceId, nil)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	metricData := []SModelartsMetric{}
    41  	err = resp.Unmarshal(&metricData, "metrics")
    42  	if err != nil {
    43  		return nil, errors.Wrapf(err, "resp.Unmarshal")
    44  	}
    45  	result := []cloudprovider.MetricValues{}
    46  	for i := range metricData {
    47  		isMB := false
    48  		if metricData[i].Datapoints[0].Unit == "Megabytes" {
    49  			isMB = true
    50  			metricData[i].Datapoints[0].Unit = "Bytes"
    51  		}
    52  		ret := cloudprovider.MetricValues{
    53  			Id:     opts.ResourceId,
    54  			Unit:   metricData[i].Datapoints[0].Unit,
    55  			Values: []cloudprovider.MetricValue{},
    56  		}
    57  		tags := map[string]string{}
    58  		switch metricData[i].Metric.MetricName {
    59  		case "cpuUsage":
    60  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_CPU_USAGE
    61  		case "memUsedRate":
    62  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_MEM_USAGE
    63  		case "gpuUtil":
    64  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_GPU_UTIL
    65  		case "gpuMemUsage":
    66  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_GPU_MEM_USAGE
    67  		case "npuUtil":
    68  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_NPU_UTIL
    69  		case "npuMemUsage":
    70  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_NPU_MEM_USAGE
    71  		case "diskAvailableCapacity":
    72  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_DISK_AVAILABLE_CAPACITY
    73  		case "diskCapacity":
    74  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_DISK_CAPACITY
    75  		case "diskUsedRate":
    76  			ret.MetricType = cloudprovider.MODELARTS_POOL_METRIC_TYPE_DISK_USAGE
    77  		default:
    78  			log.Warningf("invalid metricName %s for %s %s", metricData[i].Metric.MetricName, opts.ResourceType, opts.ResourceId)
    79  			continue
    80  		}
    81  		for _, value := range metricData[i].Datapoints {
    82  			if isMB {
    83  				value.Statistics[0].Value *= 1024
    84  			}
    85  			if value.Statistics[0].Value == -1 {
    86  				value.Statistics[0].Value = 0
    87  			}
    88  			metricValue := cloudprovider.MetricValue{
    89  				Value:     value.Statistics[0].Value,
    90  				Timestamp: time.UnixMilli(value.Timestamp),
    91  				Tags:      tags,
    92  			}
    93  			ret.Values = append(ret.Values, metricValue)
    94  		}
    95  		result = append(result, ret)
    96  	}
    97  	return result, nil
    98  }
    99  
   100  func (self *SHuaweiClient) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   101  	switch opts.ResourceType {
   102  	case cloudprovider.METRIC_RESOURCE_TYPE_MODELARTS_POOL:
   103  		return self.getModelartsPoolMetrics(opts)
   104  	default:
   105  		return nil, errors.Wrapf(cloudprovider.ErrNotSupported, "%s", opts.ResourceType)
   106  	}
   107  }