#!/usr/local/bin/perl # # check filesystem mounts # # Make certain all filesystems are mounted read-write or read-only, as # appropriate. # # By Doke Scott, 2014.2.24 # # $Header: /opt/home/doke/work/nagios/RCS/check_mounts,v 1.9 2015/03/30 15:21:04 doke Exp $ # use strict; #use warnings; #no warnings 'uninitialized'; use Getopt::Long; #use Data::Dumper; use vars qw( @ignore_fstypes @ignore_mounts $verbose $help @crits @warns @unknowns @oks @ignores $rc $sep ); @ignore_fstypes = ( 'autofs', 'binfmt_misc', 'ctfs', 'debugfs', 'devfs', 'devpts', 'devtmpfs', 'fd', 'fusectl', 'hsfs', 'iso9660', 'lofs', 'mntfs', 'objfs', 'proc', 'securityfs', 'sharefs', 'swap', 'sysfs', 'tmpfs', 'usbfs', ); ################# $verbose = 0; $help = 0; # print the usage of this script sub usage { print "Usage: $0 [-vh] [-e mntpoint] ... -I s Ignore a mountpoint -v increase Verbosity -h Help "; exit -1; } # check arguments Getopt::Long::Configure ("bundling"); GetOptions( 'I=s' => \@ignore_mounts, 'v+' => \$verbose, 'h' => \$help, ); &usage( 0 ) if ( $help ); check_mounts(); $rc = 0; $sep = ''; if ( $#crits >= 0 ) { $rc = 2; print "CRITICAL ", join( ", ", @crits ); $sep = '; '; } if ( $#warns >= 0 ) { $rc = 1 if ( $rc == 0 ); print $sep, "Warning ", join( ", ", @warns ); $sep = '; '; } if ( $#unknowns >= 0 ) { $rc = 3 if ( $rc == 0 ); print $sep, "Unknown ", join( ", ", @unknowns ); $sep = '; '; } if ( $rc == 0 || $verbose ) { print $sep, "Ok ", join( ", ", @oks ); $sep = '; '; } if ( $#ignores >= 0 ) { print $sep, "Ignoring ", join( ", ", @ignores ); } print "\n"; exit $rc; ########################### sub check_mounts { my( $dev, $mount, $type, $options, $freq, $pass, $option, %fstab_data, %mount_data, %all_mounts, $rdev, $mntatboot, $time ); $verbose && print "check_mounts()\n"; # get info on intended mounts from /etc/fstab or /etc/vfstab if ( -f "/etc/fstab" ) { # linux /etc/fstab if ( open( fH, "/etc/fstab" ) ) { while ( ) { next if ( m/^\s*$|^\s*#/ ); ( $dev, $mount, $type, $options, $freq, $pass ) = split; $verbose && print "/etc/fstab $mount $type $options\n"; $fstab_data{ $mount }{ 'dev' } = $dev; $fstab_data{ $mount }{ 'type' } = $type; foreach $option ( split( m/,/, $options ) ) { next if ( $option eq '-' || $option eq 'defaults' ); if ( $option =~ m/(\S+?)=(\S+)/i ) { $fstab_data{ $mount }{ 'options' }{ $1 } = $2; } else { $fstab_data{ $mount }{ 'options' }{ $option } = $option; } } $all_mounts{ $mount } = 1; } close fH; } } elsif ( -f "/etc/vfstab" ) { # solaris /etc/vfstab if ( open( fH, "/etc/vfstab" ) ) { while ( ) { next if ( m/^\s*$|^\s*#/ ); ( $dev, $rdev, $mount, $type, $pass, $mntatboot, $options ) = split; $verbose && print "/etc/vfstab $mount $type $options\n"; $fstab_data{ $mount }{ 'dev' } = $dev; $fstab_data{ $mount }{ 'rdev' } = $rdev; $fstab_data{ $mount }{ 'type' } = $type; $fstab_data{ $mount }{ 'mntatboot' } = $mntatboot; foreach $option ( split( m/,/, $options ) ) { next if ( $option eq '-' || $option eq 'defaults' ); if ( $option =~ m/(\S+?)=(\S+)/i ) { $fstab_data{ $mount }{ 'options' }{ $1 } = $2; } else { $fstab_data{ $mount }{ 'options' }{ $option } = $option; } } $all_mounts{ $mount } = 1; } close fH; } } # get info on current mounts from /proc/mounts or /etc/mnttab if ( -r "/proc/mounts" ) { # linux /proc/mounts if ( open( fH, "/proc/mounts" ) ) { while ( ) { next if ( m/^\s*$|^\s*#/ ); # shouldn't happen ( $dev, $mount, $type, $options, $freq, $pass ) = split; $verbose && print "/proc/mounts $mount $type $options\n"; $mount_data{ $mount }{ 'dev' } = $dev; $mount_data{ $mount }{ 'type' } = $type; foreach $option ( split( m/,/, $options ) ) { next if ( $option eq '-' || $option eq 'defaults' ); if ( $option =~ m/(\S+?)=(\S+)/i ) { $mount_data{ $mount }{ 'options' }{ $1 } = $2; } else { $mount_data{ $mount }{ 'options' }{ $option } = $option; } } $all_mounts{ $mount } = 1; } close fH; } } elsif ( -r "/etc/mnttab" ) { # solaris /etc/mnttab if ( open( fH, "/etc/mnttab" ) ) { while ( ) { next if ( m/^\s*$|^\s*#/ ); # shouldn't happen ( $dev, $mount, $type, $options, $time ) = split( m/\t/ ); # tabs $verbose && print "/etc/mnttab $mount $type $options\n"; $mount_data{ $mount }{ 'dev' } = $dev; $mount_data{ $mount }{ 'type' } = $type; foreach $option ( split( m/,/, $options ) ) { next if ( $option eq '-' || $option eq 'defaults' ); if ( $option =~ m/(\S+?)=(\S+)/i ) { $mount_data{ $mount }{ 'options' }{ $1 } = $2; } else { $mount_data{ $mount }{ 'options' }{ $option } = $option; } } $all_mounts{ $mount } = 1; } close fH; } } foreach $mount ( sort keys %all_mounts ) { $verbose && print "checking $mount\n"; if ( 0 < scalar grep { $mount eq $_ } @ignore_mounts ) { $verbose && print "ignoring mount $mount\n"; push @ignores, "$mount"; next; } $type = $mount_data{ $mount }{ 'type' }; $type ||= $fstab_data{ $mount }{ 'type' }; if ( 0 < scalar grep { $type eq $_ } @ignore_fstypes ) { $verbose && print "ignoring type $type for mount $mount\n"; next; } if ( $mount_data{ $mount }{ 'dev' } ) { # mounted # is it read only? if ( $mount_data{ $mount }{ 'options' }{ 'ro' } ) { # is it supposed to be? if ( $fstab_data{ $mount }{ 'options' }{ 'ro' } ) { # ok push @oks, "$mount is mounted ro"; } else { # oops push @warns, "$mount is read-only"; } } else { # it's read-write # is it supposed to be? if ( $fstab_data{ $mount }{ 'options' }{ 'ro' } ) { # oops push @warns, "$mount is mounted read-write, but is supposed to be read-only"; } else { # ok push @oks, "$mount is mounted rw"; } } } else { # not mounted # is it supposed to be? if ( $fstab_data{ $mount }{ 'mntatboot' } eq 'yes' || $fstab_data{ $mount }{ 'options' }{ 'auto' } ) { # oops push @warns, "$mount is not mounted"; } else { # ok push @oks, "$mount is not mounted"; } } } }