#!/bin/bash

. ${TOOLKIT}-functions.sh

search_plugin()
{
	local path=
	for path in /usr/lib/nagios/plugins
	do
		[ -f $path/$1 ] && {
			echo "$path/$1"
			return 0
		}
	done
	return 1
}

# create check commands on the fly
#
instdir="/etc/nagios/nrpe.d"
donefile="done.tmp"
:>$donefile


# commands: defaults
#
echo "  building checks: defaults"
stat=0
sect="cli-nagios-command"

for entry in $(jtconf --listvars $sect)
do
	val=$(jtconf $sect.$entry)
	plugin=$(echo "$val" | cut -d' ' -f1)
	val=$(echo "$val" | sed -e "s/^$plugin\s\s*//")
	cmd="$entry"
	out="check_$cmd.cfg"

	plugin_path=$(search_plugin $plugin) || {
		echo "ERROR: [$sect.$entry] plugin '$plugin' not found"
		stat=1
		continue
	}

	printf "    command %-16s %s %s\n" "$cmd:" "$plugin" "$val"

	echo "# kusa autogenerated file from $sect.$entry = $val" >$out.tmp
	echo "# ::do_not_edit::" >>$out.tmp
	echo "command[$cmd]=$plugin_path $val" >>$out.tmp
	installfile $out.tmp $instdir/$out nagios:nagios 444
	echo "$out" >>$donefile
done
[ $stat != 0 ] && exit $stat





echo "  building checks: disk"

stat=0
sect="cli-nagios-checkdisk"

plugin="check_disk"
plugin_path=$(search_plugin check_disk) || {
	echo -e "\n  ERROR: plugin '$plugin' not found\n" >&2
	exit 1
}

for entry in $(jtconf --listvars $sect)
do
	val=$(jtconf $sect.$entry)

	warn=$(echo "$val" | cut -d' ' -f1)
	crit=$(echo "$val" | cut -d' ' -f2)
	dir=$(echo "$val" | cut -d' ' -f3)
	cmd="disk_$entry"
	out="check_$cmd.cfg"

	[ "X$warn" = "X" -o "X$crit" = "X" -o "X$dir" = "X" ] && {
		echo "wrong definition on $sect.$entry" >&2
		echo "  have:  $val" >&2
		echo "  need:  warning_val critical_val dir_path [opt parms...]" >&2
		exit 1
	}

	printf "    command %-16s %-4s %-4s %s\n" "$cmd:" "$warn" "$crit" "$dir"

	echo "# kusa autogenerated file from $sect.$entry = $val" >$out.tmp
	echo "# ::do_not_edit::" >>$out.tmp
	echo "command[$cmd]=$plugin_path -w $warn -c $crit -p $dir -m" >>$out.tmp
	installfile $out.tmp $instdir/$out nagios:nagios 444
	echo "$out" >>$donefile
done








echo "  building checks: disk smart status"

stat=0
sect="cli-nagios-checksmart"

plugin="check_kusmart"
plugin_path=$(search_plugin $plugin) || {
	echo -e "\n  ERROR: plugin '$plugin' not found\n" >&2
	exit 1
}

for entry in $(jtconf --listvars $sect)
do
	val=$(jtconf $sect.$entry)
	device=$val
	cmd="kusmart_$entry"
	out="check_$cmd.cfg"

	printf "    command %-16s %s\n" "$cmd:" "$val"

	echo "# kusa autogenerated file from $sect.$entry = $val" >$out.tmp
	echo "# ::do_not_edit::" >>$out.tmp
	echo "command[$cmd]=$plugin_path $device" >>$out.tmp
	installfile $out.tmp $instdir/$out nagios:nagios 444
	echo "$out" >>$donefile
done






echo "  building checks: procs"

stat=0
sect="cli-nagios-checkproc"

plugin="check_proc_regexp"
plugin_path=$(search_plugin check_proc_regexp) || {
	echo -e "\n  ERROR: plugin '$plugin' not found\n" >&2
	exit 1
}

for entry in $(jtconf --listvars $sect)
do
	val=$(jtconf $sect.$entry)
	cmd="proc_$entry"
	out="check_$cmd.cfg"

	printf "    command %-16s %s %s\n" "$cmd:" "$plugin" "$val"

	echo "# kusa autogenerated file from $sect.$entry = $val" >$out.tmp
	echo "# ::do_not_edit::" >>$out.tmp
	echo "command[$cmd]=$plugin_path $val" >>$out.tmp
	installfile $out.tmp $instdir/$out nagios:nagios 444
	echo "$out" >>$donefile
done



# cleanup
#
for file in $( (cd $instdir; fgrep -l ' kusa autogenerated ' check_*.cfg) )
do
	grep -q "^$file$" $donefile || {
		echo "  purging obsolete check '$instdir/$file'"
		rm -f "$instdir/$file"
	}
done


# ensure that nagios user can walk around
#
usermod -G root nagios


$SOMETHING_CHANGED && {
	echo -e "\n  $MODNAME: changes detected, restarting services ...\n"
	restart_service nagios-nrpe-server nrpe || exit 1
}

exit 0
