#!/bin/bash
#
# Uses SNMP to check for bacula processes on remote hosts
#
#echo "Start mit $@" >> /var/log/naglog
#
# (C) Arno Lehmann IT-Service Lehmann
# Contact: al@its-lehmann.de
#
# License: GNU GPL version 2 or above.
# I recommend actually reading the GPL...
# In short: You may use, modify, distribute this script in any way you like
# but it must remain free. You must give access to it's source (easy enough
# with a shell script).
# You must not change the license to anything 'less' free.
# Please keep the notice of the original author.
# WARRANTIES: NONE, NEITHER EXPRESS OR IMPLIED.

PROGNAME=`/bin/basename $0`
HOST=""
COMM=""
VER=""
FD=0
SD=0
DIR=0

print_usage() {
    echo "Usage: $PROGNAME -H host -C community -P snmp version"
    echo "       [-FD] [-SD] [-DIR]"
    echo
    echo "Check remote host for running bacula processes. Which ones"
    echo "are checked for is given on the command line."
    echo "Missing services are reported as CRITICAL."
    echo
    exit 23
}

while test -n "$1"; do
    case "$1" in
	-H)
	HOST=$2
	shift
	;;
	-P)
	VER=$2
	shift
	;;
	-C)
	COMM=$2
	shift
	;;
	-FD)
	FD=1
	;;
	-SD)
	SD=1
	;;
	-DIR)
	DIR=1
	;;
	*)
	print_usage
	;;
    esac
    shift
done

# echo "FD: $FD SD: $SD DIR: $DIR";

RES=`snmpwalk -v $VER -c $COMM $HOST HOST-RESOURCES-MIB::hrSWRunName 2>/dev/null | grep 'STRING: "bacula' | sed -e 's/^.*\.\([0-9]*\) = STRING: "\(.*\)"/\2/'| uniq`

# RES=`snmpwalk -v $VER -c $COMM $HOST HOST-RESOURCES-MIB::hrSWRunName 2>/dev/null | grep 'STRING: "bacula' | sed -e 's/^.*\.\([0-9]*\) = STRING: "\(.*\)"/\2/'`

#echo $RES


FDR=0
SDR=0
DIRR=0

for R in $RES ; do
    case $R in
	bacula-fd*)
	FDR=1
	;;
	bacula-sd*)
	SDR=1
	;;
	bacula-dir*)
	DIRR=1
	;;
    esac
done

RC=0
RT=""
if [ $DIR -gt 0 ] ; then
    if [ $DIRR -gt 0 ] ; then
	RT="$RT dir: OK"
    else
	RT="$RT dir: FAIL"
	RC=2
    fi
fi
if [ $SD -gt 0 ] ; then
    if [ $SDR -gt 0 ] ; then
	RT="$RT sd: OK"
    else
	RT="$RT sd: FAIL"
	RC=2
    fi
fi
if [ $FD -gt 0 ] ; then
    if [ $FDR -gt 0 ] ; then
	RT="$RT fd: OK"
    else
	RT="$RT fd: FAIL"
	RC=2
    fi
fi

if [ $RC -gt 0 ] ; then
    RT="CRITICAL: $RT"
else
    RT="OK: $RT"
fi

echo $RT
exit $RC
