1#!/bin/sh
2# validation reporter - reports validation failures to a collection server.
3# Copyright NLnet Labs, 2010
4# BSD license.
5
6
7###
8# Here is the configuration for the validation reporter
9# it greps the failure lines out of the log and sends them to a server.
10
11# The pidfile for the reporter daemon.
12pidfile="/var/run/validation-reporter.pid"
13
14# The logfile to watch for logged validation failures.
15logfile="/var/log/unbound.log"
16
17# how to notify the upstream 
18# nc is netcat, it sends tcp to given host port.  It makes a tcp connection
19# and writes one log-line to it (grepped from the logfile).
20# the notify command can be: "nc the.server.name.org 1234"
21# the listening daemon could be:  nc -lk 127.0.0.1 1234 >> outputfile &
22notify_cmd="nc localhost 1234"
23
24
25###
26# Below this line is the code for the validation reporter,
27# first the daemon itself, then the controller for the daemon.
28reporter_daemon() {
29	trap "rm -f \"$pidfile\"" EXIT
30	tail -F $logfile | grep --line-buffered "unbound.*info: validation failure" | \
31	while read x; do
32		echo "$x" | $notify_cmd
33	done
34}
35
36
37###
38# controller for daemon.
39start_daemon() {
40	echo "starting reporter"
41	nohup $0 rundaemon </dev/null >/dev/null 2>&1 &
42	echo $! > "$pidfile"
43}
44
45kill_daemon() {
46	echo "stopping reporter"
47	if test -s "$pidfile"; then
48		kill `cat "$pidfile"`
49		# check it is really dead
50		if kill -0 `cat "$pidfile"` >/dev/null 2>&1; then
51			sleep 1
52			while kill -0 `cat "$pidfile"` >/dev/null 2>&1; do
53				kill `cat "$pidfile"` >/dev/null 2>&1
54				echo "waiting for reporter to stop"
55				sleep 1
56			done
57		fi
58	fi
59}
60
61get_status_daemon() {
62	if test -s "$pidfile"; then
63		if kill -0 `cat "$pidfile"`; then
64			return 0;
65		fi
66	fi
67	return 1;
68}
69
70restart_daemon() {
71	kill_daemon
72	start_daemon
73}
74
75condrestart_daemon() {
76	if get_status_daemon; then
77		echo "reporter ("`cat "$pidfile"`") is running"
78		exit 0
79	fi
80	start_daemon
81	exit 0
82}
83
84status_daemon() {
85	if get_status_daemon; then
86		echo "reporter ("`cat "$pidfile"`") is running"
87		exit 0
88	fi
89	echo "reporter is not running"
90	exit 1
91}
92
93case "$1" in
94	rundaemon)
95		reporter_daemon
96	;;
97	start)
98		start_daemon
99	;;
100	stop)
101		kill_daemon
102	;;
103	restart)
104		restart_daemon
105	;;
106	condrestart)
107		condrestart_daemon
108	;;
109	status)
110		status_daemon
111	;;
112	*)
113		echo "Usage: $0 {start|stop|restart|condrestart|status}"
114		exit 2
115	;;
116esac
117exit $?
118