[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/share/se3/scripts-alertes/ -> check_disk_all (source)

   1  #!/usr/bin/perl
   2  # -*- cperl -*-
   3  # $Id: check_disk_all,v 1.1 2006/06/20 15:08:28 janl Exp $
   4  
   5  =head1 NAME
   6  
   7  check_disk_all - check for available disk-space on all disks
   8  
   9  =head1 SYNOPSIS
  10  
  11  check_disk_all [OPTIONS][devices]
  12  
  13  =head1 OPTIONS
  14  
  15  =over 8
  16  
  17  =item B<--df> PATH_TO_DF
  18  
  19  Use this df insted of the default. The default is /bin/df
  20  
  21  =item B<--dfoptions> OPTIONS
  22  
  23  Pass these options to df instead of defaults. Defaults are "-Plk" on
  24  Linux and SunOS "lk" on anything else.  GNU df may be required for the
  25  "-P" option.
  26  
  27  =item B<--special> DEVICE=WARN,CRIT
  28  
  29  Set limits for warnings/critical errors for this device.
  30  
  31  =item B<--exclude> DEVICE
  32  
  33  Exclude this device
  34  
  35  =item B<--inode>
  36  
  37  Also check available inodes. (Require gnu df. Use the --df option)
  38  
  39  =item B<--only> DEVICE
  40  
  41  Check only this device
  42  
  43  =item B<--help>
  44  
  45  Help.
  46  
  47  =item B<--man>
  48  
  49  Show man-page.
  50  
  51  =head1 DESCRIPTION
  52  
  53  Parses output from either GNU df or solaris df.
  54  
  55  =head1 LIMITATIONS
  56  
  57  Not tested on other OSes then Debian GNU/Linux and Solaris. Most of the
  58  unixen should have a "df" thats produces POSIX-compliant output (if
  59  there is such a thing, GNU df talk of this).
  60  
  61  =head1 AUTHORS
  62  
  63  Per Andreas Buer <perbu@linpro.no>.
  64  
  65  Bugfixing by Andreas Dahl <andread@linpro.no> and Nicolai Langfeldt
  66  <janl@linpro.no>
  67  
  68  =back
  69  
  70  =cut
  71  
  72  
  73  use strict;
  74  use warnings;
  75  use Getopt::Long;
  76  use Data::Dumper;
  77  use Pod::Usage;
  78  use POSIX;
  79  
  80  use constant CRIT => 2;
  81  use constant WARN => 1;
  82  use constant OK => 0;
  83  
  84  my %OPTS;
  85  
  86  
  87  my %SPECIAL;
  88  my %EXCLUDES;
  89  my %ONLY;
  90  
  91  GetOptions( \%OPTS, "warn=i", "crit=i",
  92              "dfoptions=s",
  93              "df=s",
  94              "special=s" => \%SPECIAL,
  95              "exclude=s" => \%EXCLUDES,
  96              "only=s" => \%ONLY,
  97              "help",
  98              "man",
  99              "inode"
 100            ) or pod2usage( -verbose=> 0,
 101                            -message => "Try '$0 --help' for more help.\n"
 102                          );
 103  
 104  pod2usage( -verbose => 1 ) if ($OPTS{help});
 105  pod2usage( -verbose => 2 ) if ($OPTS{man});
 106  
 107  # defaults
 108  
 109  $OPTS{warn} ||= 20;
 110  $OPTS{crit} ||= 10;
 111  
 112  my %DF = get_df();
 113  
 114  my (@warn, @crit);
 115  
 116  if (scalar @ARGV) {
 117    for my $dev (@ARGV) {
 118  
 119      if (not defined($DF{$dev})) {
 120        die("Unknown filesystem: $dev\n");
 121      }
 122  
 123      my $rv = check($dev);
 124      if ($rv == CRIT) {
 125        push(@crit, $dev);
 126      } elsif ($rv == WARN) {
 127        push(@warn, $dev);
 128      }
 129    }
 130  }
 131  else {
 132    # check all filesystems.
 133    for my $dev (sort keys %DF) {
 134      next if (defined( $EXCLUDES{$dev} ));
 135      next if (%ONLY && ! defined( $ONLY{$dev} ));
 136      my $rv = check($dev);
 137  
 138      if ($rv == CRIT) {
 139        push(@crit, $dev);
 140      } elsif ($rv == WARN) {
 141        push(@warn, $dev);
 142      }
 143    }
 144  }
 145  
 146  if (@crit) {
 147    print("CRITICAL (", scalar @crit , " filesystems) ",
 148          join(", ",
 149               map { "$_ ($DF{$_}->{free}% free [$DF{$_}->{ifree}\% inodes])"
 150                         } @crit ), "\n");
 151    exit(CRIT);
 152  
 153  } elsif (@warn) {
 154    print("WARNING (", scalar @warn , " filesystems) ",
 155          join(", ",
 156               map { "$_ ($DF{$_}->{free}% free [$DF{$_}->{ifree}\% inodes])"
 157                         } @warn ), "\n");
 158    exit(WARN);
 159  } else {
 160    print("OK - All checked filsystems OK\n");
 161    exit(OK);
 162  }
 163  
 164  
 165  
 166  sub check {
 167    my ($dev) = @_;
 168    my ($warn, $crit);
 169    # special device
 170    if (defined( $SPECIAL{$dev} )) {
 171      ($warn,$crit) = split(/,/, $SPECIAL{$dev})
 172    } else {
 173      ($warn, $crit) = ($OPTS{warn}, $OPTS{crit} );
 174    }
 175  
 176    if ( $DF{$dev}->{free} < $crit ||
 177         ($OPTS{inode} && $DF{$dev}->{ifree} < $crit ) ) {
 178      return CRIT;
 179    } elsif ( $DF{$dev}->{free} < $warn ||
 180              ($OPTS{inode} && $DF{$dev}->{ifree} < $warn ) ) {
 181      return WARN;
 182    }
 183  
 184    OK;
 185  }
 186  
 187  
 188  
 189  sub get_df {
 190    my %TMP;
 191  
 192    my $df = $OPTS{df} || find_df();
 193    my $options = $OPTS{dfoptions} || find_df_options();
 194  
 195    # Space
 196    open(DF,"$df -$options |");
 197    while(<DF>) {
 198      next if (not m,^(:?/|swap|\w+\:),);
 199    resplit:
 200      my ($dev, $size, $used, $avail, $cap, $mnt) = split '\s+';
 201      # Sometimes df continuates the output on the next line.
 202      if (!defined($size) || !$size) {
 203        chomp $_; # Eat the newline so that the concatenation is good
 204        $_ .= <DF>;
 205        goto resplit;
 206      }
 207      next if ($dev =~ m,^(:?/proc|fd|mnttab|none)$,);
 208      $TMP{$dev} = {
 209                    size => $size,
 210                    used => $used,
 211                    avail => $avail,
 212                    cap => $cap,
 213                    mnt => $mnt,
 214                    ifree => "--", # Will be replaced if inodes are checked
 215                    free =>
 216                    ($size == 0)? 0 : sprintf("%2.0f", 100 * $avail / $size),
 217                   };
 218    }
 219    close(DF);
 220    # Inodes
 221    if ($OPTS{inode}){
 222      open(DF,"$df --inode -$options |");
 223      while(<DF>) {
 224        next if (not m,^(:?/|swap|\w+\:),);
 225        my ($dev, $size, $used, $avail, $cap, $mnt) = split '\s+';
 226        next if ($dev =~ m,^(:?/proc|fd|mnttab)$,);
 227        $TMP{$dev}{'icap'} = $cap;
 228        if ( $cap =~ m/(\d+)\%/ ){
 229          $TMP{$dev}{'ifree'} = 100 - $1;
 230        }
 231        else {
 232          $TMP{$dev}{'ifree'} = 0;
 233        }
 234      }
 235      close(DF);
 236    }
 237    return %TMP;
 238  }
 239  
 240  sub find_df {
 241    my $uname = POSIX::uname();
 242    if ($uname eq 'SunOS') {
 243      return '/usr/local/bin/df';
 244    } else {
 245      # default
 246      return '/bin/df';
 247    }
 248  }
 249  
 250  sub find_df_options {
 251    my $uname = POSIX::uname();
 252  
 253    if ($uname eq 'Linux' or $uname eq 'SunOS') {
 254      return 'Plk';
 255    } else {
 256      # default
 257      return 'lk';
 258    }
 259  
 260    # .. add you own hacks here
 261  
 262  }


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1