aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore18
-rw-r--r--LICENSE7
-rw-r--r--MANIFEST4
-rw-r--r--Makefile.PL38
-rw-r--r--libexec/check_mount152
5 files changed, 219 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4dbadf4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,18 @@
+Makefile
+Makefile.old
+Build
+Build.bat
+META.*
+MYMETA.*
+.build/
+_build/
+cover_db/
+blib/
+inc/
+.lwpcookies
+.last_cover_stats
+nytprof.out
+pod2htm*.tmp
+pm_to_blib
+check_mount-*
+check_mount-*.tar.gz
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..cb45c5d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2020 Tom Ryder
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..8a77fda
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,4 @@
+libexec/check_mount
+LICENSE
+MANIFEST
+Makefile.PL
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..9aa45d1
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,38 @@
+use 5.006;
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'check_mount',
+ AUTHOR => 'Tom Ryder <tom@sanctum.geek.nz>',
+ VERSION_FROM => 'libexec/check_mount',
+ LICENSE => 'MIT',
+ INSTALLSCRIPT => '/usr/local/nagios/libexec',
+ INSTALLSITESCRIPT => '/usr/local/nagios/libexec',
+ EXE_FILES => ['libexec/check_mount'],
+ MIN_PERL_VERSION => '5.010_001',
+ CONFIGURE_REQUIRES => {
+ 'ExtUtils::MakeMaker' => '0',
+ },
+ PREREQ_PM => {
+ 'English' => 0,
+ 'Exception::Class' => 0,
+ 'Monitoring::Plugin' => 0,
+ 'Path::Tiny' => 0,
+ 'Quota' => 0,
+ 'Try::Tiny' => 0,
+ },
+ META_MERGE => {
+ 'meta-spec' => { version => 2 },
+ resources => {
+ homepage => 'https://sanctum.geek.nz/cgit/check_mount.git/',
+ repository => {
+ type => 'git',
+ url => 'https://sanctum.geek.nz/code/check_mount.git/',
+ web => 'https://sanctum.geek.nz/cgit/check_mount.git/',
+ },
+ },
+ },
+ clean => { FILES => 'check_mount-*' },
+);
diff --git a/libexec/check_mount b/libexec/check_mount
new file mode 100644
index 0000000..31481c1
--- /dev/null
+++ b/libexec/check_mount
@@ -0,0 +1,152 @@
+#!perl
+#
+# This plugin takes a mountpoint and checks that a filesystem is mounted
+# thereupon, once and once only.
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# License: MIT
+#
+package main;
+
+# Force me to write this properly
+use strict;
+use warnings;
+use utf8;
+
+# Require at least this Perl version
+use 5.010_001;
+
+# Import required modules
+use English qw(-no_match_vars);
+use Exception::Class ( PluginException => { alias => 'throw' } );
+use Monitoring::Plugin qw(%ERRORS);
+use Path::Tiny;
+use Quota;
+use Try::Tiny;
+
+# Decree package version
+our $VERSION = '1.00';
+
+# Add description and license package variables
+our $DESCRIPTION = <<'EOF';
+This plugin takes a mountpoint and checks that a filesystem is mounted
+thereupon, once and once only.
+EOF
+our $LICENSE = <<'EOF';
+MIT License <https://opensource.org/licenses/MIT>
+EOF
+
+# Custom plugin options
+our @OPTS = (
+ {
+ spec => 'mountpoint|m=s',
+ help => 'Path to mountpoint',
+ label => 'PATH',
+ required => 1,
+ },
+);
+
+# Build Monitoring::Plugin object
+my $mp = Monitoring::Plugin->new(
+ usage => 'Usage: %s --mountpoint|-m PATH',
+ version => $VERSION,
+ blurb => $DESCRIPTION,
+ license => $LICENSE,
+);
+
+# Anything that dies in here raises ->plugin_die()
+try {
+
+ # Add and read custom options
+ for my $opt (@OPTS) {
+ $mp->add_arg( %{$opt} );
+ }
+ $mp->getopts();
+
+ # Start counting down to timeout
+ alarm $mp->opts->timeout();
+
+ # Get a cleaned-up and absolute path for the mountpoint
+ length $mp->opts->mountpoint
+ or throw 'Empty mountpoint path';
+ my $point = path( $mp->opts->mountpoint )->realpath();
+ $point->exists()
+ or throw "$point does not exist";
+ $point->is_dir()
+ or throw "$point is not a directory";
+
+ # Read the mount table row by row and collect any entries that correspond
+ # to our mountpoint
+ my @mounts = grep { $_->{path} eq $point } mounts();
+
+ # Make a string for the message that describes the device and type for each
+ # of the matching mounts, comma-separated
+ my $devs = join q(, ), map { "$_->{dev} ($_->{type})" } @mounts;
+
+ # One mount is ideal; exit OK, describing the mounted devices and the
+ # filesytem types
+ if ( @mounts == 1 ) {
+ $mp->add_message( $ERRORS{OK}, "Mounted on $point: $devs\n" );
+ }
+
+ # More than one mount is not ideal; exit WARNING, describing the mounted
+ # devices and the filesystem types
+ elsif ( @mounts > 1 ) {
+ $mp->add_message( $ERRORS{WARNING}, "Multi mounts on $point: $devs\n" );
+ }
+
+ # No matching mount at all is bad
+ else {
+ $mp->add_message( $ERRORS{CRITICAL}, "No mounts found on $point\n" );
+ }
+
+ # Exit with the selected code and message
+ $mp->plugin_exit( $mp->check_messages );
+}
+catch {
+ $mp->plugin_die($_);
+};
+
+# Return a listref of hashrefs describing every mount table entry
+sub mounts {
+
+ # Open mount table for reading, throw on error (return great than zero)
+ Quota::setmntent() == 0
+ or throw Quota::strerr();
+
+ # Iterate through each mount table entry to collect a list. The
+ # getmntent() function here returns an empty list when it's read the whole
+ # table, and undef on error in place of what would normally be the device,
+ # so this may seem a bit awkward on first reading.
+ #
+ my @mounts;
+ while ( my @mnt = Quota::getmntent() ) {
+
+ # Check the first element; if it's undefined, there was an error, and
+ # we should bail out
+ defined $mnt[0]
+ or throw Quota::strerr();
+
+ # Otherwise, this really is a mountpoint entry; extract the device,
+ # mountpoint, and filesystem type
+ my ( $dev, $path, $type ) = @mnt;
+ length $path or next;
+
+ # Build a hash structure including canonicalising and resolving the
+ # path, and add it as a reference to the mounts collection
+ my %mount = (
+ dev => $dev || 'unknown',
+ path => path($path)->realpath(),
+ type => $type || 'unknown',
+ );
+ push @mounts, \%mount;
+ }
+
+ # Close mount table
+ Quota::endmntent();
+
+ # Return collected mounts
+ return @mounts;
+}
+
+1;