ソースを参照

also grab cpu information

Chris Mague 6 年 前
コミット
c56503d92d
4 ファイル変更65 行追加0 行削除
  1. 1 0
      lib/neoinfra.rb
  2. 33 0
      lib/neoinfra/cloudwatch.rb
  3. 10 0
      lib/neoinfra/nodes.rb
  4. 21 0
      models/node_stats.rb

+ 1 - 0
lib/neoinfra.rb

@@ -16,4 +16,5 @@ module NeoInfra
   require 'neoinfra/aws'
   require 'neoinfra/vpcs'
   require 'neoinfra/nodes'
+  require 'neoinfra/cloudwatch'
 end

+ 33 - 0
lib/neoinfra/cloudwatch.rb

@@ -7,6 +7,39 @@ require 'neoinfra/cloudwatch'
 module NeoInfra
   # Provide informations about the accounts available
   class Cloudwatch
+    def get_instance_cpu(key, secret, region, instance_id)
+      begin
+        stats = {:cpu_avg => -1, :cpu_max => -1}
+        cwstats = Fog::AWS::CloudWatch.new(
+                          {
+                              :region                => region,
+                              :aws_access_key_id     => key,
+                              :aws_secret_access_key => secret,
+                            }
+        )
+        cpu_stats=cwstats.get_metric_statistics(
+          { 'Dimensions' => [ {'Name' => 'InstanceId', 'Value' => instance_id} ],
+            'Namespace'  => 'AWS/EC2',
+            'MetricName' => 'CPUUtilization',
+            'Statistics' => ['Average'],
+            'EndTime'    => DateTime.now,
+            'StartTime'  => DateTime.now-7,
+            'Period'     => 3600
+        }).data[:body]['GetMetricStatisticsResult']['Datapoints'].collect{|r| r['Average']}
+        if cpu_stats.size > 0
+          stats[:cpu_max] = cpu_stats.max
+          stats[:cpu_avg] = cpu_stats.inject(0.0) { |sum, el| sum + el } / cpu_stats.size
+        else
+          stats[:cpu_max] = 0
+          stats[:cpu_avg] = 0
+        end
+        return stats
+      rescue Exception => e
+        puts "ERR: #{e.message}: #{account[:name]},#{region} #{base_conf.inspect}"
+        return stats
+      end
+    end
+
     def get_bucket_size(key, secret, region, bucket)
       conf = {
         aws_access_key_id: key,

+ 10 - 0
lib/neoinfra/nodes.rb

@@ -3,6 +3,7 @@
 require 'neo4j'
 require 'neoinfra'
 require 'nodes'
+require 'node_stats'
 require 'accounts'
 require 'fog-aws'
 
@@ -104,11 +105,20 @@ module NeoInfra
               ami: ec2.image_id
             )
             n.save
+            cw = NeoInfra::Cloudwatch.new
+            stats = cw.get_instance_cpu(account[:key], account[:secret], region, ec2.id)
+            s = NodeStats.new(
+              node_id: ec2.id,
+              cpu_max: stats[:cpu_max],
+              cpu_avg: stats[:cpu_avg],
+            )
+            s.save
             begin
               NodeAccount.create(from_node: n, to_node: AwsAccount.where(name: account[:name]).first)
               NodeAz.create(from_node: n, to_node: Az.where(az: ec2.availability_zone).first)
               NodeSshKey.create(from_node: n, to_node: SshKey.where(name: ec2.key_name).first)
               NodeSubnet.create(from_node: n, to_node: Subnet.where(subnet_id: ec2.subnet_id).first)
+              Node2Stats.create(from_node: n, to_node: s)
             rescue Exception => e
               puts "Account #{account[:name]} couldn't load the following node: #{e.message}"
               p n

+ 21 - 0
models/node_stats.rb

@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+require 'neo4j'
+
+# Node setup
+class NodeStats
+  include Neo4j::ActiveNode
+  property :node_id, constraint: :unique
+  property :cpu_max
+  property :cpu_avg
+  property :mem_max
+  property :mem_avg
+  has_one :out, :node, rel_class: :Node2Stats
+end
+
+class Node2Stats
+  include Neo4j::ActiveRel
+  from_class :Node
+  to_class   :NodeStatus
+  type       :node
+end