1238106Sdes#!/bin/sh
2238106Sdes# validation reporter - reports validation failures to a collection server.
3238106Sdes# Copyright NLnet Labs, 2010
4238106Sdes# BSD license.
5238106Sdes
6238106Sdes
7238106Sdes###
8238106Sdes# Here is the configuration for the validation reporter
9238106Sdes# it greps the failure lines out of the log and sends them to a server.
10238106Sdes
11238106Sdes# The pidfile for the reporter daemon.
12238106Sdespidfile="/var/run/validation-reporter.pid"
13238106Sdes
14238106Sdes# The logfile to watch for logged validation failures.
15238106Sdeslogfile="/var/log/unbound.log"
16238106Sdes
17238106Sdes# how to notify the upstream 
18238106Sdes# nc is netcat, it sends tcp to given host port.  It makes a tcp connection
19238106Sdes# and writes one log-line to it (grepped from the logfile).
20238106Sdes# the notify command can be: "nc the.server.name.org 1234"
21238106Sdes# the listening daemon could be:  nc -lk 127.0.0.1 1234 >> outputfile &
22238106Sdesnotify_cmd="nc localhost 1234"
23238106Sdes
24238106Sdes
25238106Sdes###
26238106Sdes# Below this line is the code for the validation reporter,
27238106Sdes# first the daemon itself, then the controller for the daemon.
28238106Sdesreporter_daemon() {
29238106Sdes	trap "rm -f \"$pidfile\"" EXIT
30238106Sdes	tail -F $logfile | grep --line-buffered "unbound.*info: validation failure" | \
31238106Sdes	while read x; do
32238106Sdes		echo "$x" | $notify_cmd
33238106Sdes	done
34238106Sdes}
35238106Sdes
36238106Sdes
37238106Sdes###
38238106Sdes# controller for daemon.
39238106Sdesstart_daemon() {
40238106Sdes	echo "starting reporter"
41238106Sdes	nohup $0 rundaemon </dev/null >/dev/null 2>&1 &
42238106Sdes	echo $! > "$pidfile"
43238106Sdes}
44238106Sdes
45238106Sdeskill_daemon() {
46238106Sdes	echo "stopping reporter"
47238106Sdes	if test -s "$pidfile"; then
48238106Sdes		kill `cat "$pidfile"`
49238106Sdes		# check it is really dead
50238106Sdes		if kill -0 `cat "$pidfile"` >/dev/null 2>&1; then
51238106Sdes			sleep 1
52238106Sdes			while kill -0 `cat "$pidfile"` >/dev/null 2>&1; do
53238106Sdes				kill `cat "$pidfile"` >/dev/null 2>&1
54238106Sdes				echo "waiting for reporter to stop"
55238106Sdes				sleep 1
56238106Sdes			done
57238106Sdes		fi
58238106Sdes	fi
59238106Sdes}
60238106Sdes
61238106Sdesget_status_daemon() {
62238106Sdes	if test -s "$pidfile"; then
63238106Sdes		if kill -0 `cat "$pidfile"`; then
64238106Sdes			return 0;
65238106Sdes		fi
66238106Sdes	fi
67238106Sdes	return 1;
68238106Sdes}
69238106Sdes
70238106Sdesrestart_daemon() {
71238106Sdes	kill_daemon
72238106Sdes	start_daemon
73238106Sdes}
74238106Sdes
75238106Sdescondrestart_daemon() {
76238106Sdes	if get_status_daemon; then
77238106Sdes		echo "reporter ("`cat "$pidfile"`") is running"
78238106Sdes		exit 0
79238106Sdes	fi
80238106Sdes	start_daemon
81238106Sdes	exit 0
82238106Sdes}
83238106Sdes
84238106Sdesstatus_daemon() {
85238106Sdes	if get_status_daemon; then
86238106Sdes		echo "reporter ("`cat "$pidfile"`") is running"
87238106Sdes		exit 0
88238106Sdes	fi
89238106Sdes	echo "reporter is not running"
90238106Sdes	exit 1
91238106Sdes}
92238106Sdes
93238106Sdescase "$1" in
94238106Sdes	rundaemon)
95238106Sdes		reporter_daemon
96238106Sdes	;;
97238106Sdes	start)
98238106Sdes		start_daemon
99238106Sdes	;;
100238106Sdes	stop)
101238106Sdes		kill_daemon
102238106Sdes	;;
103238106Sdes	restart)
104238106Sdes		restart_daemon
105238106Sdes	;;
106238106Sdes	condrestart)
107238106Sdes		condrestart_daemon
108238106Sdes	;;
109238106Sdes	status)
110238106Sdes		status_daemon
111238106Sdes	;;
112238106Sdes	*)
113238106Sdes		echo "Usage: $0 {start|stop|restart|condrestart|status}"
114238106Sdes		exit 2
115238106Sdes	;;
116238106Sdesesac
117238106Sdesexit $?
118