#!/usr/bin/perl
#
# Author:  Petter Reinholdtsen
# Date:    2006-05-15
# License: GPL
#
# Nagios module to report the RAID status from the gdth linux kernel
# module.
#
# Based on documentation found in
# <URL:http://nagiosplug.sourceforge.net/developer-guidelines.html>

use strict;
use warnings;

use Getopt::Long;

my $verbose;
my $help;
my $RC = 0;

sub usage {
    print "Usage: $0 [-h|--help]\n";
    print "  Reports the gdth status as found in /proc/scsi/gdth/*\n"
}

my $result = GetOptions ("help" => \$help, "verbose+" => \$verbose);

if ($help) {
    usage();
    exit 0;
}

if ( ! -d "/proc/scsi/gdth" ) {
    print "No gdth status file found, /proc/scsi/gdth/ missing";
    exit 3;
}

chdir "/proc/scsi/gdth";

my $RESULT = "";

# Looking for this text block:
# Logical Drives:
#  Number:        0               Status:         ok
#  Capacity [MB]: 17333           Type:           RAID-1
#  Slave Number:  15              Status:         ok
#  Missing Drv.:  0               Invalid Drv.:   0
#  To Array Drv.: --
for my $controller (<*>) {
    open(INFO, "< $controller") || die "Unable to read from $controller";
    # Have to use 'cat', as redirect into egrep didn't work with the
    # file in /proc/.
    while (<INFO>) {
        chomp;
        last if (/Array Drives:/); # Stop after the Logical Drive block
        if (m/^\s+Number:\s+(\d+)\s+Status:\s+(\S+)$/) {
            my ($num, $status) = ($1,$2);
            if ("ok" ne $status) {
                $RC = 2;
                $RESULT="$RESULT ID $controller volume $num($status)";
#               print "Gdth RAID controller $controller volume $num ($status) NOT OK\n"
            } else {
#               print "Gdth RAID controller $controller volume $num OK\n"
            }
        }
    }
    close(INFO);
}
if ( 0 == "$RC" ) {
    print "gdth RAID OK\n";
} else {
    print "gdth RAID $RESULT NOT OK\n";
}
exit $RC;
