#!/usr/bin/perl

# check_writable is a Nagios plugin that checks if one or more
# directories are writable by:
#
# - checking that the supplied directory is indeed a directory
# - checking if the the filesystem permissions are OK
# - creating a temporary file
# - writing random data to the temporary file (and reading it back)
#
# It return a critical status if one of the tests fails
#
# See  the INSTALL file for installation instructions
#
# Copyright (c) 2007, ETH Zurich.
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU general public license (gpl) version 3.
# See the LICENSE file for details.
#
# RCS information
# enable substitution with:
#   $ svn propset svn:keywords "Id Revision HeadURL Source Date"
#
#   $Id: check_writable 868 2007-11-13 13:47:43Z corti $
#   $Revision: 868 $
#   $HeadURL: https://svn.id.ethz.ch/nagios_plugins/check_writable/check_writable $
#   $Date: 2007-11-13 14:47:43 +0100 (Tue, 13 Nov 2007) $

use 5.008;
use strict;
use warnings;
use Carp;

use version; our $VERSION = '2.0.1';

use Data::Random;
use English qw(-no_match_vars);
use File::Temp;
use Getopt::Long;
use Nagios::Plugin::Threshold;
use Nagios::Plugin;
use Pod::Usage qw(pod2usage);

# IMPORTANT: Nagios plugins could be executed using embedded perl in this case
#            the main routine would be executed as a subroutine and all the
#            declared subroutines would therefore be inner subroutines
#            This will cause all the global lexical variables not to stay shared
#            in the subroutines!
#
# All variables are therefore declared as package variables...
#
use vars qw(
  $plugin
  $result
  $verbose
  @dir
  @errors
);

##############################################################################
# main
#

################
# Initialization
$plugin = Nagios::Plugin->new( shortname => 'CHECK_WRITABLE' );

########################
# Command line arguments

Getopt::Long::Configure(qw{bundling});
$result = GetOptions(
    'dir|d=s'    => \@dir,
    'help|?'     => sub { pod2usage(); },
    'verbose|v+' => \$verbose,
    'version'    => sub { print "check_writeable version $VERSION\n"; exit 3; }
);

if ( !$result ) {
    pod2usage();
}

if ( !@dir ) {
    pod2usage( -message => 'at least one directory has to be specified' );
}

File::Temp->safe_level(File::Temp::HIGH);

foreach my $dir (@dir) {

    if ($verbose) {
        print "Checking $dir\n";
    }

    # 1) check FS permissions

    if ( !-d $dir ) {
        push @errors, "'$dir' is not a directory";
        next;
    }
    elsif ( !-w $dir || !-W $dir ) {
        push @errors, "'$dir' is not writable";
        next;
    }

    # 2) try to create a temporary file

    my $tmp = new File::Temp(
        TEMPLATE => 'writetest-XXXXX',
        DIR      => $dir,
        CLEANUP  => 1
    );

    if ($verbose) {
        print "Creating $tmp\n";
    }

    # 3) try to write to the file

    my $random_chars =
      join q{}, Data::Random::rand_chars( set => 'all', size => 32 ) ;

    print { $tmp } $random_chars;

    close $tmp or
        $plugin->nagios_exit(UNKNOWN, "Cannot close $tmp: $OS_ERROR");

    # 4) read the content back and check it

    my $IN;
    if ( ! open $IN, q{<}, $tmp->filename ) {
        push @errors, "Cannot read temporary file in $dir";
        next;
    }

    while (<$IN>) {
        if ( $_ ne $random_chars ) {
            push @errors, "R/W error on temporary file in $dir";
        }
        last;
    }

    close $IN or
        $plugin->nagios_exit(UNKNOWN, "Cannot close $tmp: $OS_ERROR");

}

if (@errors) {
    $plugin->nagios_exit( CRITICAL, join q{ }, @errors );
}

$plugin->nagios_exit( OK, join q{ }, @dir );

1;

__END__

=pod

=head1 NAME

C<check_writable> is a Nagios plugin that checks if one or more
directories are writable.

=head1 DESCRIPTION

C<check_writable> is a Nagios plugin that checks if one or more
directories are writable by:

 - checking that the supplied directory is indeed a directory
 - checking if the the filesystem permissions are OK
 - creating a temporary file
 - writing random data to the temporary file (and reading it back)

It return a critical status if one of the tests fails

=head1 VERSION

Version 2.0.1

=head1 SYNOPSIS

 check_writeable --dir=DIRECTORY

 Options:
  --help,-h,-?                  this help message
  --dir,-d                      directory to check
  --verbose,-v                  verbose output
  --version                     prints version and exits

=head1 REQUIRED ARGUMENTS

--dir

=head1 OPTIONS

  --help,-h,-?                  this help message
    
  --dir,-d                      directory to check
    
  --verbose,-v                  verbose output
    
  --version                     prints version and exits

=head1 EXAMPLE

check_dir --dir=/tmp

check_diskio checks if /tmp is writable

=head1 DIAGNOSTICS

You can specify multiple --verbose options to increase the program
verbosity.

=head1 EXIT STATUS

0 if OK, 1 in case of a warning, 2 in case of a critical status and 3
in case of an unkown problem

=head1 DEPENDENCIES

check_writable depends on

=over 4

=item * Carp

=item * Data::Random

=item * File::Temp

=item * English

=item * Getopt::Long

=item * Nagios::Plugin

=item * Nagios::Plugin::Threshold

=item * POSIX

=item * Pod::Usage

=item * version

=back

=head1 CONFIGURATION

=head1 INCOMPATIBILITIES

None reported.

=head1 SEE ALSO

Nagios documentation

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to matteo.corti@id.ethz.ch,
or through the web interface at
https://svn.id.ethz.ch/trac/bd_webhosting/newticket

=head1 AUTHOR

Matteo Corti <matteo.corti@id.ethz.ch>

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2007, ETH Zurich.

This module is free software; you can redistribute it and/or modify it
under the terms of GNU general public license (gpl) version 3.
See the LICENSE file for details.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.

=head1 ACKNOWLEDGMENTS
