From 0e719b1fb1d6956290b1c2a0416d7998dff836bf Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 30 Oct 2015 18:53:02 +0100 Subject: [PATCH] feature #4083: Add cloudwatch information to EC2 monitoring (cherry picked from commit afad1f05d53b20d63b6edf4b3aa9aca62a51215a) --- src/vmm_mad/remotes/ec2/ec2_driver.rb | 69 +++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/src/vmm_mad/remotes/ec2/ec2_driver.rb b/src/vmm_mad/remotes/ec2/ec2_driver.rb index f62deea805..a01381f5c7 100755 --- a/src/vmm_mad/remotes/ec2/ec2_driver.rb +++ b/src/vmm_mad/remotes/ec2/ec2_driver.rb @@ -443,10 +443,9 @@ private # Retrieve the vm information from the EC2 instance def parse_poll(instance) begin - info = "#{POLL_ATTRIBUTE[:memory]}=0 " \ - "#{POLL_ATTRIBUTE[:cpu]}=0 " \ - "#{POLL_ATTRIBUTE[:nettx]}=0 " \ - "#{POLL_ATTRIBUTE[:netrx]}=0 " + cloudwatch_str = cloudwatch_monitor_info(instance.instance_id) + + info = "#{POLL_ATTRIBUTE[:memory]}=0 #{cloudwatch_str}" state = "" if !instance.exists? @@ -612,5 +611,67 @@ private str end + + # Extract monitoring information from Cloud Watch + # CPU, NETTX and NETRX + def cloudwatch_monitor_info(id) + cw=AWS::CloudWatch::Client.new + + # CPU + begin + cpu = get_cloudwatch_metric(cw, + "CPUUtilization", + ["Average"], + "Percent", + id)[:datapoints][-1][:average] + rescue #  + cpu = 0 + end + + + # NETTX + nettx = 0 + begin + nettx_dp = get_cloudwatch_metric(cw, + "NetworkOut", + ["Sum"], + "Bytes", + id)[:datapoints] + nettx_dp.each{|dp| + nettx += dp[:sum].to_i + } + rescue   + end + + # NETRX + netrx = 0 + begin + netrx_dp = get_cloudwatch_metric(cw, + "NetworkIn", + ["Sum"], + "Bytes", + id)[:datapoints] + netrx_dp.each{|dp| + netrx += dp[:sum].to_i + } + rescue   + end + + "CPU=#{cpu.to_s} NETTX=#{nettx.to_s} NETRX=#{netrx.to_s} " + end + + # Get metric from AWS/EC2 namespace from the last 10 minutes + def get_cloudwatch_metric(cw, metric_name, statistics, units, id) + options={:namespace=>"AWS/EC2", + :metric_name=>metric_name, + :start_time=> (Time.now - 60*10).iso8601, + :end_time=> (Time.now).iso8601, + :period=>60, + :statistics=>statistics, + :unit=>units, + :dimensions=>[{:name=>"InstanceId", :value=>id}]} + + cw.get_metric_statistics(options) + end end