Skip to content

Commit

Permalink
Add support for insight metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
cfln123 committed Sep 16, 2024
1 parent 287404f commit 7533103
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
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
2 changes: 1 addition & 1 deletion lib/cfnguardian/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def get_alarms(group,overides={})

# String interpolation for alarm dimensions
@alarms.each do |alarm|
next if alarm.dimensions.nil?
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

0 comments on commit 7533103

Please sign in to comment.