class AWS::CloudWatch::MetricAlarmCollection

# 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 {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| ... }

Attributes

metric[R]

@return [Metric]

Public Class Methods

new(metric, options = {}) click to toggle source

@api private

Calls superclass method AWS::CloudWatch::AlarmCollection::new
# File lib/aws/cloud_watch/metric_alarm_collection.rb, line 49
def initialize metric, options = {}
  @metric = metric
  super(options.merge(:config => metric.config))
end

Public Instance Methods

[](alarm_name) click to toggle source

@param [String] alarm_name @return [Alarm]

# 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 = {}) click to toggle source

Creates an alarm for this metric. @param (see AlarmCollection#create) @option (see MetricAlarm#update) @return (see AlarmCollection#create)

Calls superclass method AWS::CloudWatch::AlarmCollection#create
# 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) click to toggle source

Returns a new collection that will filter results when enumerated.

@example Filtering by a 1 hour period

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

@example Filtering by statistic

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

@example Filtering by a unit

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

@param [String,Symbol] name @param [String,Integer] value @return [MetricAlarmCollection]

# 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) click to toggle source

Returns a new collection that filters alarms by a period.

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

@param [Integer] period @return [MetricAlarmCollection]

# File lib/aws/cloud_watch/metric_alarm_collection.rb, line 107
def with_period period
  filter(:period, period)
end
with_statistic(statistic) click to toggle source

Returns a new collection that filters alarms by a statistic.

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

@param [String] statistic @return [MetricAlarmCollection]

# File lib/aws/cloud_watch/metric_alarm_collection.rb, line 117
def with_statistic statistic
  filter(:statistic, statistic)
end
with_unit(unit) click to toggle source

Returns a new collection that filters alarms by a unit.

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

@param [String] unit @return [MetricAlarmCollection]

# File lib/aws/cloud_watch/metric_alarm_collection.rb, line 127
def with_unit unit
  filter(:unit, unit)
end

Protected Instance Methods

_each_item(options = {}) { |alarm| ... } click to toggle source
# File lib/aws/cloud_watch/metric_alarm_collection.rb, line 133
def _each_item options = {}, &block

  options = @filters.merge(options)

  options[:namespace] = metric.namespace
  options[:metric_name] = metric.metric_name
  options[:dimensions] = metric.dimensions unless metric.dimensions.empty?

  resp = client.describe_alarms_for_metric(options)
  resp.data[:metric_alarms].each do |details|

    alarm = Alarm.new_from(
      :describe_alarms_for_metric,
      details,
      details[:alarm_name],
      :config => config)

    yield(alarm)

  end

  resp.data[:next_token]

end