mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
add get/put/list metrics
This commit is contained in:
parent
99516e62e1
commit
5a1789c182
8 changed files with 255 additions and 0 deletions
|
@ -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
|
||||
|
||||
|
|
40
lib/fog/aws/parsers/cloud_watch/get_metric_statistics.rb
Normal file
40
lib/fog/aws/parsers/cloud_watch/get_metric_statistics.rb
Normal file
|
@ -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
|
54
lib/fog/aws/parsers/cloud_watch/list_metrics.rb
Normal file
54
lib/fog/aws/parsers/cloud_watch/list_metrics.rb
Normal file
|
@ -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
|
26
lib/fog/aws/parsers/cloud_watch/put_metric_data.rb
Normal file
26
lib/fog/aws/parsers/cloud_watch/put_metric_data.rb
Normal file
|
@ -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
|
45
lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb
Normal file
45
lib/fog/aws/requests/cloud_watch/get_metric_statistics.rb
Normal file
|
@ -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
|
38
lib/fog/aws/requests/cloud_watch/list_metrics.rb
Normal file
38
lib/fog/aws/requests/cloud_watch/list_metrics.rb
Normal file
|
@ -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
|
48
lib/fog/aws/requests/cloud_watch/put_metric_data.rb
Normal file
48
lib/fog/aws/requests/cloud_watch/put_metric_data.rb
Normal file
|
@ -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
|
|
@ -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')
|
||||
|
|
Loading…
Reference in a new issue