#!/usr/bin/perl -w 
#########################################################################
#
# File: check_fs_readable.pl
#
# Description: 
#        This nagios plugin is a very simple minded check to determine 
#        if a mounted file system is writable.  This was written in reaction
#        to chronic issues with my VPS provider having SAN issues that 
#        manifested themselves in the root file system suddenly becoming 
#        read only.  Since the VPSes in question were all built with 
#        a singular file system having root go read only caused all the 
#        applications on the box to come to a screaching halt.
#
#        This plugin is meant to be run locally on a system or from nrpe.
#
# Logic:   Attempt to cwd to /tmp and attempt to open a file for writing.
#          If either fail, sound the alarm. 
#
# Version:  0.5
#
# Author: Peter L. Berghold <salty.cowdawg@gmail.com>
#
# License: GPL
#
# CAVEATS: optionally you can provide an alternative directory to check. Make
#          certain that whatever userid nagios is running as can write to that
#          directory so you do not get false positives.
#
########################################################################

use strict;
use Getopt::Long;
use constant {
	      OK => 0,
	      WARNING => 1,
	      CRITICAL => 2,
	      UNKNOWN => 3
	      };
use constant RETCODES => qw ( OK WARNING CRITICAL UNKNOWN ) ;

my $testdir = "/tmp";

my $res = GetOptions("dir=s" => \$testdir );

chdir $testdir or crap_out(CRITICAL,"Could not cwd to $testdir");

my $fname = "foo.$$.txt";

open FOUT,"> $fname" or crap_out(CRITICAL,"Could to write to file system");

close FOUT;

printf "OK: File System Normal\n";
exit(OK);

sub crap_out{
    my ($rc,$msg)=@_;
    printf "%s: %s\n",(RETCODES)[$rc],$msg;
    exit($rc);
}
