diff --git a/sysadmin/dnscheck.config b/sysadmin/dnscheck.config new file mode 100644 index 0000000..a6144fa --- /dev/null +++ b/sysadmin/dnscheck.config @@ -0,0 +1,7 @@ +# Place this in /etc/config/dnscheck + +config setup 'check_service' + option host 'CHANGEME' + option server '127.0.0.1' + option timeout '5' + option maxfailures '3' diff --git a/sysadmin/dnscheck.init b/sysadmin/dnscheck.init new file mode 100644 index 0000000..23325fe --- /dev/null +++ b/sysadmin/dnscheck.init @@ -0,0 +1,41 @@ +#!/bin/sh /etc/rc.common +# +# start dnscheck +# +# https://openwrt.org/docs/guide-developer/procd-init-script-example + +# Init sequence +START=99 +STOP=10 + +# PROCD +USE_PROCD=1 + +start_service() { + procd_open_instance dnscheck + procd_set_param command /bin/sh "/usr/lib/dnscheck/dnscheck.sh" + + procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5} + + procd_set_param stdout 1 # forward stdout of the command to logd + procd_set_param stderr 1 # same for stderr + + procd_set_param pidfile /var/run/dnscheck.pid + + procd_close_instance +} + +stop_service() { + logger -t "dnscheck" "kresd monitoring stopped" +} + +service_triggers() +{ + procd_add_reload_trigger "dnscheck" +} + +reload_service() +{ + stop + start +} diff --git a/sysadmin/turrisos-restart-kresd.sh b/sysadmin/turrisos-restart-kresd.sh new file mode 100644 index 0000000..3593dfa --- /dev/null +++ b/sysadmin/turrisos-restart-kresd.sh @@ -0,0 +1,64 @@ +# Checks DNS and restarts kresd if necessary + +# check prerequisites + +. /lib/functions.sh + +if [ ! -x "$(command -v host)" ]; then + echo 'dnscheck error: host is not installed, exiting' >&2 + exit 1 +fi + +perform_check() { + + local host + local server + local timeout + local maxfailures + local failure=0 + + config_get host check_service host "CHANGEME" + config_get server check_service server "10.64.0.1" + config_get timeout check_service timeout "5" + config_get maxfailures check_service maxfailures "3" + + # check DNS + host -w "${timeout}" "${host}" "${server}" &> /dev/null + if [ $? -ne 0 ]; then + failure=1 + logger -p err -t "dnscheck" "kresd failed to respond, ${failcount} of ${maxfailures}" + let "failcount++" + else + if [ $failcount -ge 0 ]; then + logger -p info -t "dnscheck" "kresd is responding again" + fi + + failcount=0 + fi + + if [ $failcount -ge $maxfailures ]; then + failcount=0 + logger -p err -t "dnscheck" "kresd not responding, restarting" + + # restart service + /etc/init.d/resolver restart + fi + + return $failure +} + + +# read configuration +config_load dnscheck + +# check loop +logger -p info -t "dnscheck" "Starting kresd monitoring" +failcount=0 +while sleep 10; do + perform_check ${job} + if [ $? -ne 0 ]; then + logger -p err -t "dnscheck" "kresd did not respond and was restarted" + fi +done + +exit 0