2013-07-03 19:14:36 +02:00
#!/usr/bin/perl
use strict;
use warnings;
2018-08-02 02:03:42 +02:00
my $s3_id = exists $ENV{'s3_id'} ? $ENV{'s3_id'} : "user";
2013-07-03 19:14:36 +02:00
my $s3curl = "perl s3-curl/s3curl.pl --id $s3_id -- -s -S";
sub get_bucket_list()
{
my $buckets = `$s3curl http://s3.amazonaws.com`;
my $str = $buckets;
my @bucket_list;
2018-08-02 02:03:42 +02:00
2013-07-03 19:14:36 +02:00
while ($buckets =~ s/.<Name>([\w._-]+)<\/Name>//)
{
push @bucket_list, $1;
2018-08-02 02:03:42 +02:00
}
2013-07-03 19:14:36 +02:00
2018-08-02 02:03:42 +02:00
return @bucket_list;
2013-07-03 19:14:36 +02:00
}
my @bucket_list = split /\s+/, ($ENV{'buckets'} || '');
if (not @bucket_list)
{
@bucket_list = get_bucket_list();
}
if ($ARGV[0] and $ARGV[0] eq "autoconf")
{
if (@bucket_list)
{
print "yes\n";
exit 0;
}
else
{
print "no\n";
exit 1;
}
}
sub get_bucket_stats
{
my ($name) = @_;
my $stats = `$s3curl http://$name.s3.amazonaws.com`;
my %res;
2018-08-02 02:03:42 +02:00
2013-07-03 19:14:36 +02:00
$res{'size'} = 0;
2018-08-02 02:03:42 +02:00
2013-07-03 19:14:36 +02:00
while ($stats =~ s/.<Size>([\w._-]+)<\/Size>//)
{
$res{'size'} += $1;
$res{'count'}++;
}
return %res;
}
if ($ARGV[0] and $ARGV[0] eq "config")
{
# The headers
print "graph_title S3 Storage Usage\n";
2017-02-20 22:14:23 +01:00
print "graph_category cloud\n";
2013-07-03 19:14:36 +02:00
print "graph_args --base 1024 -l 0\n";
print "graph_vlabel bytes\n";
print "graph_info Plugin available at <a href='https://github.com/aptivate/munin-contrib/blob/master/plugins/s3/s3_storage'>https://github.com/aptivate/munin-contrib/blob/master/plugins/s3/s3_storage</a>\n";
2018-08-02 02:03:42 +02:00
2013-07-03 19:14:36 +02:00
foreach my $bucket_name (@bucket_list)
{
print "$bucket_name.label Bucket $bucket_name\n";
2018-08-02 02:03:42 +02:00
}
2013-07-03 19:14:36 +02:00
exit;
}
foreach my $bucket_name (@bucket_list)
{
2018-08-02 02:03:42 +02:00
my %stats = get_bucket_stats($bucket_name);
2013-07-03 19:14:36 +02:00
print "$bucket_name.value " . $stats{'size'} . "\n";
}
exit 0;