1#!/bin/sh
2#
3# collectd - start and stop the statistics collection daemon
4# http://collectd.org/
5#
6# Copyright (C) 2005-2006 Florian Forster <octo@verplant.org>
7# Copyright (C) 2006-2009 Sebastian Harl <tokkee@debian.org>
8#
9
10### BEGIN INIT INFO
11# Provides:          collectd
12# Required-Start:    $local_fs $remote_fs
13# Required-Stop:     $local_fs $remote_fs
14# Should-Start:      $network $named $syslog $time cpufrequtils
15# Should-Stop:       $network $named $syslog
16# Default-Start:     2 3 4 5
17# Default-Stop:      0 1 6
18# Short-Description: manage the statistics collection daemon
19# Description:       collectd is the statistics collection daemon.
20#                    It is a small daemon which collects system information
21#                    periodically and provides mechanisms to monitor and store
22#                    the values in a variety of ways.
23### END INIT INFO
24
25. /etc/init.d/functions
26
27export PATH=/sbin:/bin:/usr/sbin:/usr/bin
28
29DISABLE=0
30
31NAME=collectd
32DAEMON=/usr/sbin/collectd
33
34CONFIGFILE=/etc/collectd.conf
35PIDFILE=/var/run/collectd.pid
36
37USE_COLLECTDMON=1
38COLLECTDMON_DAEMON=/usr/sbin/collectdmon
39COLLECTDMON_PIDFILE=/var/run/collectdmon.pid
40
41MAXWAIT=30
42
43# Gracefully exit if the package has been removed.
44test -x $DAEMON || exit 0
45
46if [ -r /etc/default/$NAME ]; then
47	. /etc/default/$NAME
48fi
49
50if test "$ENABLE_COREFILES" = 1; then
51	ulimit -c unlimited
52fi
53
54if test "$USE_COLLECTDMON" = 1; then
55	_PIDFILE="$COLLECTDMON_PIDFILE"
56else
57	_PIDFILE="$PIDFILE"
58fi
59
60# return:
61#   0 if config is fine
62#   1 if there is a syntax error
63#   2 if there is no configuration
64check_config() {
65	if test ! -e "$CONFIGFILE"; then
66		return 2
67	fi
68	if ! $DAEMON -t -C "$CONFIGFILE"; then
69		return 1
70	fi
71	return 0
72}
73
74# return:
75#   0 if the daemon has been started
76#   1 if the daemon was already running
77#   2 if the daemon could not be started
78#   3 if the daemon was not supposed to be started
79d_start() {
80	if test "$DISABLE" != 0; then
81		# we get here during restart
82		echo "disabled by /etc/default/$NAME"
83		return 3
84	fi
85
86	if test ! -e "$CONFIGFILE"; then
87		# we get here during restart
88		echo "disabled, no configuration ($CONFIGFILE) found"
89		return 3
90	fi
91
92	check_config
93	rc="$?"
94	if test "$rc" -ne 0; then
95		echo "not starting, configuration error"
96		return 2
97	fi
98
99	if test "$USE_COLLECTDMON" = 1; then
100		start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
101			--exec $COLLECTDMON_DAEMON -- -P "$_PIDFILE" -- -C "$CONFIGFILE" \
102			|| return 2
103	else
104		start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
105			--exec $DAEMON -- -C "$CONFIGFILE" -P "$_PIDFILE" \
106			|| return 2
107	fi
108	return 0
109}
110
111still_running_warning="
112WARNING: $NAME might still be running.
113In large setups it might take some time to write all pending data to
114the disk. You can adjust the waiting time in /etc/default/collectd."
115
116# return:
117#   0 if the daemon has been stopped
118#   1 if the daemon was already stopped
119#   2 if daemon could not be stopped
120d_stop() {
121	PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
122
123	start-stop-daemon --stop --quiet --oknodo --pidfile "$_PIDFILE"
124	rc="$?"
125
126	if test "$rc" -eq 2; then
127		return 2
128	fi
129
130	sleep 1
131	if test -n "$PID" && kill -0 $PID 2> /dev/null; then
132		i=0
133		while kill -0 $PID 2> /dev/null; do
134			i=$(( $i + 2 ))
135			echo -n " ."
136
137			if test $i -gt $MAXWAIT; then
138				echo "$still_running_warning"
139				return 2
140			fi
141
142			sleep 2
143		done
144		return "$rc"
145	fi
146	return "$rc"
147}
148
149# return:
150#   0 if the daemon is running
151#   3 if the daemon is stopped
152d_status(){
153	if test "$USE_COLLECTDMON" = 1; then
154		status $COLLECTDMON_DAEMON
155	else
156		status $DAEMON
157	fi
158}
159
160case "$1" in
161	start)
162		echo -n "Starting $NAME"
163		d_start
164		case "$?" in
165			0|1) echo "." ;;
166			*) exit 1 ;;
167		esac
168		;;
169	stop)
170		echo -n "Stopping $NAME"
171		d_stop
172		case "$?" in
173			0|1) echo "." ;;
174			*) exit 1 ;;
175		esac
176		;;
177	status)
178		d_status
179		;;
180	restart|force-reload)
181		echo -n "Restarting $NAME"
182		check_config
183		rc="$?"
184		if test "$rc" -eq 1; then
185			echo "not restarting, configuration error"
186			exit 1
187		fi
188		d_stop
189		rc="$?"
190		case "$rc" in
191			0|1)
192				sleep 1
193				d_start
194				rc2="$?"
195				case "$rc2" in
196					0|1) echo "." ;;
197					*) exit 1 ;;
198				esac
199				;;
200			*)
201				exit 1
202				;;
203		esac
204		;;
205	*)
206		echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
207		exit 3
208		;;
209esac
210
211# vim: syntax=sh noexpandtab sw=4 ts=4 :
212
213