root / plugins / disk / dirsizes @ 6c7ad652
Historique | Voir | Annoter | Télécharger (2,53 ko)
| 1 | b6c93cc7 | ian dobson | #!/usr/bin/perl |
|---|---|---|---|
| 2 | # -*- perl -*- |
||
| 3 | # |
||
| 4 | # |
||
| 5 | ############################################################################## |
||
| 6 | # |
||
| 7 | # |
||
| 8 | # This munin plugin watches the sizes of the given directories. |
||
| 9 | # @author Kevin Fischer |
||
| 10 | # @version 2010/08/05 |
||
| 11 | # @website http://kevin-fischer.de |
||
| 12 | # |
||
| 13 | # Copy this to your node's config file (default: plugin-conf.d/munin-node): |
||
| 14 | # [dirsizes] |
||
| 15 | # user root |
||
| 16 | # env.watchdirs /var/www,/tmp |
||
| 17 | # |
||
| 18 | # Change the env.watchdirs-variable according to your wishes. |
||
| 19 | 8589c6df | klemens | # DON'T FORGET TO RUN AS ROOT! |
| 20 | b6c93cc7 | ian dobson | # |
| 21 | # You can test this plugin by calling it with params "test" and your watchdirs: |
||
| 22 | # ./dirsizes test /dir1,/tmp/dir2 |
||
| 23 | # |
||
| 24 | 5fcad700 | Edward Plainview | # The directories can contain wildcards that are automatically expanded. |
| 25 | # |
||
| 26 | b6c93cc7 | ian dobson | # |
| 27 | ############################################################################## |
||
| 28 | # |
||
| 29 | |||
| 30 | use strict; |
||
| 31 | 879686e5 | Nicolas Joyard | use Munin::Plugin; |
| 32 | b6c93cc7 | ian dobson | my @watchdirs; |
| 33 | |||
| 34 | if ( exists $ARGV[0] and $ARGV[0] eq "test" ) {
|
||
| 35 | |||
| 36 | # Split the watchdirs string |
||
| 37 | @watchdirs = split( ",", $ARGV[1] ); |
||
| 38 | } |
||
| 39 | else {
|
||
| 40 | |||
| 41 | # If no dirs are given, exit. |
||
| 42 | if ( !defined( $ENV{"watchdirs"} ) ) {
|
||
| 43 | die "No directories given! See the manual at top of this plugin file."; |
||
| 44 | } |
||
| 45 | |||
| 46 | # Split the watchdirs string |
||
| 47 | @watchdirs = split( ",", $ENV{"watchdirs"} );
|
||
| 48 | } |
||
| 49 | |||
| 50 | 5fcad700 | Edward Plainview | # Glob all of the watchdirs. |
| 51 | my @globbed_watchdirs; |
||
| 52 | foreach my $watchdir ( @watchdirs ) |
||
| 53 | {
|
||
| 54 | foreach my $expanded_dir ( glob( $watchdir ) ) |
||
| 55 | {
|
||
| 56 | push @globbed_watchdirs, $expanded_dir; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | @watchdirs = @globbed_watchdirs; |
||
| 60 | |||
| 61 | b6c93cc7 | ian dobson | # Config or read request? |
| 62 | if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {
|
||
| 63 | |||
| 64 | # Munin basic info |
||
| 65 | print "graph_title Directory sizes\n"; |
||
| 66 | print "graph_args --base 1024 --lower-limit 0\n"; |
||
| 67 | print "graph_vlabel directory size\n"; |
||
| 68 | print "graph_info Displays the sizes of all configured directories.\n"; |
||
| 69 | print "graph_category disk\n"; |
||
| 70 | print "graph_total Total\n"; |
||
| 71 | |||
| 72 | # All available directories |
||
| 73 | foreach my $dir (@watchdirs) {
|
||
| 74 | |||
| 75 | # Remove illegal characters |
||
| 76 | 879686e5 | Nicolas Joyard | my $label = clean_fieldname($dir); |
| 77 | b6c93cc7 | ian dobson | |
| 78 | # Print name |
||
| 79 | print "dir", $label, ".label ", $dir, "\n"; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | # Read request, output the directory sizes |
||
| 84 | else {
|
||
| 85 | |||
| 86 | # All available directories |
||
| 87 | foreach my $dir (@watchdirs) {
|
||
| 88 | # Remove illegal characters |
||
| 89 | 879686e5 | Nicolas Joyard | my $label = clean_fieldname($dir); |
| 90 | b6c93cc7 | ian dobson | |
| 91 | # Get the dirsize |
||
| 92 | my $dirsize = getSize($dir); |
||
| 93 | |||
| 94 | # Print name |
||
| 95 | print "dir", $label, ".value ", $dirsize, ".0\n"; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | # Function: getSize($dir) |
||
| 100 | sub getSize {
|
||
| 101 | my ($dir) = @_; |
||
| 102 | |||
| 103 | # Get the size via `du` |
||
| 104 | 5fcad700 | Edward Plainview | my @dirsize = split( ' ', `du -cb "$dir" | grep "total" | tail -1 ` ); |
| 105 | b6c93cc7 | ian dobson | return @dirsize[0]; |
| 106 | } |
||
| 107 | |||
| 108 | exit 0; |
