Browse Source

include bucket size in the bucket information

Chris Mague 7 years ago
parent
commit
4b92de0aa7
3 changed files with 37 additions and 1 deletions
  1. 6 1
      lib/neoinfra/aws.rb
  2. 30 0
      lib/neoinfra/cloudwatch.rb
  3. 1 0
      models/s3.rb

+ 6 - 1
lib/neoinfra/aws.rb

@@ -7,6 +7,7 @@ require 'fog'
 require 's3'
 require 'neo4j'
 require 'neoinfra/config'
+require 'neoinfra/cloudwatch'
 
 # NeoInfra Account information
 module NeoInfra
@@ -58,6 +59,7 @@ module NeoInfra
 
     def load_buckets
       @cfg = NeoInfra::Config.new
+      cw = NeoInfra::Cloudwatch.new
       neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
       Neo4j::Session.open(:server_db, neo4j_url)
       @cfg.accounts.each do |account|
@@ -69,7 +71,10 @@ module NeoInfra
         s = Fog::Storage.new(base_conf)
         s.directories.each do |bucket|
           next unless Bucket.where(name: bucket.key).empty?
-          b = Bucket.new(name: bucket.key)
+          b = Bucket.new(
+            name: bucket.key,
+            size: cw.get_bucket_size(account[:key], account[:secret], bucket.location, bucket.key,)
+          )
           b.save
           BucketRegion.create(from_node: b, to_node: Region.where(region: bucket.location).first )
           BucketAccount.create(from_node: b, to_node: AwsAccount.where(name: account[:name]).first )

+ 30 - 0
lib/neoinfra/cloudwatch.rb

@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+require 'fog'
+require 'neoinfra/cloudwatch'
+
+# NeoInfra Account information
+module NeoInfra
+  # Provide informations about the accounts available
+  class Cloudwatch
+    def get_bucket_size(key, secret, region, bucket)
+        conf = {
+          aws_access_key_id: key,
+          aws_secret_access_key: secret,
+          region: region
+        }
+        cwstats = Fog::AWS::CloudWatch.new(conf)
+        cwstats.get_metric_statistics({
+          'Statistics' => ['Maximum'],
+          'StartTime'  => DateTime.now-7,
+          'EndTime'    => DateTime.now,
+          'Period'     => 3600,
+          'MetricName' => 'BucketSizeBytes',
+          'Namespace'  => "AWS/S3",
+          'Dimensions' => [
+            {'Name' => 'BucketName','Value' => bucket},
+            {'Name' => 'StorageType','Value' => 'StandardStorage'}]
+        }).data[:body]['GetMetricStatisticsResult']['Datapoints'].last['Maximum']
+    end
+  end
+end

+ 1 - 0
models/s3.rb

@@ -6,6 +6,7 @@ require 'neo4j'
 class Bucket
   include Neo4j::ActiveNode
   property :name, constraint: :unique
+  property :size
   has_one :out, :region, rel_class: :BucketRegion
   has_one :out, :owner, rel_class: :BucketAccount
 end