Search code examples
rubyamazon-web-servicesamazon-s3sensu

Trying to find a Sensu plugin AWS S3


I'm trying to find or create a Sensu plugin to count AWS S3 objects within a folder. For example,

All buckets/test1/test2/

I want to know how many objects are within test2 and alert me if the number goes above the threshold.

I found this, but I'n not able to get it to work.

# by default you only get 1000 objects at a time
# so you have to roll your own cursor

S3.connect!

objects = []
last_key = nil
begin
  new_objects = AWS::S3::Bucket.objects(bucket_name, :marker => last_key)
  objects    += new_objects
  last_key    = objects.last.key
end while new_objects.size > 0

# you can easily define the above as an all_objects method on   AWS::S3::Bucket

If anyone knows a different way to do this, please let me know.

Thank you

Devon


Solution

  • I decided to go a different route, I used this code to accomplish what I wanted to do.

    #!/bin/bash
    value=$(aws s3 ls bucket/dir1/dir2/ -- recursive --human-readable --summarize | grep .file type | wc -l)
    if [ $value -gt 1000 ];
    then
          echo "$value Warning"
          exit 2
    fi
    

    Thank you Everyone for the help

    Devon