Nagios Read Only Filesystem Detection

From Federal Burro of Information
Jump to navigationJump to search

aka rofs

check_rofs.pl

#!/usr/bin/perl -w

############################## check_rofs.pl ##############
# Version : 0.1
# Date : Dec 24th 2012
# Author  : David Thornton
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
###########################################################

# This script has been knee capped slightly while it's in early dev so that it will not fail if the file is still there from last timew, and it will not delete the file when it's done.
# it just tries to open the file read write. - dthornton

# ideally the process would be:
# if the file is there: error because the last run failed.
# create file or fail
# write to file or fail
# delete file or fail
# end

use strict;
use Getopt::Long;
# use Data::Dumper;
use Digest::MD5;

(my $Version) = '$Revision: 1.4 $' =~ /([\d\.]+)/;

my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my @keys = qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksize block);

my $o_help=undef;          # wan't some help ?
my $o_version=undef;       # print version
my $o_fs=undef;            # File with list of files
my $VERBOSE=0;             # Debug?
my $o_debug;
my $o_paramok=undef;
my $filename = "check_rofs.tmp";

sub print_version {
        print "$0 version : $Version\n";
}

sub print_usage {
    print "Usage: check_rofs.pl [-v] [-h] [-d] -f <filesystem>\n";
}

sub print_help {
  print "\nNagios Plugin to check a that a file system is Read/Write.\n";
  print_usage();
  print <<EOT;
-v, --version
  Print version of this plugin.
-h, --help
  Print this help message.
-d --debug
  Print more info about what is going on.
-f --filesystem
  The root of the filesystem that you want to check.
EOT
}

sub check_options {
  $VERBOSE && print "Checking options\n";
  Getopt::Long::Configure("bundling");
  GetOptions(
    'h'     => \$o_help,            'help'          => \$o_help,
    'v'     => \$o_version,         'version'       => \$o_version,
    'f:s'   => \$o_fs,              'filesystem'    => \$o_fs,
    'd'     => \$VERBOSE,           'debug'         => \$VERBOSE

  );

  if (defined ($o_help))         { print_help(); exit $ERRORS{"UNKNOWN"}};
  if (defined ($o_version))      { print_version(); exit $ERRORS{"UNKNOWN"}};
  unless (defined ($o_fs)) { print "Filesystem not defined\n" ; exit $ERRORS{"UNKNOWN"} };
  return 1;
}

if ( ! check_options() ) {
        print_usage();
        exit $ERRORS{"UNKNOWN"};
}

$VERBOSE && print "DEBUG is ON\n";

#if (-e $o_fs."/".$filename) {
#    print "CRITICAL: Test file ".$o_fs."/".$filename." already exists!\n";
#    exit $ERRORS{"CRITICAL"};
#}

my $fh ;

if ( ! open $fh, ">", $o_fs."/".$filename ) {
    print "Failed to create file ".$o_fs."/".$filename." Err: $!\n";
    exit $ERRORS{"CRITICAL"};
}

#if ( ! unlink $o_fs."/".$filename ) {
#    print "Failed to remove file ".$o_fs."/".$filename."\n";
#    exit $ERRORS{"CRITICAL"};
#}

print "OK created/destroyed file on $o_fs.\n";
exit $ERRORS{"OK"};