Class: Aws::Mon

Inherits:
AwsBase show all
Includes:
AwsBaseInterface
Defined in:
lib/ec2/mon_interface.rb

Overview

This is the interface for Amazon CloudWatch.

Defined Under Namespace

Classes: QMonGetMetricStatistics, QMonListMetrics

Constant Summary collapse

API_VERSION =

Amazon EC2 API version being used

"2009-05-15"
DEFAULT_HOST =
"monitoring.amazonaws.com"
DEFAULT_PATH =
'/'
DEFAULT_PROTOCOL =
'https'
DEFAULT_PORT =
443
@@bench =
Aws::AwsBenchmarkingBlock.new
@@api =

Current API version (sometimes we have to check it outside the GEM).

ENV['EC2_API_VERSION'] || API_VERSION

Constants included from AwsBaseInterface

AwsBaseInterface::DEFAULT_SIGNATURE_VERSION

Constants inherited from AwsBase

AwsBase::AMAZON_PROBLEMS

Instance Attribute Summary

Attributes included from AwsBaseInterface

#aws_access_key_id, #cache, #last_errors, #last_request, #last_request_id, #last_response, #logger, #params, #signature_version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AwsBaseInterface

#cache_hits?, caching, caching=, #caching?, #close_conn, #close_connection, #connection, #escape_params, #generate_request2, #get_conn, #hash_params, #init, #multi_thread, #on_exception, #request_cache_or_info, #request_info2, #request_info3, #request_info_impl, #request_info_xml_simple, #request_info_xml_simple3, #signed_service_params, #symbolize, #update_cache

Methods inherited from AwsBase

amazon_problems, amazon_problems=

Constructor Details

#initialize(aws_access_key_id = nil, aws_secret_access_key = nil, params = {}) ⇒ Mon

Returns a new instance of Mon.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ec2/mon_interface.rb', line 47

def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
  init({:name             => 'MON',
        :default_host     => ENV['MON_URL'] ? URI.parse(ENV['MON_URL']).host : DEFAULT_HOST,
        :default_port     => ENV['MON_URL'] ? URI.parse(ENV['MON_URL']).port : DEFAULT_PORT,
        :default_service  => ENV['MON_URL'] ? URI.parse(ENV['MON_URL']).path : DEFAULT_PATH,
        :default_protocol => ENV['MON_URL'] ? URI.parse(ENV['MON_URL']).scheme : DEFAULT_PROTOCOL,
        :api_version      => API_VERSION},
       aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'],
       aws_secret_access_key|| ENV['AWS_SECRET_ACCESS_KEY'],
       params)
end

Class Method Details

.apiObject



42
43
44
# File 'lib/ec2/mon_interface.rb', line 42

def self.api
  @@api
end

.benchObject



27
28
29
# File 'lib/ec2/mon_interface.rb', line 27

def self.bench
  @@bench
end

.bench_ec2Object



35
36
37
# File 'lib/ec2/mon_interface.rb', line 35

def self.bench_ec2
  @@bench.service
end

.bench_xmlObject



31
32
33
# File 'lib/ec2/mon_interface.rb', line 31

def self.bench_xml
  @@bench.xml
end

.connection_nameObject



22
23
24
# File 'lib/ec2/mon_interface.rb', line 22

def self.connection_name
  :mon_connection
end

Instance Method Details

#generate_request(action, params = {}) ⇒ Object



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
# File 'lib/ec2/mon_interface.rb', line 60

def generate_request(action, params={})
  service_hash = {"Action"         => action,
                  "AWSAccessKeyId" => @aws_access_key_id,
                  "Version"        => @@api}
  service_hash.update(params)
  service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], @params[:service])

  # use POST method if the length of the query string is too large
  if service_params.size > 2000
    if signature_version == '2'
      # resign the request because HTTP verb is included into signature
      service_params = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], @params[:service])
    end
    request                 = Net::HTTP::Post.new(service)
    request.body            = service_params
    request['Content-Type'] = 'application/x-www-form-urlencoded'
  else
    request = Net::HTTP::Get.new("#{@params[:service]}?#{service_params}")
  end

  #puts "\n\n --------------- QUERY REQUEST TO AWS -------------- \n\n"
  #puts "#{@params[:service]}?#{service_params}\n\n"

  # prepare output hash
  {:request  => request,
   :server   => @params[:server],
   :port     => @params[:port],
   :protocol => @params[:protocol]}
end

#get_metric_statistics(measure_name, stats, start_time, end_time, unit, options = {}) ⇒ Object

measureName: CPUUtilization (Units: Percent), NetworkIn (Units: Bytes), NetworkOut (Units: Bytes), DiskWriteOps (Units: Count)

DiskReadBytes (Units: Bytes), DiskReadOps (Units: Count), DiskWriteBytes (Units: Bytes)

stats: array containing one or more of Minimum, Maximum, Sum, Average, Samples start_time : Timestamp to start end_time: Timestamp to end unit: Either Seconds, Percent, Bytes, Bits, Count, Bytes, Bits/Second, Count/Second, and None

Optional parameters:

period: 	Integer 60 or multiple of 60
dimensions:    Hash containing keys ImageId, AutoScalingGroupName, InstanceId, InstanceType
customUnit:   nil. not supported currently.
namespace:    AWS/EC2


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
161
162
163
164
165
166
167
# File 'lib/ec2/mon_interface.rb', line 133

def get_metric_statistics (measure_name, stats, start_time, end_time, unit, options={})

  period                = options[:period] || 60
  dimensions            = options[:dimensions] || nil
  custom_unit           = options[:custom_unit] || nil
  namespace             = options[:namespace] || "AWS/EC2"

  params                = {}
  params['MeasureName'] = measure_name
  i                     =1
  stats.each do |s|
    params['Statistics.member.'+i.to_s] = s
    i                                   = i+1
  end
  params['Period'] = period
  if (dimensions != nil)
    i = 1
    dimensions.each do |k, v|
      params['Dimensions.member.'+i.to_s+".Name."+i.to_s]  = k
      params['Dimensions.member.'+i.to_s+".Value."+i.to_s] = v
      i                                                    = i+1
    end
  end
  params['StartTime'] = start_time
  params['EndTime']   = end_time
  params['Unit']      = unit
  #params['CustomUnit'] = customUnit always nil
  params['Namespace'] = namespace

  link                = generate_request("GetMetricStatistics", params)
  resp                = request_info(link, QMonGetMetricStatistics.new(:logger => @logger))

rescue Exception
  on_exception
end

#list_metrics(options = {}) ⇒ Object


REQUESTS



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ec2/mon_interface.rb', line 103

def list_metrics(options={})

  next_token = options[:next_token] || nil

  params     = {}
  params['NextToken'] = next_token unless next_token.nil?

  @logger.info("list Metrics ")

  link = generate_request("ListMetrics", params)
  resp = request_info(link, QMonListMetrics.new(:logger => @logger))

rescue Exception
  on_exception
end

#request_info(request, parser, options = {}) ⇒ Object

Sends request to Amazon and parses the response Raises AwsError if any banana happened todo: remove this and switch to using request_info2



94
95
96
97
# File 'lib/ec2/mon_interface.rb', line 94

def request_info(request, parser, options={})
  conn = get_conn(self.class.connection_name, @params, @logger)
  request_info_impl(conn, @@bench, request, parser, options)
end