Nagios plugin to check readonly filesystems

I Found this script a while ago (can’t remember the source, sorry).This script checks if you have any readonly filesystems which should’t be readonly.Place this in your script folder on your Linux server which SNMP can read.check_rofs.sh

#!/bin/sh
#
# Nagios plugin to check if mounted devices are rw
#
STATE_OK=0
STATE_CRITICAL=2

for i in `grep ' ro,' /proc/mounts | awk '{ print $2 }'`
do
        OUTPUT=$(grep ' $i ' /etc/fstab | awk '{ print $4 }' | grep 'ro' 2>/dev/null)
        if [ $i == 0 ]; then
                MESSAGE="$i $MESSAGE";
        fi
done

if [ "$MESSAGE" ]
then
        echo "CRITICAL: Read-only filesystem: $MESSAGE"
        exit $STATE_CRITICAL
fi

echo "OK: No Read-Only filesystems"
exit $STATE_OK

Put this line in your /etc/snmp/snmpd.conf:

extend .1.3.6.1.4.1.2021.81 check_rofs /bin/sh /etc/snmp/check_rofs.sh

On your Nagios server, place this in your Nagios libexec or plugin folder (be sure you copy the text in full because of the line breaks):check_rofs.pl

#!/usr/bin/perl -w
# nagios: -epn
use strict;
require 5.6.0;
use lib qw( /opt/nagios/libexec );
use utils qw(%ERRORS $TIMEOUT &print_revision &support &usage);
use SNMP;
use Getopt::Long;
use vars qw( $exit $message $opt_version $opt_timeout $opt_help $opt_command $opt_host $opt_community $opt_verbose $opt_port $opt_proto $opt_pass $opt_user $snmp_session $PROGNAME $TIMEOUT );

$PROGNAME      = "check_rofs.pl";
$opt_verbose   = undef;
$opt_host      = undef;
$opt_community = 'public';
$opt_command   = undef;
$opt_port      = 161;
$opt_proto     = '2c';
$message       = undef;
$exit          = 'OK';
$TIMEOUT       = 5;

sub process_options {
    my( $opt_crit, $opt_warn ) = ();
    Getopt::Long::Configure( 'bundling' );
    GetOptions(
        'V'     => \$opt_version,       'version'     => \$opt_version,
        'v'     => \$opt_verbose,       'verbose'     => \$opt_verbose,
        'h'     => \$opt_help,          'help'        => \$opt_help,
        'H:s'   => \$opt_host,          'hostname:s'  => \$opt_host,
        'p:i'   => \$opt_port,          'port:i'      => \$opt_port,
        'P:s'   => \$opt_proto,         'protocol:s'  => \$opt_proto,
        'C:s'   => \$opt_community,     'community:s' => \$opt_community,
        't:i'   => \$TIMEOUT,           'timeout:i'   => \$TIMEOUT,
        'A:s'   => \$opt_pass,          'authpass:s'  => \$opt_pass,
        'U:s'   => \$opt_user,          'secname:s'   => \$opt_user,
    );
    if ( defined($opt_version) ) { local_print_revision(); }
    if ( defined($opt_verbose) ) { $SNMP::debugging = 1; }
    if ( !defined($opt_host) || defined($opt_help)) {
        print_help();
        exit $ERRORS{UNKNOWN};
    }
}



sub local_print_revision {
        print_revision( $PROGNAME, '$Revision: 1.0 $ ' )
}

sub print_usage {
    print "Usage: $PROGNAME -H  [-C ] [-P ] -U  -A  [-t ]\n";
}

sub print_help {
    local_print_revision();
    print "SNMP drbd status check plugin for Nagios\n\n",
    print_usage();
    print <<EOT;
-v, --verbose
   print extra debugging information
-h, --help
   print this help message
-H, --hostname=HOST
   name or IP address of host to check
-C, --community=COMMUNITY NAME
   community name for the host's SNMP agent
-P, --protocol=[2c|3]
   SNMP protocol version
-U, --secname=USERNAME
   SNMPv3 username
-A, --authpassword=PASSWORD
   SNMPv3 authentication password
EOT
}

sub verbose (@) {
    return if ( !defined($opt_verbose) );
    print @_;
}

sub check_for_errors {
    if ( $snmp_session->{ErrorNum} ) {
        print "UNKNOWN - error retrieving SNMP data: $snmp_session->{ErrorStr}\n";
        exit $ERRORS{UNKNOWN};
    }
}

# =========================================================================== #
# =====> MAIN
# =========================================================================== #
process_options();

# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
     print ("UNKNOWN: No response from remote host\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);

$snmp_session = new SNMP::Session(
    DestHost => $opt_host,
    Community => $opt_community,
    RemotePort => $opt_port,
    Version   => $opt_proto,
    SecName => $opt_user,
    SecLevel => 'authNoPriv',
    AuthProto => 'MD5',
    AuthPass => $opt_pass
);

# retrieve the data from the remote host
my ($rofs) = $snmp_session->bulkwalk( 0, 1, [['.1.3.6.1.4.1.2021.81.4']] );
check_for_errors();

alarm( 0 ); # all done with the network connection
my $result;
foreach my $answer ( @$rofs ) {
  $result = $answer->val;
  print $result, "\n";
  if($result =~ m/^WARNING/) {
    $exit= 'WARNING';
    }
  if($result =~ m/^OK/) {
    $exit= 'OK';
  }
  if ($result =~ m/^CRITICAL/) {
    $exit= 'CRITICAL';
  }
}
exit $ERRORS{$exit};

Create a command for Nagios:SNMP v2: (-C = communityname)check_readonly_filesystems $USER1$/check_rofs.pl -H $HOSTADDRESS$ -C $ARG1$ SNMP v3: (-U = username, -A = password)check_readonly_filesystems $USER1$/check_rofs.pl -H $HOSTADDRESS$ -P3 -U $ARG1$ -A $ARG2$