Class: AWS::CloudWatch::MetricAlarmCollection

Inherits:
AlarmCollection show all
Includes:
AWS::Core::Collection::Simple
Defined in:
lib/aws/cloud_watch/metric_alarm_collection.rb

Overview

MetricAlarmCollection

Represents all alarms for a single metric.

Getting an alarm by name

If you know the name of the alarm, you can get a reference using the #[] method.

metric.alarms['alarm-name']

Enumerating Alarms

You can enumerate all alarms for a metric using each (or any of the methods defined in AWS::Core::Collection).

metric.alarms.each do |alarm|
  puts alarm.name
end

Filtering Alarms

Use one of the filtering methods to reduce the number of alarms returned.

metric.alarms.with_unit('Seconds').each {|alarm| ... }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AWS::Core::Collection

#each, #each_batch, #enum, #first, #in_groups_of, #page

Methods inherited from AlarmCollection

#delete, #with_action_prefix, #with_name, #with_name_prefix, #with_state_value

Instance Attribute Details

#metricMetric (readonly)

Returns:



55
56
57
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 55

def metric
  @metric
end

Instance Method Details

#[](alarm_name) ⇒ Alarm

Parameters:

  • alarm_name (String)

Returns:



59
60
61
62
63
64
65
66
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 59

def [] alarm_name
  options = {}
  options[:namespace] = metric.namespace
  options[:metric_name] = metric.name
  options[:dimensions] = metric.dimensions unless metric.dimensions.empty?
  options[:config] = config
  Alarm.new(alarm_name, options)
end

#create(alarm_name, options = {}) ⇒ Alarm

Creates an alarm for this metric.

Parameters:

  • alarm_name (String)

    The descriptive name for the alarm. This name must be unique within the user’s AWS account.

  • options (Hash) (defaults to: {})

Returns:



72
73
74
75
76
77
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 72

def create alarm_name, options = {}
  options[:namespace] = metric.namespace
  options[:metric_name] = metric.metric_name
  options[:dimensions] = metric.dimensions unless metric.dimensions.empty?
  super(alarm_name, options)
end

#filter(name, value) ⇒ MetricAlarmCollection

Returns a new collection that will filter results when enumerated.

Examples:

Filtering by a 1 hour period


metric.alarms.filter('period', 3600)

Filtering by statistic


my_metric = metrics.filter('statistic', 'maximum')

Filtering by a unit


metrics = metrics.filter('unit', 'Megabits')

Parameters:

  • name (String, Symbol)
  • value (String, Integer)

Returns:



96
97
98
99
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 96

def filter name, value
  filters = @filters.merge(name.to_s.to_sym => value)
  MetricAlarmCollection.new(metric, :filters => filters)
end

#with_period(period) ⇒ MetricAlarmCollection

Returns a new collection that filters alarms by a period.

metric.alarms.with_period(3600).each {|alarm| ... }

Parameters:

  • period (Integer)

Returns:



107
108
109
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 107

def with_period period
  filter(:period, period)
end

#with_statistic(statistic) ⇒ MetricAlarmCollection

Returns a new collection that filters alarms by a statistic.

metric.alarms.with_statistic('Average').each {|alarm| ... }

Parameters:

  • statistic (String)

Returns:



117
118
119
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 117

def with_statistic statistic
  filter(:statistic, statistic)
end

#with_unit(unit) ⇒ MetricAlarmCollection

Returns a new collection that filters alarms by a unit.

metric.alarms.with_unit('Percent').each {|alarm| ... }

Parameters:

  • unit (String)

Returns:



127
128
129
# File 'lib/aws/cloud_watch/metric_alarm_collection.rb', line 127

def with_unit unit
  filter(:unit, unit)
end