#!/bin/bash

. ${TOOLKIT}-functions.sh

# sanity check
#
var='cli-nagios.allowed_hosts'
val=$(jtconf $var)

echo "$val" | grep -q '[a-zA-Z]' && {
	echo >&2
	echo "  ERROR: hostnames not allowed, you must use only IPs" >&2
	echo "         $var='$val'" >&2
	exit 1
}


# server_address cannot be empty, needs to wipe out the whole entry
# we use MODLOCALDB to store temp kusa-db var
#
var='cli-nagios.server_address'
val=$(jtconf $var 2>/dev/null) || :

[ "X$val" != "X" ] && val="server_address=$val"

(
	echo "[temp]"
	echo "  srvaddr	$val"
) >>$MODLOCALDB



# build up automatic (defaults) values for disk checks
# (automatically add partitions from known paths)
#
# we store this info in a persistent, automatic kusa db file,
# at the same level of dist files (00)
#
save_db="/etc/kusa/conf.d/auto.00.generated-$MODNAME"
:> $save_db

echo "[cli-nagios-checkdisk]" >>$save_db
parms=$(jtconf cli-nagios.autocheckdisk_parms)
donelist='done.tmp'
:> $donelist


add_disk_check()
{
	local name=$1
	local path=$2

	mountpoint $path >/dev/null 2>&1 || return 0

	fstype=$(mount | grep " on $path " | sed -e 's/.* type //' -e 's/ .*//')
	case $fstype in
	    nfs*)	return 0 ;;
	    sshfs)	return 0 ;;
	esac

	manual=$(jtconf cli-nagios-checkdisk.$name 2>/dev/null) || :
	if [ "X$manual" = "X" ]
	then
		grep -q "^$path$" $donelist || {
			printf "  auto checkdisk %-10s %-6s %-10s %s\n" "$name:" "$fstype" "$parms" "$path"
			echo "  $name	$parms $path" >>$save_db
			echo "$path" >>$donelist
		}
	else
		echo "  auto checkdisk $name (on $path), manually overridden by '$manual'"
	fi
	return 0
}

add_disk_check root /

for var in $(jtconf --listvars path | grep -v '^rundir$')
do
	val=$(jtconf "path.$var")
	name=$(echo "$var" | sed -e 's/dir$//')

	add_disk_check $name $val
done


exit 0
