#! /bin/bash

#$title$ Trivial File Transfer Protocol (TFTP) daemon
#$check$ test file is readable, reasonable number of daemons is running
#$ref$ tftpd(8), /srv/tftp/, /var/log/daemon.log
#$author$ Rafal Rzeczkowski
#$version$ 0.70

level_check short

#CHANGELOG
#0.50	initial @IT
#0.60	check ToD service
#0.70	move ToD check to its own plugin

DAEMON_COUNT_MAX=20
TEST_FILE='etc/localtime'

DAEMON_COUNT=$(pgrep -c 'in.tftpd')

if [[ $DAEMON_COUNT -eq 0 ]]; then
	fail critical 'daemon not running'
	exit
elif [[ $DAEMON_COUNT -gt $DAEMON_COUNT_MAX ]]; then
	fail warning "too many ($DAEMON_COUNT) daemons running; expected <$DAEMON_COUNT_MAX"
	helpmsg 'ensure TFTP sessions are being terminated with ACKs'
	exit
else
	echodebug "$DAEMON_COUNT in.tftpd daemon(s) active"
fi

TMP=$(mktemp)
LOG=$(mktemp)
TARGET='localhost' # also works in v6 mode as 'ip6-localhost'
for AF in 4 6; do
# verify that the selected address family is functional
ip -$AF route show|awk '{if ($1=="default"){exit 1}}' && continue

if tftp -$AF -m binary $TARGET -c get $TEST_FILE $TMP > $LOG; then
	if [[ ! -s $TMP ]]; then
		fail warning "empty test file $TEST_FILE received from $TARGET: $(<$LOG)"
		rm $TMP $LOG
		exit
	elif ! cmp /$TEST_FILE $TMP; then
		fail warning "test file $TEST_FILE does not match source"
		rm $TMP $LOG
		exit
	else
		echodebug "received good copy of tftp://$TARGET/$TEST_FILE (IPv$AF)"
	fi
else
	fail critical "general failure accessing TFTP on $TARGET: $(<$LOG)"
	rm $TMP $LOG
	exit
fi

done
rm $TMP $LOG

ok
# vim: cindent:shiftwidth=4:tabstop=4:smarttab:textwidth=100
