#!/bin/bash
# Hendrik Cornelißen <hc@hosteurope.de>

set_global_code() {
	code="${1}";
	[ x"${code}" = x"" ] && return;
	
	[ x"${global_code}" = x"CRITICAL" ] && return;
	[ x"${global_code}" = x"OK" ] && global_code="${code}" && return;

	[ x"${global_code}" = x"WARNING" ] && [ x"${code}" != x"OK" ] && global_code="${code}" && return;
}

logdir="/var/cache/he-nagios-plugins-local/";

hba='*';
logical_drive='*';
[ x"${1}" != x"" ] && hba="${1}";
[ x"${2}" != x"" ] && logical_drive="${2}";

wanted_hotspares=0;
[ x"${3}" != x"" ] && wanted_hotspares="${3}";

logdata='';
global_code='OK';

# begin import from cron-hwreport.sh #
function is_xen(){
        if test -d /sys/bus/xen/devices ; then
                true;
        else
                false;
        fi
}

function is_xen_domU(){
        if is_xen; then
                if test -L /sys/bus/xen/devices/vif-0 ; then
                        true;
                else
                        false;
                fi
        else
                false;
        fi
}

function is_xen_dom0(){
        if is_xen; then
                if is_xen_domU ; then
                        false;
                else
                        true;
                fi
        else
                false;
        fi
}
# end import from cron-hwreport.sh #

# virtuelle Gastmaschinen nicht weiter belästigen #
is_xen_domU && printf 'OK virtual machine.\n' && exit 0;

[ ! -d /sys ] && printf 'CRITICAL SYSFS is not available.\n' 1>&2 && exit 2;
[ ! -d /sys/class/scsi_host ] && printf 'WARNING no /sys/class/scsi_host found.' && exit 1;

cd "${logdir}";
logfiles="`find . -type f -name \"hba${hba}-logical_drive${logical_drive}\" 2>/dev/null || printf 'failure'`";

# old hardware may or may not have something resembling a raid controller #
# feverishly try to cope with that by assuming the most pleasant result #
[ x"${logfiles}" = x"" ] \
	&& logdata="logfile not found for HBA ${hba}, LD ${logical_drive}" \
	&& code="CRITICAL" \
	&& [ x"${hba}" = x"*" ] && [ x"${logical_drive}" = x"*" ] \
	&& code="OK" \
	&& set_global_code "${code}" \
	&& logdata="no logfiles found";

[ x"${logfiles}" = x"failure" ] \
	&& set_global_code "CRITICAL" \
	&& logfiles="" \
	&& logdata="error while finding logfile(s)";

logfiles="`printf '%s\n' \"${logfiles}\" | sort`";

for logfile in ${logfiles};
do
	. "${logfile}";
	logdata="${logdata}HBA ${hba}, LD ${logical_drive}, ";

	show_hotspares="${hotspares}";
	show_absolute_charge="${absolute_charge}";
	show_relative_charge="${relative_charge}";
	show_raid_state="${raid_state}";
	show_write_policy="${write_policy}";
	code=OK;

	[ x"${raid_state}" != x"OPTIMAL" ] && show_raid_state="+++${raid_state}+++" && code=WARNING;

	[ x"${write_policy}" = x"WRITETHROUGH" ] && show_write_policy="+++${write_policy}+++" && code=WARNING;
	[ "${hotspares}" -lt "${wanted_hotspares}" ] && show_hotspares="+++${hotspares}+++" && code=CRITICAL;

	if [ x"${absolute_charge}" != x"-1" ] && [ "${absolute_charge}" -lt 30 ];
	then
		show_absolute_charge="+++${absolute_charge}+++";
		code=WARNING;
		[ "${absolute_charge}" -lt 20 ] && code=CRITICAL;
	fi;

	if [ x"${relative_charge}" != x"-1" ] && [ "${relative_charge}" -lt 30 ];
	then
		show_relative_charge="+++${relative_charge}+++";
		code=WARNING;
		[ "${relative_charge}" -lt 20 ] && code=CRITICAL;
	fi;

	stale="`find \"${logfile}\" -mmin +70`";
	[ x"${stale}" != x"" ] \
		&& reason='stale logfile' \
		&& code=CRITICAL;

	if [ x"${reason}" != x"" ];
	then
		logdata="${logdata}reason: ${reason}; ";
	else
		logdata="${logdata}state ${show_raid_state}, ${percent} percent, ${show_write_policy}, RAID ${raid_level}, ${hotspares}/${wanted_hotspares} hotspares, ${show_relative_charge}/${show_absolute_charge}% battery;|battery_relative=${relative_charge}%;30;30;0;100 battery_absolute=${absolute_charge}%;30;30;0;100"
	fi;

	set_global_code "${code}";
done;

printf '%s %s\n' "${global_code}" "${logdata}" | sed -e 's/; $/./';

[ x"${global_code}" = x"OK" ] && exit 0;
[ x"${global_code}" = x"WARNING" ] && exit 1;
[ x"${global_code}" = x"UNKNOWN" ] && exit 3;
exit 2;
