Class: AWS::CloudWatch::AlarmCollection

Inherits:
Object
  • Object
show all
Includes:
AWS::Core::Collection::WithLimitAndNextToken
Defined in:
lib/aws/cloud_watch/alarm_collection.rb

Overview

AlarmCollection

Represents alarms for an AWS account.

Getting an alarm by name

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

cw = AWS::CloudWatch.new
alarm = cw.alarms['alarm-name']

Enumerating Alarms

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

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

Filtering Alarms

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

cw.alarms.with_name_prefix('some-prefix-').each {|alarm| ... }

Direct Known Subclasses

MetricAlarmCollection

Instance Method Summary collapse

Methods included from AWS::Core::Collection

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

Constructor Details

#initialize(options = {}) ⇒ AlarmCollection

Returns a new instance of AlarmCollection.



49
50
51
52
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 49

def initialize options = {}
  @filters = options[:filters] || {}
  super
end

Instance Method Details

#[](alarm_name) ⇒ Alarm

Parameters:

  • alarm_name (String)

Returns:



56
57
58
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 56

def [] alarm_name
  Alarm.new(alarm_name, :config => config)
end

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

Creates an alarm and associates it with the specified 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: {})

Options Hash (options):

  • :namespace (String, required)

    The namespace for the alarm’s associated metric.

  • :metric_name (String, required)

    The name for the alarm’s associated metric.

  • :dimensions (Array<Hash>)

    The dimensions for the alarm’s associated metric. Each dimension must specify a :name and a :value.

  • :comparison_operator (String, required)

    The arithmetic operation to use when comparing the specified Statistic and Threshold. The specified Statistic value is used as the first operand. Valid values inlucde:

    • ‘GreaterThanOrEqualToThreshold’

    • ‘GreaterThanThreshold’

    • ‘LessThanThreshold’

    • ‘LessThanOrEqualToThreshold’

  • :evaluation_periods (Integer, required)

    The number of periods over which data is compared to the specified threshold.

  • :period (Integer, required)

    The period in seconds over which the specified statistic is applied.

  • :statistic (String, required)

    The statistic to apply to the alarm’s associated metric. Valid values include:

    • ‘SampleCount’

    • ‘Average’

    • ‘Sum’

    • ‘Minimum’

    • ‘Maximum’

  • :threshold (Number, required)

    The value against which the specified statistic is compared.

  • :insufficient_data_actions (Array<String>)

    The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state. Each action is specified as an Amazon Resource Number (ARN). Currently the only action supported is publishing to an Amazon SNS topic or an Amazon Auto Scaling policy.

  • :ok_actions (Array<String>)

    The list of actions to execute when this alarm transitions into an OK state from any other state. Each action is specified as an Amazon Resource Number (ARN). Currently the only action supported is publishing to an Amazon SNS topic or an Amazon Auto Scaling policy.

  • :actions_enabled (Boolean)

    Indicates whether or not actions should be executed during any changes to the alarm’s state.

  • :alarm_actions (Array<String>)

    The list of actions to execute when this alarm transitions into an ALARM state from any other state. Each action is specified as an Amazon Resource Number (ARN). Currently the only action supported is publishing to an Amazon SNS topic or an Amazon Auto Scaling policy. Maximum of 5 alarm actions.

  • :alarm_description (String)

    The description for the alarm.

  • :unit (String)

    The unit for the alarm’s associated metric.

Returns:



74
75
76
77
78
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 74

def create alarm_name, options = {}
  options[:alarm_name] = alarm_name
  client.put_metric_alarm(options)
  self[alarm_name]
end

#delete(*alarm_names) ⇒ nil

Delete one or more alarms by name.

cloud_watch.alarms.delete('alarm1', 'alarm2')

Parameters:

  • alarm_names (String, Array<String>)

Returns:

  • (nil)


86
87
88
89
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 86

def delete *alarm_names
  client.delete_alarms(:alarm_names => alarm_names.flatten)
  nil
end

#filter(name, value) ⇒ Alarm

Returns a new collection with the given filter.

Parameters:

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

Returns:



95
96
97
98
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 95

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

#with_action_prefix(prefix) ⇒ MetricAlarmCollection

Parameters:

  • prefix (String)

Returns:



102
103
104
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 102

def with_action_prefix prefix
  filter(:action_prefix, prefix)
end

#with_name(*names) ⇒ MetricAlarmCollection

Parameters:

  • names (String, Array<String>)

    A list of alarm names to retrieve information for.

Returns:



115
116
117
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 115

def with_name *names
  filter(:alarm_names, names.flatten)
end

#with_name_prefix(prefix) ⇒ MetricAlarmCollection

Parameters:

  • prefix (String)

    The alarm name prefix.

Returns:



108
109
110
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 108

def with_name_prefix prefix
  filter(:alarm_name_prefix, prefix)
end

#with_state_value(state) ⇒ MetricAlarmCollection

Parameters:

  • state (String)

    The state value to be used in matching alarms.

Returns:



121
122
123
# File 'lib/aws/cloud_watch/alarm_collection.rb', line 121

def with_state_value state
  filter(:state_value, state)
end