Projet

Général

Profil

Révision db184710

IDdb184710e59a3d487b80b0295b5305add8b650fa
Parent 73239ed2
Enfant 6374860f

Ajouté par Chris Wilson il y a plus de 12 ans

Fix line endings to Unix.

Standardise indentation, spacing and braces.

Allow overriding the list of buckets, default to all buckets.

Hide a lot of unwanted output from Curl.

Voir les différences:

plugins/s3/s3_storage
1
#!/usr/bin/perl
2

  
3
use strict;
4

  
5

  
6
my $s3_id = exists $ENV{'s3_id'} ? $ENV{'s3_id'} : "user";    
7
my $s3cmd = 's3curl.pl --id  ' . $s3_id . '  http://s3.amazonaws.com/';
8

  
9

  
10

  
11
if ( $ARGV[0] eq "autoconf" ) {
12
    if (`/usr/bin/perl $0` eq "" ) {
13
    print "no\n";
14
    exit 1;
15
    } else {
16
    print "yes\n";
17
    exit 0;
18
    }
19
}
20

  
21

  
22

  
23
sub get_bucket_list() {
24

  
25

  
26
    my $buckets = `$s3cmd`;
27

  
28
    my $str = $buckets;
29

  
30
    my @bucket_list = ();
31
    my $pos = 0;
32
    
33
    while ($str =~ /.<Name>([\w._-]+)<\/Name>/) {
34

  
35
        $bucket_list[$pos++] = $1;
36
        $str = $';
37
    } 
38

  
39
    return @bucket_list; 
40
}
41

  
42

  
43
sub get_bucket_stats {
44
my ($name) = @_;
45

  
46
    my $s3cmd_local = $s3cmd . $name;
47
    my $stats = `$s3cmd_local`;
48

  
49
    my %res;
50
    
51
    $res{'size'}  = 0;        
52
    $res{'count'} = 0;        
53
        
54
    while ($stats =~ /.<Size>([\w._-]+)<\/Size>/) {
55
        $stats = $';
56
        
57
        $res{'size'} += $1;
58
        $res{'count'}++;
59
    } 
60
    return %res;
61
}
62

  
63

  
64

  
65
if ( $ARGV[0] eq "config" ) {
66

  
67
    # The headers
68
    print "graph_title S3 Storage Usage\n";
69
    print "graph_category s3\n";
70
    print "graph_args --base 1024 -l 0\n";
71
    print "graph_vlabel bytes\n";
72
    print 'graph_info Plugin available at <a href="http://www.ohardt.com/dev/munin/">http://www.ohardt.com/dev/munin/</a>' . "\n";
73
    
74
    my @bucket_list = get_bucket_list();
75
    
76
    my $bucket_name;
77
    
78
    foreach $bucket_name ( @bucket_list ) {
79
 
80
        print $bucket_name . ".label Bucket " . $bucket_name . "\n";   
81
    
82
    } 
83

  
84
    exit;
85
    
86
    
87
}
88

  
89

  
90
my @bucket_list = get_bucket_list();
91

  
92
my $bucket_name;
93
    
94
foreach $bucket_name ( @bucket_list ) {
95
 
96
    my %stats = get_bucket_stats( $bucket_name );   
97
 
98
    print $bucket_name . ".value " . $stats{'size'} . "\n";
99
}
100
  
1
#!/usr/bin/perl
2

  
3
use strict;
4
use warnings;
5

  
6
my $s3_id = exists $ENV{'s3_id'} ? $ENV{'s3_id'} : "user";    
7
my $s3curl = "perl s3-curl/s3curl.pl --id $s3_id -- -s -S";
8

  
9
sub get_bucket_list()
10
{
11
    my $buckets = `$s3curl http://s3.amazonaws.com`;
12
    my $str = $buckets;
13
    my @bucket_list;
14
    
15
    while ($buckets =~ s/.<Name>([\w._-]+)<\/Name>//)
16
	 {
17
        push @bucket_list, $1;
18
    } 
19

  
20
    return @bucket_list; 
21
}
22

  
23
my @bucket_list = split /\s+/, ($ENV{'buckets'} || '');
24
if (not @bucket_list)
25
{
26
    @bucket_list = get_bucket_list();
27
}
28

  
29
if ($ARGV[0] and $ARGV[0] eq "autoconf")
30
{
31
    if (@bucket_list)
32
	 {
33
        print "yes\n";
34
        exit 0;
35
    }
36
	 else
37
	 {
38
        print "no\n";
39
        exit 1;
40
    }
41
}
42

  
43
sub get_bucket_stats
44
{
45
    my ($name) = @_;
46
    my $stats = `$s3curl http://$name.s3.amazonaws.com`;
47
    my %res;
48
    
49
    $res{'size'}  = 0;
50
        
51
    while ($stats =~ s/.<Size>([\w._-]+)<\/Size>//)
52
	 {
53
        $res{'size'} += $1;
54
		  $res{'count'}++;
55
    }
56

  
57
    return %res;
58
}
59

  
60
if ($ARGV[0] and $ARGV[0] eq "config")
61
{
62
    # The headers
63
    print "graph_title S3 Storage Usage\n";
64
    print "graph_category s3\n";
65
    print "graph_args --base 1024 -l 0\n";
66
    print "graph_vlabel bytes\n";
67
    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";
68
    
69
    foreach my $bucket_name (@bucket_list)
70
	 {
71
        print "$bucket_name.label Bucket $bucket_name\n";
72
    } 
73

  
74
    exit;
75
}
76

  
77
foreach my $bucket_name (@bucket_list)
78
{
79
    my %stats = get_bucket_stats($bucket_name);   
80
    print "$bucket_name.value " . $stats{'size'} . "\n";
81
}
82

  
83
exit 0;

Formats disponibles : Unified diff