bin/checkfs.pjotr
changeset 4 a29ba54493af
equal deleted inserted replaced
3:bdbd5e99f85a 4:a29ba54493af
       
     1 #! /usr/bin/perl
       
     2 #use 5.10.0;
       
     3 use strict;
       
     4 use warnings;
       
     5 use Pod::Usage;
       
     6 use Getopt::Long;
       
     7 use Nagios;
       
     8 use Switch 'Perl6';
       
     9 
       
    10 my $fs;
       
    11 my $stdin = 0;
       
    12 
       
    13 GetOptions(
       
    14     "h|help" => sub { pod2usage(-exit => 0, -verbose => 1) },
       
    15     "m|man"  => sub {
       
    16         pod2usage(
       
    17             -exit      => 0,
       
    18             -verbose   => 2,
       
    19             -noperldoc => system("perldoc -V 2>/dev/null 1>&2")
       
    20         );
       
    21     },
       
    22     "s|stdin" => \$stdin,
       
    23 ) and $fs = shift;
       
    24 
       
    25 pod2usage if (!$fs && !$stdin);
       
    26 
       
    27 my @data;
       
    28 
       
    29 if ($stdin) {
       
    30    while (<STDIN>) {
       
    31       push @data, $_;
       
    32    }
       
    33 } else {
       
    34    @data = `df -P -B 1K $fs`;
       
    35 }
       
    36 
       
    37 analyze(\@data);
       
    38 
       
    39 sub analyze {
       
    40    my $data = shift;
       
    41    my ($fs, $blocks, $used, $avail, undef, $mp) = split /\s+/ => $data->[1];
       
    42    my $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB";
       
    43 
       
    44    my $free = $avail / $blocks;
       
    45 
       
    46    if ($free < 0.1)    { print "FS CRIT - $msg\n"; exit Nagios::CRITICAL }
       
    47    elsif ($free < 0.3) { print "FS WARN - $msg\n"; exit Nagios::WARNING }
       
    48    else                { print "FS OK - $msg\n";   exit Nagios::OK }
       
    49 
       
    50 
       
    51 #   given ($avail / $blocks) {
       
    52 #       when ($_ < 0.1) { print "FS CRIT - $msg\n"; exit Nagios::CRITICAL }
       
    53 #       when ($_ < 0.3) { print "FS WARN - $msg\n"; exit Nagios::WARNING }
       
    54 #       default         { print "FS OK - $msg\n";   exit Nagios::OK }
       
    55 #   }
       
    56 }
       
    57 
       
    58 __END__
       
    59 
       
    60 =head1 NAME
       
    61 
       
    62     checkfs - check the current file system
       
    63 
       
    64 =head1 SYNOPSIS
       
    65 
       
    66     checkfs [-h|--help]
       
    67     checkfs [-m|--man]
       
    68     checkfs <filesystem>
       
    69 
       
    70 =head1 DESCRIPTION
       
    71 
       
    72 B<checkfs> checks the filesystem.
       
    73 
       
    74 =head1 OPTIONS
       
    75 
       
    76 =over 4
       
    77 
       
    78 =item B<-h>|B<--help>
       
    79 
       
    80 =item B<-m>|B<--man>
       
    81 
       
    82 The usual help messages;
       
    83 
       
    84 =back
       
    85 
       
    86 
       
    87