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

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/cloud_watch.rb,
lib/fog/aws/requests/cloud_watch/list_metrics.rb,
lib/fog/aws/requests/cloud_watch/put_metric_data.rb,
lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb

Instance Method Summary collapse

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, in [‘eu-west-1’, ‘us-east-1’, ‘us-west-1’, ‘ap-southeast-1’, ‘ap-northeast-1’]

Returns

  • CloudWatch object with connection to AWS.



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
# File 'lib/fog/aws/cloud_watch.rb', line 48

def initialize(options={})
  @aws_access_key_id      = options[:aws_access_key_id]
  @aws_secret_access_key  = options[:aws_secret_access_key]
  @hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)

  options[:region] ||= 'us-east-1'
  @host = options[:host] || case options[:region]
  when 'ap-northeast-1'
    'monitoring.ap-northeast-1.amazonaws.com'
  when 'ap-southeast-1'
    'monitoring.ap-southeast-1.amazonaws.com'
  when 'eu-west-1'
    'monitoring.eu-west-1.amazonaws.com'
  when 'us-east-1'
    'monitoring.us-east-1.amazonaws.com'
  when 'us-west-1'
    'monitoring.us-west-1.amazonaws.com'
  else
    raise ArgumentError, "Unknown region: #{options[:region].inspect}"
  end
  @path       = options[:path]      || '/'
  @port       = options[:port]      || 443
  @scheme     = options[:scheme]    || 'https'
  @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", options[:persistent])
end

Instance Method Details

#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 prodide #{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_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



74
75
76
# File 'lib/fog/aws/cloud_watch.rb', line 74

def reload
  @connection.reset
end