1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
module AWS
class CloudWatch
# # 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| ... }
#
class MetricAlarmCollection < AlarmCollection
include Core::Collection::Simple
# @api private
def initialize metric, options = {}
@metric = metric
super(options.merge(:config => metric.config))
end
# @return [Metric]
attr_reader :metric
# @param [String] alarm_name
# @return [Alarm]
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
# Creates an alarm for this metric.
# @param (see AlarmCollection#create)
# @option (see MetricAlarm#update)
# @return (see AlarmCollection#create)
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
# 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]
def filter name, value
filters = @filters.merge(name.to_s.to_sym => value)
MetricAlarmCollection.new(metric, :filters => filters)
end
# Returns a new collection that filters alarms by a period.
#
# metric.alarms.with_period(3600).each {|alarm| ... }
#
# @param [Integer] period
# @return [MetricAlarmCollection]
def with_period period
filter(:period, period)
end
# Returns a new collection that filters alarms by a statistic.
#
# metric.alarms.with_statistic('Average').each {|alarm| ... }
#
# @param [String] statistic
# @return [MetricAlarmCollection]
def with_statistic statistic
filter(:statistic, statistic)
end
# Returns a new collection that filters alarms by a unit.
#
# metric.alarms.with_unit('Percent').each {|alarm| ... }
#
# @param [String] unit
# @return [MetricAlarmCollection]
def with_unit unit
filter(:unit, unit)
end
protected
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
end
end
end
|