From c1d0f7d41385bb3606ff8001f958374415cf7402 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Mon, 13 Sep 2021 22:30:42 +0800 Subject: [PATCH] feat(cloudmon): support classic vm metrics --- .../collectors/azuremon/azureservice.go | 29 +++++++++---- .../collectors/azuremon/azureutils.go | 41 +++++++++++++++---- pkg/cloudmon/collectors/common/basemon.go | 6 +-- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/pkg/cloudmon/collectors/azuremon/azureservice.go b/pkg/cloudmon/collectors/azuremon/azureservice.go index 1054203952..10f6819308 100644 --- a/pkg/cloudmon/collectors/azuremon/azureservice.go +++ b/pkg/cloudmon/collectors/azuremon/azureservice.go @@ -20,6 +20,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/cloudmon/collectors/common" "yunion.io/x/onecloud/pkg/cloudprovider" @@ -41,37 +42,49 @@ func (self *SAzureCloudReport) collectRegionMetricOfHost(region cloudprovider.IC return err } for _, server := range servers { + srvId, _ := server.GetString("id") + srvName, _ := server.GetString("name") + srvPrefix := srvId + "/" + "srvName" external_id, err := server.GetString("external_id") if err != nil { continue } + classicKey := "microsoft.classiccompute/virtualmachines" + ns := "Microsoft.Compute/virtualMachines" + metricSpecs := azureMetricSpecs + if strings.Contains(strings.ToLower(external_id), classicKey) { + ns = classicKey + metricSpecs = azureClassicMetricsSpec + } metricNameArr := make([]string, 0) - for metricName, _ := range azureMetricSpecs { + for metricName := range metricSpecs { metricNameArr = append(metricNameArr, metricName) } metricNames := strings.Join(metricNameArr, ",") - rtnMetrics, err := azureReg.GetMonitorData(metricNames, "Microsoft.Compute/virtualMachines", external_id, since, until) + azureReg.GetClient().Debug(true) + rtnMetrics, err := azureReg.GetMonitorData(metricNames, ns, external_id, since, until) if err != nil { - log.Errorln(err) + log.Errorf("get metrics for server %s error: %v", srvPrefix, err) continue } if rtnMetrics == nil || rtnMetrics.Value == nil { + log.Warningf("server %s metrics is nil", srvPrefix) continue } - for metricName, influxDbSpecs := range azureMetricSpecs { + for metricName, influxDbSpecs := range metricSpecs { for _, value := range *rtnMetrics.Value { if value.Name.LocalizedValue != nil { - if metricName == *(value.Name.LocalizedValue) { + if metricName == *(value.Name.LocalizedValue) || (value.Name.Value != nil && *value.Name.Value == metricName) { metric, err := common.FillVMCapacity(server.(*jsonutils.JSONDict)) if err != nil { - return err + return errors.Wrapf(err, "fill vm %q capacity", srvPrefix) } dataList = append(dataList, metric) if value.Timeseries != nil { for _, timeserie := range *value.Timeseries { serverMetric, err := self.collectMetricFromThisServer(server, timeserie, influxDbSpecs) if err != nil { - return err + return errors.Wrapf(err, "collect metrics from server %q", srvPrefix) } dataList = append(dataList, serverMetric...) } @@ -82,7 +95,7 @@ func (self *SAzureCloudReport) collectRegionMetricOfHost(region cloudprovider.IC } err = common.SendMetrics(self.Session, dataList, self.Args.Debug, "") if err != nil { - log.Errorln(err) + log.Errorf("send %q metrics error: %v", srvName, err) } dataList = dataList[:0] } diff --git a/pkg/cloudmon/collectors/azuremon/azureutils.go b/pkg/cloudmon/collectors/azuremon/azureutils.go index 4acc91a36c..527b0aeb3c 100644 --- a/pkg/cloudmon/collectors/azuremon/azureutils.go +++ b/pkg/cloudmon/collectors/azuremon/azureutils.go @@ -119,13 +119,38 @@ const ( KEY_USABLE = "usable" ) -//multiCloud查询指标列表组装 +// multiCloud查询指标列表组装 +const ( + MetricKeyPercentageCPU = "Percentage CPU" + MetricKeyNetworkInTotal = "Network In Total" + MetricKeyNetworkOutTotal = "Network Out Total" + MetricKeyDiskReadBytes = "Disk Read Bytes" + MetricKeyDiskWriteBytes = "Disk Write Bytes" + MetricKeyDiskReadOPS = "Disk Read Operations/Sec" + MetricKeyDiskWriteOPS = "Disk Write Operations/Sec" + + MetricKeyClassicNetworkIn = "Network In" + MetricKeyClassicNetworkOut = "Network Out" + MetricKeyClassicDiskReadBPS = "Disk Read Bytes/Sec" + MetricKeyClassicDiskWriteBPS = "Disk Write Bytes/Sec" +) + var azureMetricSpecs = map[string][]string{ - "Percentage CPU": {DEFAULT_STATISTICS, UNIT_PERCENT, INFLUXDB_FIELD_CPU_USAGE}, - "Network In Total": {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_NET_BPS_RX}, - "Network Out Total": {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_NET_BPS_TX}, - "Disk Read Bytes": {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_DISK_READ_BPS}, - "Disk Write Bytes": {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_DISK_WRITE_BPS}, - "Disk Read Operations/Sec": {DEFAULT_STATISTICS, UNIT_COUNT_SEC, INFLUXDB_FIELD_DISK_READ_IOPS}, - "Disk Write Operations/Sec": {DEFAULT_STATISTICS, UNIT_COUNT_SEC, INFLUXDB_FIELD_DISK_WRITE_BPS}, + MetricKeyPercentageCPU: {DEFAULT_STATISTICS, UNIT_PERCENT, INFLUXDB_FIELD_CPU_USAGE}, + MetricKeyNetworkInTotal: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_NET_BPS_RX}, + MetricKeyNetworkOutTotal: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_NET_BPS_TX}, + MetricKeyDiskReadBytes: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_DISK_READ_BPS}, + MetricKeyDiskWriteBytes: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_DISK_WRITE_BPS}, + MetricKeyDiskReadOPS: {DEFAULT_STATISTICS, UNIT_COUNT_SEC, INFLUXDB_FIELD_DISK_READ_IOPS}, + MetricKeyDiskWriteOPS: {DEFAULT_STATISTICS, UNIT_COUNT_SEC, INFLUXDB_FIELD_DISK_WRITE_IOPS}, +} + +var azureClassicMetricsSpec = map[string][]string{ + MetricKeyPercentageCPU: {DEFAULT_STATISTICS, UNIT_PERCENT, INFLUXDB_FIELD_CPU_USAGE}, + MetricKeyClassicNetworkIn: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_NET_BPS_RX}, + MetricKeyClassicNetworkOut: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_NET_BPS_TX}, + MetricKeyClassicDiskReadBPS: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_DISK_READ_BPS}, + MetricKeyClassicDiskWriteBPS: {DEFAULT_STATISTICS, UNIT_MEM, INFLUXDB_FIELD_DISK_WRITE_BPS}, + MetricKeyDiskReadOPS: {DEFAULT_STATISTICS, UNIT_COUNT_SEC, INFLUXDB_FIELD_DISK_READ_IOPS}, + MetricKeyDiskWriteOPS: {DEFAULT_STATISTICS, UNIT_COUNT_SEC, INFLUXDB_FIELD_DISK_WRITE_IOPS}, } diff --git a/pkg/cloudmon/collectors/common/basemon.go b/pkg/cloudmon/collectors/common/basemon.go index 14b70b417e..72a90c4dd4 100644 --- a/pkg/cloudmon/collectors/common/basemon.go +++ b/pkg/cloudmon/collectors/common/basemon.go @@ -129,14 +129,14 @@ func (self *CloudReportBase) InitProviderInstance() (cloudprovider.ICloudProvide if err == nil { err := options.Unmarshal(&endpoints) if err != nil { - log.Errorf("Unmarshal SApsaraEndpoints err:%v", err) + log.Errorf("Unmarshal SApsaraEndpoints err: %v", err) } err = options.Unmarshal(&hwendpoints) if err != nil { - log.Errorf("Unmarshal SHCSOEndpoints err:%v", err) + log.Errorf("Unmarshal SHCSOEndpoints err: %v", err) } } else { - log.Errorf("get cloudAccout options err:%v", err) + log.Errorf("get cloudAccout options err: %v", err) } cfg := cloudprovider.ProviderConfig{ Id: self.SProvider.Id,