1 #! /bin/bash
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 #
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
25 . /lib/lsb/init-functions
27 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
29 DISABLE=0
31 DESC="statistics collection and monitoring daemon"
32 NAME=collectd
33 DAEMON=/usr/sbin/collectd
35 CONFIGFILE=/etc/collectd/collectd.conf
36 PIDFILE=/var/run/collectd.pid
38 USE_COLLECTDMON=1
39 COLLECTDMON_DAEMON=/usr/sbin/collectdmon
40 COLLECTDMON_PIDFILE=/var/run/collectdmon.pid
42 MAXWAIT=30
44 # Gracefully exit if the package has been removed.
45 test -x $DAEMON || exit 0
47 if [ -r /etc/default/$NAME ]; then
48 . /etc/default/$NAME
49 fi
51 if test "$ENABLE_COREFILES" == 1; then
52 ulimit -c unlimited
53 fi
55 if test "$USE_COLLECTDMON" == 1; then
56 _PIDFILE="$COLLECTDMON_PIDFILE"
57 else
58 _PIDFILE="$PIDFILE"
59 fi
61 # return:
62 # 0 if config is fine
63 # 1 if there is a syntax error
64 # 2 if there is no configuration
65 check_config() {
66 if test ! -e "$CONFIGFILE"; then
67 return 2
68 fi
69 if ! $DAEMON -t -C "$CONFIGFILE"; then
70 return 1
71 fi
72 return 0
73 }
75 # return:
76 # 0 if the daemon has been started
77 # 1 if the daemon was already running
78 # 2 if the daemon could not be started
79 # 3 if the daemon was not supposed to be started
80 d_start() {
81 if test "$DISABLE" != 0; then
82 # we get here during restart
83 log_progress_msg "disabled by /etc/default/$NAME"
84 return 3
85 fi
87 if test ! -e "$CONFIGFILE"; then
88 # we get here during restart
89 log_progress_msg "disabled, no configuration ($CONFIGFILE) found"
90 return 3
91 fi
93 check_config
94 rc="$?"
95 if test "$rc" -ne 0; then
96 log_progress_msg "not starting, configuration error"
97 return 2
98 fi
100 if test "$USE_COLLECTDMON" == 1; then
101 start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
102 --exec $COLLECTDMON_DAEMON -- -P "$_PIDFILE" -- -C "$CONFIGFILE" \
103 || return 2
104 else
105 start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
106 --exec $DAEMON -- -C "$CONFIGFILE" -P "$_PIDFILE" \
107 || return 2
108 fi
109 return 0
110 }
112 still_running_warning="
113 WARNING: $NAME might still be running.
114 In large setups it might take some time to write all pending data to
115 the disk. You can adjust the waiting time in /etc/default/collectd."
117 # return:
118 # 0 if the daemon has been stopped
119 # 1 if the daemon was already stopped
120 # 2 if daemon could not be stopped
121 d_stop() {
122 PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
124 start-stop-daemon --stop --quiet --oknodo --pidfile "$_PIDFILE"
125 rc="$?"
127 if test "$rc" -eq 2; then
128 return 2
129 fi
131 sleep 1
132 if test -n "$PID" && kill -0 $PID 2> /dev/null; then
133 i=0
134 while kill -0 $PID 2> /dev/null; do
135 i=$(( $i + 2 ))
136 echo -n " ."
138 if test $i -gt $MAXWAIT; then
139 log_progress_msg "$still_running_warning"
140 return 2
141 fi
143 sleep 2
144 done
145 return "$rc"
146 fi
147 return "$rc"
148 }
150 case "$1" in
151 start)
152 log_daemon_msg "Starting $DESC" "$NAME"
153 d_start
154 case "$?" in
155 0|1) log_end_msg 0 ;;
156 2) log_end_msg 1 ;;
157 3) log_end_msg 255; true ;;
158 *) log_end_msg 1 ;;
159 esac
160 ;;
161 stop)
162 log_daemon_msg "Stopping $DESC" "$NAME"
163 d_stop
164 case "$?" in
165 0|1) log_end_msg 0 ;;
166 2) log_end_msg 1 ;;
167 esac
168 ;;
169 status)
170 status_of_proc -p "$_PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
171 ;;
172 restart|force-reload)
173 log_daemon_msg "Restarting $DESC" "$NAME"
174 check_config
175 rc="$?"
176 if test "$rc" -eq 1; then
177 log_progress_msg "not restarting, configuration error"
178 log_end_msg 1
179 exit 1
180 fi
181 d_stop
182 rc="$?"
183 case "$rc" in
184 0|1)
185 sleep 1
186 d_start
187 rc2="$?"
188 case "$rc2" in
189 0|1) log_end_msg 0 ;;
190 2) log_end_msg 1 ;;
191 3) log_end_msg 255; true ;;
192 *) log_end_msg 1 ;;
193 esac
194 ;;
195 *)
196 log_end_msg 1
197 ;;
198 esac
199 ;;
200 *)
201 echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
202 exit 3
203 ;;
204 esac
206 # vim: syntax=sh noexpandtab sw=4 ts=4 :