#!/usr/bin/perl -w
our $VERSION = '2.4.1 (2022-08-12)';
#
# check_afs_bos -- Monitor AFS bos output for problems in Nagios.
#
# Given an AFS server (file or VLDB), runs bos status on each one.  Checks to
# see if there is a communication failure, and also checks to see if anything
# in the output looks unusual or wrong.  If either of these conditions are
# true, print that information to STDOUT.  Suitable for being run inside
# Nagios.
#
# Written by Russ Allbery <rra@stanford.edu>
# Based on an earlier script by Neil Crellin <neilc@stanford.edu>
# Copyright 2003, 2004, 2010, 2013
#     The Board of Trustees of the Leland Stanford Junior University
#
# This program is free software; you may redistribute it and/or modify it
# under the same terms as Perl itself.

##############################################################################
# Modules and declarations
##############################################################################

require 5.006;

use strict;

use Getopt::Long qw(GetOptions);

##############################################################################
# Site configuration
##############################################################################

# The full path to bos.  Make sure that this is on local disk so that
# monitoring doesn't have an AFS dependency.
our ($BOS) = grep { -x $_ } qw(/usr/bin/bos /usr/local/bin/bos);
$BOS ||= 'bos';

# The default timeout in seconds (implemented by alarm) for rxdebug.
our $TIMEOUT = 10;

# The list of regular expressions matching expected output.  You may need to
# customize this for what you're running at your site.  Any output from bos
# that doesn't match one of these regular expressions or the warning regular
# expressions below will throw a critical error.
our @OKAY = (
    qr/^\s*$/,
    qr/^bos: running unauthenticated$/,
    qr/^Instance\ \S+,\ \(type\ is\ \S+\)(\ has\ core\ file,)?
         \ currently\ running\ normally\.$/x,
    qr/^\s*Auxiliary status is: file server running\.$/,
    qr/^\s*Auxiliary status is: run next at .+$/,
    qr/^\s*Auxiliary status is: running now\.$/,
    qr/^\s*Process last started at /,
    qr/^\s*Last exit at /,
    qr/^\s*Last error exit at /,
    qr/^\s*Command \d+ is /
);

# The list of regular expressions that match output that should produce a
# warning.  You may need to customize this for what you expect at your site.
our @WARNINGS = (
    qr/^\s*Bosserver reports inappropriate access on server directories/,
    qr/^Instance \S*fs, \(type is \S*fs\) disabled, currently shutdown\.$/,
    qr/^\s*Auxiliary status is: file server shut down\.$/
);

##############################################################################
# Implementation
##############################################################################

# Report a syntax error and exit.  We do this via stdout in order to satisfy
# the Nagios plugin output requirements, but also report a more conventional
# error via stderr in case people are calling this outside of Nagios.
sub syntax {
    print "BOS UNKNOWN - ", join ('', @_), "\n";
    warn "$0: ", join ('', @_), "\n";
    exit 3;
}

# Parse command line options.
my ($help, $host, $version);
Getopt::Long::config ('bundling', 'no_ignore_case');
GetOptions ('H|hostname=s' => \$host,
            'h|help'       => \$help,
            't|timeout=i'  => \$TIMEOUT,
            'V|version'    => \$version)
    or syntax ("invalid option");
if ($help) {
    print "Feeding myself to perldoc, please wait....\n";
    exec ('perldoc', '-t', $0) or die "Cannot fork: $!\n";
} elsif ($version) {
    my $version = $VERSION;
    print "check_afs_bos $version\n";
    exit 0;
}
syntax ("extra arguments on command line") if @ARGV;
syntax ("host to check not specified") unless (defined $host);

# Set up the alarm.
$SIG{ALRM} = sub {
    print "BOS CRITICAL - network timeout after $TIMEOUT seconds\n";
    exit 2;
};
alarm ($TIMEOUT);

# Collect the bos output into a variable.
unless (open (BOS, "$BOS status $host -noauth -long 2>&1 |")) {
    print "BOS UNKNOWN - cannot run bos\n";
    exit 3;
}
my @bos = <BOS>;
close BOS;

# Make sure that bos was successful.  Note that it generally does return
# success even if it can't contact the bos server.
if ($? != 0) {
    print "BOS CRITICAL - bos status failed\n";
    exit 2;
}

# Scan the output.  If we see anything that we don't expect, immediately
# report it as a fatal error.
my $count = 0;
my $prev_line = '';
for my $line (@bos) {
    my $okay = 0;
    my $warn = 0;
    for my $regex (@OKAY) {
        if ($line =~ /$regex/) {
            $okay = 1;
            last;
        }
    }
    for my $regex (@WARNINGS) {
        if ($line =~ /$regex/) {
            $warn = 1;
            last;
        }
    }
    unless ($okay || $warn) {
        $line =~ s/^\s+//;
        $line =~ s/\s+$//;
        if ($prev_line =~ /^Instance salvage,/ && $line =~ /running now/) {
            print "BOS WARNING - salvage is running\n";
            exit 1;
        } else {
            print "BOS CRITICAL - $line\n";
            exit 2;
        }
    }
    if ($warn) {
        $line =~ s/^\s+//;
        $line =~ s/\s+$//;
        print "BOS WARNING - $line\n";
        exit 1;
    }
    $count++ if ($line =~ /currently running normally\.$/);
    $prev_line = $line;
}
if ($count == 1) {
    print "BOS OK - one process running normally\n";
} else {
    print "BOS OK - $count processes running normally\n";
}
exit 0;

##############################################################################
# Documentation
##############################################################################

=for stopwords
AFS Crellin Nagios afs-monitor bos bosserver -hV salvager util

=head1 NAME

check_afs_bos - Monitor AFS bos output for problems in Nagios

=head1 SYNOPSIS

B<check_afs_bos> [B<-hV>] [B<-t> I<timeout>] B<-H> I<host>

=head1 DESCRIPTION

B<check_afs_bos> is a Nagios plugin for querying the AFS bosserver for
process status and reporting an alert if there are any unexpected lines in
the bos output.  The acceptable lines of output from B<bos> are configured
at the top of this script; they should be generally suitable for most
sites, but may require some customization.

B<check_afs_bos> will always print out a single line of output.  If there
is a line that isn't matched by any regexes identifying acceptable lines,
it will output the first non-matching line prefixed by C<BOS CRITICAL>.
If the salvager is running (such as when started by C<bos salvage>) or
other warnings are found, it will print that warning information prefixed
by C<BOS WARNING>.  Otherwise, it will output C<BOS OK>.  Note that this
monitoring may not catch such things as a service being constantly
restarted if it happens to be up and running normally each time the probe
runs; it doesn't pay any attention to the last start time, the last error
exit status, the presence of core files, and the like.  It mostly just
looks for the "running normally" part of the B<bos> output and makes sure
the auxiliary status is also "running normally" for a file server
process.

=head1 OPTIONS

=over 4

=item B<-H> I<host>, B<--hostname>=I<host>

The AFS server whose B<bos> status B<check_afs_bos> should check.  This
option is required.

=item B<-h>, B<--help>

Print out this documentation (which is done simply by feeding the script
to C<perldoc -t>).

=item B<-t> I<timeout>, B<--timeout>=I<timeout>

Change the timeout for the B<bos> command.  The default timeout is 10
seconds.

=item B<-V>, B<--version>

Print out the version of B<check_afs_bos> and quit.

=back

=head1 EXIT STATUS

B<check_afs_bos> follows the standard Nagios exit status requirements.
This means that it will exit with status 0 if there are no problems, with
status 1 if the salvager is running, or with status 2 if there is a
problem detected.  For other errors, such as invalid syntax,
B<check_afs_bos> will exit with status 3.

=head1 BUGS

The standard B<-v> verbose Nagios plugin option is not supported.  It
should display the complete bos status output.

The usage message for invalid options and for the B<-h> option doesn't
conform to Nagios standards.

=head1 CAVEATS

This script does not use the Nagios util library or any of the defaults
that it provides, which makes it somewhat deficient as a Nagios plugin.
This is intentional, though, since this script can be used with other
monitoring systems as well.  It's not clear what a good solution to this
would be.

=head1 SEE ALSO

This script is part of the afs-monitor package, which includes various AFS
monitoring plugins for Nagios.  It is available from the AFS monitoring
tools page at L<http://www.eyrie.org/~eagle/software/afs-monitor/>.

=head1 AUTHORS

The original idea behind this script was from Neil Crellin.  Russ Allbery
<rra@stanford.edu> updated it to work with Nagios and stripped out some
rather neat but now unnecessary code to look for any changes in the bos
output, instead just scanning it for acceptable lines.

=head1 COPYRIGHT AND LICENSE

Copyright 2003, 2004, 2010, 2013 The Board of Trustees of the Leland
Stanford Junior University

This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.

=cut
