主要参考了以下位置的资料:
- 云服务器监控接口
- 腾讯云go-sdk example
方法一:使用腾讯云go-sdk
- go.mod文件中增加这样一行:
github.com/tencentcloud/tencentcloud-sdk-go v3.0.172
- 准备好:secretId, secretKey,要查询的区域,CVM机器的实例ID
- 以下是例子代码:
import (
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/regions"
Monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
)
// ....
func QueryCvmWanOuttraffic(SecretId string, secretKey string, region string, instanceId string) (string, error) {
credential := common.NewCredential(SecretId, secretKey)
cpf := profile.NewClientProfile()
cpf.HttpProfile.ReqMethod = "GET"
cpf.HttpProfile.ReqTimeout = 5
cpf.Debug = true
cpf.SignMethod = "HmacSHA1"
monitorClient, err := Monitor.NewClient(credential, region, cpf)
if err != nil {
return "", err
}
req := Monitor.NewGetMonitorDataRequest()
var v uint64 = 60
req.Period = &v
now := time.Now()
var arr = []string{
"WanOuttraffic",
"QCE/CVM",
"InstanceId",
now.Add(time.Duration(-10) * time.Minute).Format("2006-01-02 15:04:05"), //查询最近10分钟的
now.Format("2006-01-02 15:04:05"),
}
req.MetricName = &arr[0]
req.Namespace = &arr[1]
req.Instances = []*Monitor.Instance{
&Monitor.Instance{
Dimensions: []*Monitor.Dimension{
&Monitor.Dimension{Name: &arr[2], Value: &instanceId},
},
},
}
req.StartTime = &arr[3]
req.EndTime = &arr[4]
resp, err := monitorClient.GetMonitorData(req)
if err != nil {
return "", err
}
return resp.ToJsonString(), nil
}
//
QueryCvmWanOuttraffic(os.Getenv("TENCENTCLOUD_SECRET_ID"), os.Getenv("TENCENTCLOUD_SECRET_KEY"), regions.Guangzhou, "ins-xxxxxx")
方法2:使用https协议
直接上代码:
代码语言:javascript复制import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
)
// ....
func HttpsQueryCvmWanOuttraffic(SecretId string, secretKey string, region string, instanceId string) (string, error) {
now := time.Now()
var params = map[string]string{
"namespace": "qce/cvm",
"metricName": "wan_outtraffic",
"dimensions.0.name": "unInstanceId",
"dimensions.0.value": instanceId,
"startTime": now.Add(time.Duration(-10) * time.Minute).Format("2006-01-02 15:04:05"),
"endTime": now.Format("2006-01-02 15:04:05"),
"Region": region,
"Action": "GetMonitorData",
"SecretId": SecretId,
"Timestamp": strconv.Itoa(int(now.Unix())),
"Nonce": strconv.Itoa(rand.Intn(1000000)),
}
arr := make([]string, 0, len(params))
for k, _ := range params {
arr = append(arr, k)
}
sort.Strings(arr)
arrOut := make([]string, 0, len(arr))
for _, item := range arr {
arrOut = append(arrOut, item "=" url.QueryEscape(params[item]))
}
paramStr := strings.Join(arrOut, "&")
key := []byte(secretKey)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(paramStr))
Signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
paramStr = "&Signature=" Signature
url := "https://monitor.tencentcloudapi.com/?"
resp, err := http.Get(url paramStr)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
//
HttpsQueryCvmWanOuttraffic(os.Getenv("TENCENTCLOUD_SECRET_ID"), os.Getenv("TENCENTCLOUD_SECRET_KEY"), "gz", "ins-xxxxxx")
返回的数据
返回数据大致如下:
代码语言:javascript复制{
"Response":{
"Period":60,
"MetricName":"WanOuttraffic",
"DataPoints":[
{
"Dimensions":[{"Name":"InstanceId","Value":"ins-xxxxx"}],
"Timestamps":[1589629500,1589629560,1589629620,1589629680,1589629740,1589629800,1589629860,1589629920],
"Values":[83.235,107.038,99.522,101.662,97.613,95.696,85.926,103.345]
}
],
"StartTime":"2020-05-16 19:45:00",
"EndTime":"2020-05-16 19:55:00",
"RequestId":"280b4f26-4117-4287-bc96-79e27283b0ed"
}
}
可以看见,时间戳1589629920对应2020-5-16 19:52;这一分钟的网卡流量是103.345Mbps
在腾讯云官网核对一下
进入https://console.cloud.tencent.com/ 在云服务器->实例->搜索ins-xxxxx->点击进入->监控->外网出带宽,时间粒度选择分钟,在19:52上看见:
成功了!嗨森!