From 5a1789c182aea4011b98bf40dc099849d0ef6935 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Mon, 16 May 2011 10:48:09 +0100 Subject: [PATCH] add get/put/list metrics --- lib/fog/aws/cloud_watch.rb | 3 ++ .../cloud_watch/get_metric_statistics.rb | 40 ++++++++++++++ .../aws/parsers/cloud_watch/list_metrics.rb | 54 +++++++++++++++++++ .../parsers/cloud_watch/put_metric_data.rb | 26 +++++++++ .../cloud_watch/get_metric_statistics.rb | 45 ++++++++++++++++ .../aws/requests/cloud_watch/list_metrics.rb | 38 +++++++++++++ .../requests/cloud_watch/put_metric_data.rb | 48 +++++++++++++++++ lib/fog/providers/aws.rb | 1 + 8 files changed, 255 insertions(+) create mode 100644 lib/fog/aws/parsers/cloud_watch/get_metric_statistics.rb create mode 100644 lib/fog/aws/parsers/cloud_watch/list_metrics.rb create mode 100644 lib/fog/aws/parsers/cloud_watch/put_metric_data.rb create mode 100644 lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb create mode 100644 lib/fog/aws/requests/cloud_watch/list_metrics.rb create mode 100644 lib/fog/aws/requests/cloud_watch/put_metric_data.rb diff --git a/lib/fog/aws/cloud_watch.rb b/lib/fog/aws/cloud_watch.rb index 1b70d934b..adcf19587 100644 --- a/lib/fog/aws/cloud_watch.rb +++ b/lib/fog/aws/cloud_watch.rb @@ -8,6 +8,9 @@ module Fog request_path 'fog/aws/requests/cloud_watch' + request :list_metrics + request :get_metric_statistics + request :put_metric_data class Mock diff --git a/lib/fog/aws/parsers/cloud_watch/get_metric_statistics.rb b/lib/fog/aws/parsers/cloud_watch/get_metric_statistics.rb new file mode 100644 index 000000000..b9c4dee8a --- /dev/null +++ b/lib/fog/aws/parsers/cloud_watch/get_metric_statistics.rb @@ -0,0 +1,40 @@ +module Fog + module Parsers + module AWS + module CloudWatch + + class GetMetricStatisticsResult < Fog::Parsers::Base + + def reset + @response = { 'GetMetricStatisticsResult' => {'Datapoints' => []}, 'ResponseMetadata' => {} } + reset_datapoint + end + + def reset_datapoint + @datapoint = {} + end + + def start_element(name, attrs = []) + super + end + + def end_element(name) + case name + when 'Average', 'Maximum', 'Minimum', 'SampleCount', 'Sum' + @datapoint[name] = @value.to_f + when 'Unit' + @datapoint[name] = @value + when 'Timestamp' + @datapoint[name] = Time.parse @value + when 'Datapoint' + @response['GetMetricStatisticsResult']['Datapoints'] << @datapoint + reset_datapoint + when 'RequestId' + @response['ResponseMetadata'][name] = @value + end + end + end + end + end + end +end diff --git a/lib/fog/aws/parsers/cloud_watch/list_metrics.rb b/lib/fog/aws/parsers/cloud_watch/list_metrics.rb new file mode 100644 index 000000000..1f7c73d9b --- /dev/null +++ b/lib/fog/aws/parsers/cloud_watch/list_metrics.rb @@ -0,0 +1,54 @@ +module Fog + module Parsers + module AWS + module CloudWatch + + class ListMetrics < Fog::Parsers::Base + + def reset + @response = { 'ListMetricsResult' => {'Metrics' => []}, 'ResponseMetadata' => {} } + reset_metric + end + + def reset_metric + @metric = {'Dimensions' => []} + end + + def reset_dimension + @dimension = {} + end + + def start_element(name, attrs = []) + super + case name + when 'Dimension' + reset_dimension + end + end + + def end_element(name) + case name + when 'Name' + @dimension['Name'] = @value + when 'Value' + @dimension['Value'] = @value + when 'Namespace' + @metric['Namespace'] = @value + when 'MetricName' + @metric['MetricName'] = @value + when 'Dimension' + @metric['Dimensions'] << @dimension + when 'Metric' + @response['ListMetricsResult']['Metrics'] << @metric + reset_metric + when 'NextMarker' + @response['ListMetricsResult'][name] = @value + when 'RequestId' + @response['ResponseMetadata'][name] = @value + end + end + end + end + end + end +end diff --git a/lib/fog/aws/parsers/cloud_watch/put_metric_data.rb b/lib/fog/aws/parsers/cloud_watch/put_metric_data.rb new file mode 100644 index 000000000..12bdfbc72 --- /dev/null +++ b/lib/fog/aws/parsers/cloud_watch/put_metric_data.rb @@ -0,0 +1,26 @@ +module Fog + module Parsers + module AWS + module CloudWatch + + class PutMetricData < Fog::Parsers::Base + + def reset + @response = { 'ResponseMetadata' => {} } + end + + def start_element(name, attrs = []) + super + end + + def end_element(name) + case name + when 'RequestId' + @response['ResponseMetadata'][name] = @value + end + end + end + end + end + end +end diff --git a/lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb b/lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb new file mode 100644 index 000000000..07f57b520 --- /dev/null +++ b/lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb @@ -0,0 +1,45 @@ +module Fog + module AWS + class CloudWatch + class Real + + require 'fog/aws/parsers/cloud_watch/get_metric_statistics' + + # 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 + # http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html + # + def get_metric_statistics(options={}) + 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 + + end + end + end +end diff --git a/lib/fog/aws/requests/cloud_watch/list_metrics.rb b/lib/fog/aws/requests/cloud_watch/list_metrics.rb new file mode 100644 index 000000000..47a8c3840 --- /dev/null +++ b/lib/fog/aws/requests/cloud_watch/list_metrics.rb @@ -0,0 +1,38 @@ +module Fog + module AWS + class CloudWatch + class Real + + require 'fog/aws/parsers/cloud_watch/list_metrics' + + # 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 + # http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_ListMetrics.html + # + 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 + + end + end + end +end diff --git a/lib/fog/aws/requests/cloud_watch/put_metric_data.rb b/lib/fog/aws/requests/cloud_watch/put_metric_data.rb new file mode 100644 index 000000000..3e05ac6f4 --- /dev/null +++ b/lib/fog/aws/requests/cloud_watch/put_metric_data.rb @@ -0,0 +1,48 @@ +module Fog + module AWS + class CloudWatch + class Real + + require 'fog/aws/parsers/cloud_watch/put_metric_data' + + # 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 + # http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html + # + def put_metric_data(namespace, metric_data) + 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 + + end + end + end +end diff --git a/lib/fog/providers/aws.rb b/lib/fog/providers/aws.rb index 72e87be72..6a39eecf6 100644 --- a/lib/fog/providers/aws.rb +++ b/lib/fog/providers/aws.rb @@ -9,6 +9,7 @@ module Fog service(:cdn, 'cdn/aws') service(:compute, 'compute/aws') service(:cloud_formation, 'aws/cloud_formation') + service(:cloud_watch, 'aws/cloud_watch') service(:dns, 'dns/aws') service(:elb, 'aws/elb') service(:iam, 'aws/iam')