#!/bin/bash

#
# check_metadisk.sh
#
# Program: Metadisk check plugin for Nagios
# License : GPL
# Copyright (c) 2003 Walbert Oberngruber (walbert.oberngruber@liwest.at)
#
# check_metadisk.sh,v 1.0 2003/03/21 15:28
#
# Description :
#
#  This plugin check software raid on Sun Solaris
#  which are created with Sun's Solstice DiskSuite -> metadisk
#
# Usage :
#
#  check_medadisk.sh [-1] 
#
# Tested on Sun Solaris 8
#
#
####### IMPORTANT #########################################
#
# Change this, if you like
# It depends on your nagios implementation
#
###########################################################
WORK_DIR=/usr/local/nagios/metainfo
INITFILE=$WORK_DIR/metainfo.init
RUN_FILE=$WORK_DIR/metainfo.txt
COMP_RES=$WORK_DIR/diff.txt


help_usage() {
    echo "Usage:"
    echo " $0 -r | --run"
    echo " $0 (-i | --init) for initial use, necessary for file creation"
    echo " $0 (-v | --version)"
    echo " $0 (-h | --help)"
    echo " $0 (-l | --long) for long help install version"
}

help_version() {
    echo "check_metadisk.sh (nagios-plugins) 1.0"
    echo "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute"
    echo "copies of the plugins under the terms of the GNU General Public License."
    echo "Copyright (c) 2003 Walbert Oberngruber - walbert.oberngruber@liwest.at"
}

help_long() {
    echo "This nagios plugin will check changes of 'metastat' information used for"
    echo "control software raid conditions, applied with Sun's Solstice Disk Suite."
    echo "It will compare metastat output with previously saved output, stored at"
    echo "initial run of this plugin."
    echo ""
    echo "Installation instruction:"
    echo "1. Modify variables in this script: \$WORK_DIR, \$INITFILE, \$RUN_FILE, \$COMP_RES."
    echo "2. Run $0 -i for initialize metastat information."
    echo "3. Modify owner and file rights of generated ouput to allow nagios access."
    echo "4. Integrate this script to your nagios environment and enjoy."
}

help_info() {
    help_version
    echo ""
    echo "This plugin will check metadisk condition and notify changes in metastat information."
    echo ""
    help_usage
    echo ""
    echo "Required Argument:"
    echo " -r, --run"
    echo ""
}

verify_dep() {
    needed="bash ls mkdir test awk diff metastat"
    for i in `echo $needed`
    do
        type $i > /dev/null 2>&1 /dev/null
        if [ $? -eq 1 ]
        then
            echo "I am missing an important component : $i"
            echo "Cannot continue, sorry, try to find the missing one..."
            exit 3
        fi
    done
}

verify_dep

case "$1" in
    -h|--help)
        help_info
        exit 3
        ;;

    -v|--version)
        help_version
        exit 3
        ;;

    -l|--long)
        help_long
        exit 3
        ;;

    -i|--init)
        if [ ! -d $WORK_DIR ]; then
            echo "Creating directory $WORK_DIR "
            mkdir $WORK_DIR
        fi
        echo "Creating init file $INITFILE"
        metastat > $INITFILE
        echo "Creating init file $RUN_FILE"
        metastat > $RUN_FILE
        echo "Creating init file $COMP_RES"
        metastat > $COMP_RES
        echo "IMPORTANT: Set required file rights manually!" 
        exit 3
        ;;

    -r|--run)
        if [ -f $INITFILE ]; then
            metastat > $RUN_FILE
            diff $RUN_FILE $INITFILE > $COMP_RES
            if [ -f $COMP_RES ]; then
                SIZE=`ls -ld "$COMP_RES" | awk '{print $5}'`
                if [ $SIZE -eq 0 ]; then
                    crit=0
                else
                    crit=1
                fi
            fi
        else
            echo "Initfile $INITFILE is missing !"
            crit=2
        fi
        ;;

    *)
        help_info
        exit 3
        ;;

esac


# Finally Inform Nagios of what we found...
if [ $crit -eq 2 ]; then 
    echo "UNKNOWN - check initial use of this script !"
    exit 3
elif [ $crit -eq 1 ]; then
    echo "CRITICAL - run 'metastat' on this machine, to see the problem !"
    exit 2
elif [ $crit -eq 0 ]; then
    echo "OK - software raid seems to be ok !"
    exit 0
fi

# Hey what are we doing here ???
exit 3

