Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Do not merge) Add support for insight metrics #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/cfnguardian/compile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ def validate_resources()
@resources.each do |resource|
case resource.type
when 'Alarm'
%w(metric_name namespace).each do |property|
if resource.send(property).nil?
@errors << "CfnGuardian::AlarmPropertyError - alarm #{resource.name} for resource #{resource.resource_id} has nil value for property #{property.to_camelcase}. This could be due to incorrect spelling of a default alarm name or missing property #{property.to_camelcase} on a new alarm."
if resource.metrics.nil?
%w(metric_name namespace).each do |property|
if resource.send(property).nil?
@errors << "CfnGuardian::AlarmPropertyError - alarm #{resource.name} for resource #{resource.resource_id} has nil value for property #{property.to_camelcase}. This could be due to incorrect spelling of a default alarm name or missing property #{property.to_camelcase} on a new alarm."
end
end
end
when 'Check'
Expand Down
2 changes: 2 additions & 0 deletions lib/cfnguardian/display_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def alarms()
['ResourceName', alarm.resource_name],
['Enabled', alarm.enabled],
['MetricName', alarm.metric_name],
['Metrics', alarm.metrics],
['Dimensions', alarm.dimensions],
['Threshold', alarm.threshold],
['Period', alarm.period],
Expand Down Expand Up @@ -60,6 +61,7 @@ def compare_alarms(metric_alarms)
['ResourceName', alarm.resource_name, alarm.resource_name],
['Enabled', alarm.enabled, true],
['MetricName', alarm.metric_name, metric_alarm.metric_name],
['Metrics', alarm.metrics],
['Dimensions', alarm.dimensions, dimensions],
['Threshold', alarm.threshold.to_f, metric_alarm.threshold],
['Period', alarm.period, metric_alarm.period],
Expand Down
2 changes: 2 additions & 0 deletions lib/cfnguardian/models/alarm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BaseAlarm
:metric_name,
:namespace,
:dimensions,
:metrics,
:threshold,
:period,
:evaluation_periods,
Expand All @@ -39,6 +40,7 @@ def initialize(resource)
@metric_name = nil
@namespace = nil
@dimensions = nil
@metrics = nil
@threshold = 0
@period = 60
@evaluation_periods = 1
Expand Down
12 changes: 10 additions & 2 deletions lib/cfnguardian/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,17 @@ def get_alarms(group,overides={})
@alarms.each {|a| a.group = @override_group}
end

# String interpolation for alarm dimensions
@alarms.each do |alarm|
next if alarm.dimensions.nil?
if ! alarm.metrics.nil?
alarm.metrics.each_with_index do |m,i|
m['Expression'] = { "Fn::Sub": [ m['Expression'], @resource ] }
m['Label'] = "#{alarm.name}#{i}" if m['Label'].nil?
m['Id'] = "q#{i}" if m['Id'].nil?
m['Period'] = alarm.period if m['Period'].nil?
end
end
# String interpolation for alarm dimensions
next if alarm.dimensions.nil? || ! alarm.metrics.nil?
alarm.dimensions.each do |k,v|
if v.is_a?(String) && v.match?(/^\${Resource::.*[A-Za-z]}$/)
resource_key = v.tr('${}', '').split('Resource::').last
Expand Down
9 changes: 5 additions & 4 deletions lib/cfnguardian/stacks/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ def add_alarm(alarm)
AlarmDescription "Guardian alarm #{alarm.name} for the resource #{alarm.resource_id} in alarm group #{alarm.group}"
AlarmName CfnGuardian::CloudWatch.get_alarm_name(alarm)
ComparisonOperator alarm.comparison_operator
Metrics alarm.metrics unless alarm.metrics.nil?
Dimensions alarm.dimensions.map {|k,v| {Name: k, Value: v}} unless alarm.dimensions.nil?
EvaluationPeriods alarm.evaluation_periods
Statistic alarm.statistic if alarm.extended_statistic.nil?
Period alarm.period
Statistic alarm.statistic if alarm.extended_statistic.nil? && alarm.metrics.nil?
Period alarm.period if alarm.metrics.nil?
Threshold alarm.threshold
MetricName alarm.metric_name
Namespace alarm.namespace
MetricName alarm.metric_name if alarm.metrics.nil?
Namespace alarm.namespace if alarm.metrics.nil?
AlarmActions actions
OKActions actions
TreatMissingData alarm.treat_missing_data unless alarm.treat_missing_data.nil?
Expand Down
Loading