cloudwatch.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. # frozen_string_literal: true
  2. require 'fog-aws'
  3. require 'neoinfra/cloudwatch'
  4. # NeoInfra Account information
  5. module NeoInfra
  6. # Provide informations about the accounts available
  7. class Cloudwatch
  8. def get_bucket_size(key, secret, region, bucket)
  9. conf = {
  10. aws_access_key_id: key,
  11. aws_secret_access_key: secret,
  12. region: region
  13. }
  14. cwstats = Fog::AWS::CloudWatch.new(conf)
  15. begin
  16. cwstats.get_metric_statistics('Statistics' => ['Maximum'],
  17. 'StartTime' => DateTime.now - 7,
  18. 'EndTime' => DateTime.now,
  19. 'Period' => 3600,
  20. 'MetricName' => 'BucketSizeBytes',
  21. 'Namespace' => 'AWS/S3',
  22. 'Dimensions' => [
  23. { 'Name' => 'BucketName', 'Value' => bucket },
  24. { 'Name' => 'StorageType', 'Value' => 'StandardStorage' }
  25. ]).data[:body]['GetMetricStatisticsResult']['Datapoints'].last['Maximum']
  26. rescue
  27. puts "Unable to get stats for #{bucket} returning -1"
  28. return -1
  29. end
  30. end
  31. end
  32. end