Class: CfnGuardian::CloudWatch
- Inherits:
-
Object
- Object
- CfnGuardian::CloudWatch
show all
- Includes:
- Logging
- Defined in:
- lib/cfnguardian/cloudwatch.rb
Class Method Summary
collapse
Methods included from Logging
colors, included, logger, #logger, logger=
Class Method Details
.aws_account_id ⇒ Object
129
130
131
132
133
|
# File 'lib/cfnguardian/cloudwatch.rb', line 129
def self.aws_account_id()
sts = Aws::STS::Client.new
account = sts.get_caller_identity().account
return account
end
|
.disable_alarms(alarms) ⇒ Object
107
108
109
110
111
112
|
# File 'lib/cfnguardian/cloudwatch.rb', line 107
def self.disable_alarms(alarms)
client = Aws::CloudWatch::Client.new
alarms.each_slice(100) do |batch|
client.disable_alarm_actions({alarm_names: batch})
end
end
|
.enable_alarms(alarms) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/cfnguardian/cloudwatch.rb', line 114
def self.enable_alarms(alarms)
client = Aws::CloudWatch::Client.new
alarms.each_slice(100) do |batch|
client.enable_alarm_actions({alarm_names: batch})
end
alarms.each do |alarm|
client.set_alarm_state({
alarm_name: alarm,
state_value: "OK",
state_reason: "End of guardian maintenance period"
})
end
end
|
.filter_alarms(filters:, alarms:) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/cfnguardian/cloudwatch.rb', line 56
def self.filter_alarms(filters:, alarms:)
return alarms unless filters.is_a?(Hash)
filters = filters.slice('group', 'resource', 'alarm', 'stack-id')
filtered_alarms = []
alarms.each do |alarm|
if filters.values.all? {|filter| alarm.alarm_name.include? (filter)}
filtered_alarms << alarm
end
end
return filtered_alarms
end
|
.get_alarm_arn(alarm) ⇒ Object
13
14
15
|
# File 'lib/cfnguardian/cloudwatch.rb', line 13
def self.get_alarm_arn(alarm)
return "arn:aws:cloudwatch:#{Aws.config[:region]}:#{aws_account_id()}:alarm:#{self.get_alarm_name(alarm)}"
end
|
.get_alarm_history(alarm_name, type) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/cfnguardian/cloudwatch.rb', line 70
def self.get_alarm_history(alarm_name,type)
client = Aws::CloudWatch::Client.new()
logger.debug "Searching #{type} history for #{alarm_name}"
resp = client.describe_alarm_history({
alarm_name: alarm_name,
history_item_type: type,
start_date: (Time.now.utc.to_date - 7),
end_date: (Time.now.utc.to_date + 1),
max_records: 100
})
return resp.alarm_history_items
end
|
.get_alarm_name(alarm) ⇒ Object
8
9
10
11
|
# File 'lib/cfnguardian/cloudwatch.rb', line 8
def self.get_alarm_name(alarm)
alarm_id = alarm.resource_name.nil? ? alarm.resource_id : alarm.resource_name
return "guardian-#{alarm.group}-#{alarm_id}-#{alarm.name}"
end
|
.get_alarm_names(action_prefix = nil, alarm_name_prefix = 'guardian') ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/cfnguardian/cloudwatch.rb', line 86
def self.get_alarm_names(action_prefix=nil,alarm_name_prefix='guardian')
alarms = []
client = Aws::CloudWatch::Client.new
options = {
alarm_types: ["CompositeAlarm","MetricAlarm"],
alarm_name_prefix: alarm_name_prefix
}
unless action_prefix.nil?
options[:action_prefix] = "arn:aws:sns:#{Aws.config[:region]}:#{aws_account_id()}:#{action_prefix}"
end
client.describe_alarms(options).each do |response|
alarms.concat response.composite_alarms.map(&:alarm_name)
alarms.concat response.metric_alarms.map(&:alarm_name)
end
return alarms
end
|
.get_alarms_by_name(alarm_names:, state: nil, action_prefix: nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/cfnguardian/cloudwatch.rb', line 34
def self.get_alarms_by_name(alarm_names:, state: nil, action_prefix: nil)
client = Aws::CloudWatch::Client.new()
options = {max_records: 100}
unless state.nil?
options[:state_value] = state
end
unless action_prefix.nil?
options[:action_prefix] = "arn:aws:sns:#{Aws.config[:region]}:#{aws_account_id()}:#{action_prefix}"
end
metric_alarms = []
alarm_names.each_slice(100) do |batch|
options[:alarm_names] = batch
resp = client.describe_alarms(options)
metric_alarms.push(*resp.metric_alarms)
end
return metric_alarms
end
|
.get_alarms_by_prefix(prefix:, state: nil, action_prefix: nil) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/cfnguardian/cloudwatch.rb', line 17
def self.get_alarms_by_prefix(prefix:, state: nil, action_prefix: nil)
client = Aws::CloudWatch::Client.new()
options = {max_records: 100}
options[:alarm_name_prefix] = prefix
unless state.nil?
options[:state_value] = state
end
unless action_prefix.nil?
options[:action_prefix] = action_prefix
end
resp = client.describe_alarms(options)
return resp.metric_alarms
end
|