108

阿里云ECS-云监控SDK-使用指南

 4 years ago
source link: https://studygolang.com/articles/20207?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

0.阅读可得:

(1).了解到ECS-SDK 这个东西

(2).可以通过ECS-SDK,获取ECS云主机的监控数据.包括CPU使用情况,带宽占用情况,磁盘IOPS情况等等.

1. 文档地址:

https://help.aliyun.com/document_detail/51936.html?spm=a2c4g.11186623.6.690.4f8e659d9ximRr

2.SDK地址:

https://github.com/aliyun/alibaba-cloud-sdk-go

3.官方调试地址:

https://api.aliyun.com/?spm=a2c4g.11186623.2.14.64c57751hTbqTL#/?product=Cms&api=DescribeMetricList

4.可以满足需求

(1).获取ECS的云监控数据.如CPU,流入流量,流出流量,磁盘IOPS 等性能方面的数据

(2).OSS\RDS等产品的监控

(3). ....

5.使用流程

(1).首先要在github上下载官方的 SDK

(2).打开ECS官网文档地址,查看需求对应的方法.

(3).在官方调试地址,进行调试,并生成相应的Go代码,复制示例代码然后集成在项目中,调试页面如图:

6.使用demo

需求:获取主机id为i-2ze122w6gewx4j6jfzqm的ECS在2019.05.01 00:10 到 2019.05.01 00:20时间段内,其CPU每分钟的使用情况.

(1). 查看文档,找到获取CPU数据的方法,如图: WEBRESOURCEa5f799be5f87091f26d882549f939218 (2).获取SDK: https://github.com/aliyun/alibaba-cloud-sdk-go

(3).进入调试页面输入相关参数进行调试,调试成功后,生成示例代码:

WEBRESOURCE1c24c8f8c005adf17890633e20d2a1b6

(4). 项目当中集成刚才生成的golang代码

package main

import (
	"fmt"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
)

func main() {
  client, err := cms.NewClientWithAccessKey("cn-beijing", "<accessKeyId>", "<accessSecret>")

  request := cms.CreateDescribeMetricDataRequest()

  request.EndTime = "2019-01-30 00:20:00	"
  request.StartTime = "2019-01-30 00:10:00	"
  request.Namespace = "acs_ecs_dashboard"
  request.Period = "60"
  request.MetricName = "CPUUtilization"

  response, err := client.DescribeMetricData(request)
  if err != nil {
    fmt.Print(err.Error())
  }
  fmt.Printf("response is %#v\n", response)
}

作者: xmge

github: https://github.com/xmge

go中文网: https://studygolang.com/user/xmge


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK