I am trying to loop over some results using the aws cloudwatch alarms sdk. The request set is too big for one API request, and it gives me a next token. This is the first time I have had to make more than one call for a results set. I am trying to figure out how to update the input parameters with the next token and go back through the loop till I have no token and extract the alarm name based on the Dimension name InstanceId. I want to return a []string with the alarms names associated with that instance. I know the code below is not correct but it is as close as I have been able to get. It gets Alarms, but I have not been able to get it to update the input parameters. I know there are excessive logging statements in there.
Data Sample:
{
"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"
}
Partially working code:
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
}
</div>