-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudwatch_alarm.go
186 lines (151 loc) · 6.74 KB
/
cloudwatch_alarm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package window
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
type (
CloudWatchAlarm struct {
// Indicates whether actions should be executed during any changes to the alarm's
// state.
ActionsEnabled bool
// The list of actions to execute when this alarm transitions into an ALARM
// state from any other state. Each action is specified as an Amazon Resource
// Number (ARN). Currently the only actions supported are publishing to an Amazon
// SNS topic and triggering an Auto Scaling policy.
AlarmActions []string
// The Amazon Resource Name (ARN) of the alarm.
AlarmArn string
// The time stamp of the last update to the alarm configuration. Amazon CloudWatch
// uses Coordinated Universal Time (UTC) when returning time stamps, which do
// not accommodate seasonal adjustments such as daylight savings time. For more
// information, see Time stamps (http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cloudwatch_concepts.html#about_timestamp)
// in the Amazon CloudWatch Developer Guide.
AlarmConfigurationUpdatedTimestamp time.Time
// The description for the alarm.
AlarmDescription string
// The name of the alarm.
AlarmName string
// The arithmetic operation to use when comparing the specified Statistic and
// Threshold. The specified Statistic value is used as the first operand.
ComparisonOperator string
// The list of dimensions associated with the alarm's associated metric.
Dimensions []*cloudwatch.Dimension
// The number of periods over which data is compared to the specified threshold.
EvaluationPeriods int64
// The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA
// state from any other state. Each action is specified as an Amazon Resource
// Number (ARN). Currently the only actions supported are publishing to an Amazon
// SNS topic or triggering an Auto Scaling policy.
//
// The current WSDL lists this attribute as UnknownActions.
InsufficientDataActions []string
// The name of the alarm's metric.
MetricName string
// The namespace of alarm's associated metric.
Namespace string
// The list of actions to execute when this alarm transitions into an OK state
// from any other state. Each action is specified as an Amazon Resource Number
// (ARN). Currently the only actions supported are publishing to an Amazon SNS
// topic and triggering an Auto Scaling policy.
OKActions []string
// The period in seconds over which the statistic is applied.
Period int64
// A human-readable explanation for the alarm's state.
StateReason string
// An explanation for the alarm's state in machine-readable JSON format
StateReasonData string
// The time stamp of the last update to the alarm's state. Amazon CloudWatch
// uses Coordinated Universal Time (UTC) when returning time stamps, which do
// not accommodate seasonal adjustments such as daylight savings time. For more
// information, see Time stamps (http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cloudwatch_concepts.html#about_timestamp)
// in the Amazon CloudWatch Developer Guide.
StateUpdatedTimestamp time.Time
// The state value for the alarm.
StateValue string
// The statistic to apply to the alarm's associated metric.
Statistic string
// The value against which the specified statistic is compared.
Threshold float64
// The unit of the alarm's associated metric.
Unit string
Name string
Id string
State string
Region *Region
AlarmActionSNSs []*SNSTopic
AlarmActionAutoScalingGroups []*AutoScalingGroup
InsufficientDataActionSNSs []*SNSTopic
InsufficientDataActionAutoScalingGroups []*AutoScalingGroup
OKActionSNSs []*SNSTopic
OKActionAutoScalingGroups []*AutoScalingGroup
}
CloudWatchAlarmByNameAsc []*CloudWatchAlarm
)
func (a CloudWatchAlarmByNameAsc) Len() int { return len(a) }
func (a CloudWatchAlarmByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a CloudWatchAlarmByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func LoadCloudWatchAlarms(input *cloudwatch.DescribeAlarmsInput) (map[string]*CloudWatchAlarm, error) {
alarms := map[string]*CloudWatchAlarm{}
if err := CloudWatchClient.DescribeAlarmsPages(input, func(p *cloudwatch.DescribeAlarmsOutput, _ bool) bool {
for _, ma := range p.MetricAlarms {
alarm := &CloudWatchAlarm{
ActionsEnabled: aws.BoolValue(ma.ActionsEnabled),
AlarmActions: aws.StringValueSlice(ma.AlarmActions),
AlarmArn: aws.StringValue(ma.AlarmArn),
AlarmConfigurationUpdatedTimestamp: aws.TimeValue(ma.AlarmConfigurationUpdatedTimestamp),
AlarmDescription: aws.StringValue(ma.AlarmDescription),
AlarmName: aws.StringValue(ma.AlarmName),
ComparisonOperator: aws.StringValue(ma.ComparisonOperator),
Dimensions: ma.Dimensions,
EvaluationPeriods: aws.Int64Value(ma.EvaluationPeriods),
InsufficientDataActions: aws.StringValueSlice(ma.InsufficientDataActions),
MetricName: aws.StringValue(ma.MetricName),
Namespace: aws.StringValue(ma.Namespace),
OKActions: aws.StringValueSlice(ma.OKActions),
Period: aws.Int64Value(ma.Period),
StateReason: aws.StringValue(ma.StateReason),
StateReasonData: aws.StringValue(ma.StateReasonData),
StateUpdatedTimestamp: aws.TimeValue(ma.StateUpdatedTimestamp),
StateValue: aws.StringValue(ma.StateValue),
Statistic: aws.StringValue(ma.Statistic),
Threshold: aws.Float64Value(ma.Threshold),
Unit: aws.StringValue(ma.Unit),
}
alarm.Name = alarm.AlarmName
alarm.Id = "cwa:" + alarm.AlarmArn
alarm.State = alarm.StateValue
alarms[alarm.AlarmArn] = alarm
}
return true
}); err != nil {
return nil, err
}
return alarms, nil
}
func (a *CloudWatchAlarm) Inactive() bool {
return !a.ActionsEnabled
}
func (a *CloudWatchAlarm) Summary() string {
if i := strings.IndexByte(a.AlarmDescription, '!'); i > 0 {
return a.AlarmDescription[:i]
}
if len(a.AlarmDescription) > 0 {
return a.AlarmDescription
}
return fmt.Sprintf("%s/%s/%s %s %s %s for at least %s during %d evaluation period",
a.Namespace,
a.MetricName,
a.Statistic,
a.ComparisonOperator,
strconv.FormatFloat(a.Threshold, 'f', -1, 64),
a.Unit,
roundTime(time.Duration(a.Period)*time.Second),
a.EvaluationPeriods,
)
}