Class: Fog::AWS::CloudWatch::Real

Inherits:
Object
  • Object
show all
Includes:
Fog::AWS::CredentialFetcher::ConnectionMethods
Defined in:
lib/fog/aws/cloud_watch.rb,
lib/fog/aws/requests/cloud_watch/list_metrics.rb,
lib/fog/aws/requests/cloud_watch/delete_alarms.rb,
lib/fog/aws/requests/cloud_watch/describe_alarms.rb,
lib/fog/aws/requests/cloud_watch/put_metric_data.rb,
lib/fog/aws/requests/cloud_watch/set_alarm_state.rb,
lib/fog/aws/requests/cloud_watch/put_metric_alarm.rb,
lib/fog/aws/requests/cloud_watch/enable_alarm_actions.rb,
lib/fog/aws/requests/cloud_watch/disable_alarm_actions.rb,
lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb,
lib/fog/aws/requests/cloud_watch/describe_alarm_history.rb,
lib/fog/aws/requests/cloud_watch/describe_alarms_for_metric.rb

Instance Method Summary collapse

Methods included from Fog::AWS::CredentialFetcher::ConnectionMethods

#refresh_credentials_if_expired

Constructor Details

#initialize(options = {}) ⇒ Real

Initialize connection to Cloudwatch

Notes

options parameter must include values for :aws_access_key_id and :aws_secret_access_key in order to create a connection

Examples

elb = CloudWatch.new(
 :aws_access_key_id => your_aws_access_key_id,
 :aws_secret_access_key => your_aws_secret_access_key
)

Parameters

  • options<~Hash> - config arguments for connection. Defaults to {}.

    • region<~String> - optional region to use. For instance, ‘eu-west-1’, ‘us-east-1’, etc.

Returns

  • CloudWatch object with connection to AWS.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fog/aws/cloud_watch.rb', line 92

def initialize(options={})
  @use_iam_profile = options[:use_iam_profile]
  setup_credentials(options)

  @connection_options = options[:connection_options] || {}

  @instrumentor           = options[:instrumentor]
  @instrumentor_name      = options[:instrumentor_name] || 'fog.aws.cloud_watch'

  options[:region] ||= 'us-east-1'
  @host = options[:host] || "monitoring.#{options[:region]}.amazonaws.com"
  @path       = options[:path]        || '/'
  @persistent = options[:persistent]  || false
  @port       = options[:port]        || 443
  @scheme     = options[:scheme]      || 'https'
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
end

Instance Method Details

#delete_alarms(alarm_names) ⇒ Object

Delete a list of alarms

Options

  • AlarmNames<~Array>: A list of alarms to be deleted

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/index.html?API_DeleteAlarms.html



19
20
21
22
23
24
25
26
# File 'lib/fog/aws/requests/cloud_watch/delete_alarms.rb', line 19

def delete_alarms(alarm_names)
  options = {}
  options.merge!(AWS.indexed_param('AlarmNames.member.%d', [*alarm_names]))
  request({
      'Action'    => 'DeleteAlarms',
      :parser     => Fog::Parsers::AWS::CloudWatch::DeleteAlarms.new
    }.merge(options))
end

#describe_alarm_history(options = {}) ⇒ Object

Retrieves history for the specified alarm

Options

  • AlarmName<~String>: The name of the alarm

  • EndDate<~DateTime>: The ending date to retrieve alarm history

  • HistoryItemType<~String>: The type of alarm histories to retrieve

  • MaxRecords<~Integer>: The maximum number of alarm history records to retrieve

  • NextToken<~String> The token returned by a previous call to indicate that there is more data available

  • StartData<~DateTime>: The starting date to retrieve alarm history

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/index.html?API_DescribeAlarmHistory.html



24
25
26
27
28
29
# File 'lib/fog/aws/requests/cloud_watch/describe_alarm_history.rb', line 24

def describe_alarm_history(options={})
  request({
      'Action'    => 'DescribeAlarmHistory',
      :parser     => Fog::Parsers::AWS::CloudWatch::DescribeAlarmHistory.new
    }.merge(options))
end

#describe_alarms(options = {}) ⇒ Object

Retrieves alarms with the specified names

Options

  • ActionPrefix<~String>: The action name prefix

  • AlarmNamePrefix<~String>: The alarm name prefix.

    AlarmNames cannot be specified if this parameter is specified
    
  • AlarmNames<~Array>: A list of alarm names to retrieve information for.

  • MaxRecords<~Integer>: The maximum number of alarm descriptions to retrieve

  • NextToken<~String>: The token returned by a previous call to indicate that there is more data available

  • NextToken<~String> The token returned by a previous call to indicate that there is more data available

  • StateValue<~String>: The state value to be used in matching alarms

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_DescribeAlarms.html



26
27
28
29
30
31
32
33
34
# File 'lib/fog/aws/requests/cloud_watch/describe_alarms.rb', line 26

def describe_alarms(options={})
  if alarm_names = options.delete('AlarmNames')
    options.merge!(AWS.indexed_param('AlarmNames.member.%d', [*alarm_names]))
  end
  request({
      'Action'    => 'DescribeAlarms',
      :parser     => Fog::Parsers::AWS::CloudWatch::DescribeAlarms.new
    }.merge(options))
end

#describe_alarms_for_metric(options) ⇒ Object

Retrieves all alarms for a single metric

Options

  • Dimensions<~Array>: a list of dimensions to filter against

    Name : The name of the dimension
    Value : The value to filter against
    
  • MetricName<~String>: The name of the metric

  • Namespace<~String>: The namespace of the metric

  • Period<~Integer>: The period in seconds over which the statistic is applied

  • Statistics<~String>: The statistic for the metric

  • Unit<~String> The unit for the metric

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_DescribeAlarms.html



26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aws/requests/cloud_watch/describe_alarms_for_metric.rb', line 26

def describe_alarms_for_metric(options)
  if dimensions = options.delete('Dimensions')
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
  end
  request({
      'Action'    => 'DescribeAlarmsForMetric',
      :parser     => Fog::Parsers::AWS::CloudWatch::DescribeAlarmsForMetric.new
    }.merge(options))
end

#disable_alarm_actions(alarm_names) ⇒ Object

Disables actions for the specified alarms

Options

  • AlarmNames<~Array>: The names of the alarms to disable actions for

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_DisableAlarmActions.html



19
20
21
22
23
24
25
26
# File 'lib/fog/aws/requests/cloud_watch/disable_alarm_actions.rb', line 19

def disable_alarm_actions(alarm_names)
  options = {}
  options.merge!(AWS.indexed_param('AlarmNames.member.%d', [*alarm_names]))
  request({
      'Action'    => 'DisableAlarmActions',
      :parser     => Fog::Parsers::AWS::CloudWatch::DisableAlarmActions.new
    }.merge(options))
end

#enable_alarm_actions(alarm_names) ⇒ Object

Enables actions for the specified alarms

Options

  • AlarmNames<~Array>: The names of the alarms to enable actions for

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_EnableAlarmActions.html



19
20
21
22
23
24
25
26
# File 'lib/fog/aws/requests/cloud_watch/enable_alarm_actions.rb', line 19

def enable_alarm_actions(alarm_names)
  options = {}
  options.merge!(AWS.indexed_param('AlarmNames.member.%d', [*alarm_names]))
  request({
      'Action'    => 'EnableAlarmActions',
      :parser     => Fog::Parsers::AWS::CloudWatch::EnableAlarmActions.new
    }.merge(options))
end

#get_metric_statistics(options = {}) ⇒ Object

Fetch datapoints for a metric. At most 1440 datapoints will be returned, the most datapoints that can be queried is 50850 StartTime is capped to 2 weeks ago

Options

  • Namespace<~String>: the namespace of the metric

  • MetricName<~String>: the name of the metric

  • StartTime<~Datetime>: when to start fetching datapoints from (inclusive)

  • EndTime<~Datetime>: used to determine the last datapoint to fetch (exclusive)

  • Period<~Integer>: Granularity, in seconds of the returned datapoints. Must be a multiple of 60, and at least 60

  • Statistics<~Array>: An array of up to 5 strings, which name the statistics to return

  • Unit<~String>: The unit for the metric

  • Dimensions<~Array>: a list of dimensions to filter against (optional)

    Name : The name of the dimension
    Value : The value to filter against
    

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb', line 27

def get_metric_statistics(options={})
  %w{Statistics StartTime EndTime Period MetricName Namespace}.each do |required_parameter|
    raise ArgumentError, "Must provide #{required_parameter}" unless options.has_key?(required_parameter)
  end
  statistics = options.delete 'Statistics'
  options.merge!(AWS.indexed_param('Statistics.member.%d', [*statistics]))

  if dimensions = options.delete('Dimensions')
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
  end

  request({
      'Action'    => 'GetMetricStatistics',
      :parser     => Fog::Parsers::AWS::CloudWatch::GetMetricStatistics.new
    }.merge(options))
end

#list_metrics(options = {}) ⇒ Object

List availabe metrics

Options

  • Dimensions<~Array>: a list of dimensions to filter against,

    Name : The name of the dimension
    Value : The value to filter against
    
  • MetricName<~String>: The name of the metric to filter against

  • Namespace<~String>: The namespace to filter against

  • NextToken<~String> The token returned by a previous call to indicate that there is more data available

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_ListMetrics.html



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/aws/requests/cloud_watch/list_metrics.rb', line 23

def list_metrics(options={})
  if dimensions = options.delete('Dimensions')
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
  end

  request({
      'Action'    => 'ListMetrics',
      :parser     => Fog::Parsers::AWS::CloudWatch::ListMetrics.new
    }.merge(options))
end

#put_metric_alarm(options) ⇒ Object

Creates or updates an alarm and associates it with the specified Amazon CloudWatch metric

Options

  • ActionsEnabled<~Boolean>: Indicates whether or not actions should be executed during any changes to the alarm’s state

  • AlarmActions<~Array>: A list of actions to execute

  • AlarmDescription<~String>: The description for the alarm

  • AlarmName<~String> The unique name for the alarm

  • ComparisonOperator<~String>: The arithmetic operation to use for comparison

  • Dimensions<~Array>: a list of dimensions to filter against,

    Name : The name of the dimension
    Value : The value to filter against
    
  • EvaluationPeriods<~Integer>: The number of periods over which data is compared to the specified threshold

  • InsufficientDataActions<~Array>: A list of actions to execute

  • MetricName<~String>: The name for the alarm’s associated metric

  • Namespace<~String>: The namespace for the alarm’s associated metric

  • OKActions<~Array>: A list of actions to execute

  • Period<~Integer>: The period in seconds over which the specified statistic is applied

  • Statistic<~String>: The statistic to apply to the alarm’s associated metric

  • Threshold<~Double>: The value against which the specified statistic is compared

  • Unit<~String>: The unit for the alarm’s associated metric

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/aws/requests/cloud_watch/put_metric_alarm.rb', line 33

def put_metric_alarm(options)
  if dimensions = options.delete('Dimensions')
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
    options.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
  end
  if alarm_actions = options.delete('AlarmActions')
    options.merge!(AWS.indexed_param('AlarmActions.member.%d', [*alarm_actions]))
  end
  if insufficient_data_actions = options.delete('InsufficientDataActions')
    options.merge!(AWS.indexed_param('InsufficientDataActions.member.%d', [*insufficient_data_actions]))
  end
  if ok_actions = options.delete('OKActions')
    options.merge!(AWS.indexed_param('OKActions.member.%d', [*ok_actions]))
  end

  request({
      'Action'    => 'PutMetricAlarm',
      :parser     => Fog::Parsers::AWS::CloudWatch::PutMetricAlarm.new
    }.merge(options))
end

#put_metric_data(namespace, metric_data) ⇒ Object

Publishes one or more data points to CloudWatch. A new metric is created if necessary

Options

  • Namespace<~String>: the namespace of the metric data

  • MetricData<~Array>: the datapoints to publish of the metric

    * MetricName<~String>: the name of the metric
    * Timestamp<~String>: the timestamp for the data point. If omitted defaults to the time at which the data is received by CloudWatch
    * Unit<~String>: the unit
    * Value<~Double> the value for the metric
    * StatisticValues<~Hash>:
        * Maximum<~Double>: the maximum value of the sample set
        * Sum<~Double>: the sum of the values of the sample set
        * SampleCount<~Double>: the number of samples used for the statistic set
        * Minimum<~Double>: the minimum value of the sample set
    * Dimensions<~Array>: the dimensions for the metric. From 0 to 10 may be included
         * Name<~String>
        * Value<~String>
    

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/requests/cloud_watch/put_metric_data.rb', line 31

def put_metric_data(namespace, metric_data)
  options = {'Namespace' => namespace}

  #first index the dimensions for any of the datums that have dimensions
  metric_data.collect! do |metric_datum|
    if dimensions = metric_datum.delete('Dimensions')
      metric_datum.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.collect {|dimension| dimension['Name']}))
      metric_datum.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.collect {|dimension| dimension['Value']}))
    end
    metric_datum
  end
  #then flatten out an hashes in the metric_data array
  metric_data.collect! { |metric_datum| flatten_hash(metric_datum) }
  #then index the metric_data array
  options.merge!(AWS.indexed_param('MetricData.member.%d', [*metric_data]))
  #then finally flatten out an hashes in the overall options array
  options = flatten_hash(options)

  request({
      'Action'    => 'PutMetricData',
      :parser     => Fog::Parsers::AWS::CloudWatch::PutMetricData.new
    }.merge(options))
end

#reloadObject



110
111
112
# File 'lib/fog/aws/cloud_watch.rb', line 110

def reload
  @connection.reset
end

#set_alarm_state(options) ⇒ Object

Temporarily sets the state of an alarm

Options

  • AlarmName<~String>: The names of the alarm

  • StateReason<~String>: The reason that this alarm is set to this specific state (in human-readable text format)

  • StateReasonData<~String>: The reason that this alarm is set to this specific state (in machine-readable JSON format)

  • StateValue<~String>: The value of the state

Returns

  • response<~Excon::Response>:

See Also

docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_SetAlarmState.html



22
23
24
25
26
27
# File 'lib/fog/aws/requests/cloud_watch/set_alarm_state.rb', line 22

def set_alarm_state(options)
  request({
      'Action'    => 'SetAlarmState',
      :parser     => Fog::Parsers::AWS::CloudWatch::SetAlarmState.new
    }.merge(options))
end