腾讯云的golang SDK非常好用,接入非常方便。
- 腾讯云使用golang的SDK,不用数字签名,只需要获取到key
- 腾讯云的接口返回,不用解析,可以直接使用其结构体,结构体内都是指针,需要注意
- 如下代码是获取腾讯云的历史告警
package services
import (
"fmt"
"time"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
//"huaxkeeper/logs"
"huaxkeeper/models"
)
func TengxunUpdate() {
//获取起始时间戳,时间间隔5分钟
now := time.Now()
m, _ := time.ParseDuration("-5m")
EndTime := time.Now().Unix()
StartTime := now.Add(m).Unix()
alarminfo := models.Alarm{}
credential := common.NewCredential(
"keyid",
"key",
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "monitor.tencentcloudapi.com"
client, _ := monitor.NewClient(credential, "ap-shanghai", cpf)
request := monitor.NewDescribeAlarmHistoriesRequest()
request.Module = common.StringPtr("monitor")
request.PageNumber = common.Int64Ptr(1)
request.PageSize = common.Int64Ptr(100)
request.StartTime = common.Int64Ptr(StartTime)
request.EndTime = common.Int64Ptr(EndTime)
response, err := client.DescribeAlarmHistories(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
//fmt.Printf("%s", response.ToJsonString())
tmp := response.Response.Histories
//写入数据库
for _,i := range tmp {
alarminfo.Name = *i.PolicyName
alarminfo.Summary = *i.Content
alarminfo.Project = *i.ProjectName
alarminfo.Message = *i.AlarmObject
priword := *i.MetricsInfo[0].Description
if priword == "致命级风险" {
alarminfo.Priority = "critical"
} else {
alarminfo.Priority = "major"
}
alarminfo.Type = "云产品监控"
//时间戳转时间
s_time := *i.FirstOccurTime
alarminfo.StartTime = time.Unix(s_time, 0).Format("2006-01-02 15:04:05")
if *i.AlarmStatus == "OK" {
alarminfo.State = "solved"
} else {
alarminfo.State = "alerting"
}
alarminfo.AlarmId = *i.AlarmId
alarminfo.Create(alarminfo)
}
}
网友评论