我正在尝试使用aws cloudwatch警报sdk循环一些结果。请求集对于一个API请求来说太大了,它为我提供了下一个令牌。这是我第一次不得不为结果集进行多次调用。我试图弄清楚如何使用下一个标记更新输入参数并返回循环直到我没有令牌并根据维度名称InstanceId提取警报名称。我想返回一个[]字符串,其中包含与该实例关联的警报名称。我知道下面的代码不正确,但它与我能够得到的一样接近。它获取了警报,但我无法让它更新输入参数。我知道那里有过多的日志记录。
数据样本:
{
"EvaluationPeriods": 1,
"AlarmArn": "arn:aws:cloudwatch:us-east-1:713901513416:alarm:[arm] Memory Utilization on urjanet-qc i-07e66faf",
"StateUpdatedTimestamp": "2015-12-08T14:41:12.885Z",
"AlarmConfigurationUpdatedTimestamp": "2015-07-16T18:59:18.262Z",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"AlarmActions": [
"arn:aws:sns:us-east-1:2134:uAlerts-arm"
],
"Namespace": "System/Linux",
"AlarmDescription": "Raise an alarm when i-07e66faf memory utilization exceeds 90%",
"StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2015-12-08T14:41:12.873+0000\",\"unit\":\"Percent\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[],\"threshold\":90.0}",
"Period": 300,
"StateValue": "INSUFFICIENT_DATA",
"Threshold": 90.0,
"AlarmName": "[arm] Memory Utilization on urjanet-qc i-07e66faf",
"Dimensions": [
{
"Name": "InstanceId",
"Value": "i-07e66faf"
}
],
"Unit": "Percent",
"Statistic": "Average",
"StateReason": "Insufficient Data: 1 datapoint was unknown.",
"InsufficientDataActions": [],
"OKActions": [
"arn:aws:sns:us-east-1:12341:uAlerts-arm"
],
"ActionsEnabled": true,
"MetricName": "MemoryUtilization"
}
部分工作代码:
func GetInstanceAlarms(instance *Instance) []string {
log.Println("In GetInstanceAlarms")
log.Println(instance.InstanceId)
var DeleteAlarmNames []string
NoToken := int(0)
c := cloudwatch.New(session.New())
for NoToken == 0 {
log.Println(NoToken)
params := &cloudwatch.DescribeAlarmsInput{
NextToken: aws.String(""),
}
log.Println(params.NextToken)
resp, err := c.DescribeAlarms(params)
if err != nil {
panic(err.Error())
}
log.Println(reflect.TypeOf(resp))
for idx := range resp.MetricAlarms {
for _, alarm := range resp.MetricAlarms[idx].Dimensions {
log.Println(*alarm.Name)
log.Println(*alarm.Value)
if *alarm.Name == "InstanceId" && *alarm.Value == instance.InstanceId {
log.Println(*alarm.Name)
log.Println(*alarm.Value)
DeleteAlarmNames = append(DeleteAlarmNames, *resp.MetricAlarms[idx].AlarmName)
log.Println(&DeleteAlarmNames)
}
}
if *resp.NextToken == "" {
log.Println("No Token Token")
NoToken = 1
}
if *resp.NextToken != "" {
log.Println("NextToken")
log.Println(*resp.NextToken)
log.Println("Found Token")
NoToken = 0
params.NextToken = resp.NextToken
}
}
}
log.Println(DeleteAlarmNames)
return DeleteAlarmNames
}