Code

init.d: Use log_* and status_of_proc functions from LSB's init functions.
[pkg-collectd.git] / debian / collectd-core.collectd.init.d
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 set -e
27 . /lib/lsb/init-functions
29 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
31 DISABLE=0
33 DESC="statistics collection and monitoring daemon"
34 NAME=collectd
35 DAEMON=/usr/sbin/collectd
37 CONFIGFILE=/etc/collectd/collectd.conf
38 PIDFILE=/var/run/collectd.pid
40 USE_COLLECTDMON=1
41 COLLECTDMON_DAEMON=/usr/sbin/collectdmon
42 COLLECTDMON_PIDFILE=/var/run/collectdmon.pid
44 MAXWAIT=30
46 # Gracefully exit if the package has been removed.
47 test -x $DAEMON || exit 0
49 if [ -r /etc/default/$NAME ]; then
50         . /etc/default/$NAME
51 fi
53 if test "$DISABLE" != 0 -a "$1" == "start"; then
54         log_warning_msg "Not starting $DESC, disabled by /etc/default/$NAME."
55         exit 0
56 fi
58 if test ! -e "$CONFIGFILE" -a "$1" == "start"; then
59         log_warning_msg "Not starting $DESC, no configuration ($CONFIGFILE) found."
60         exit 0
61 fi
63 if test "$ENABLE_COREFILES" == 1; then
64         ulimit -c unlimited
65 fi
67 if test "$USE_COLLECTDMON" == 1; then
68         _PIDFILE="$COLLECTDMON_PIDFILE"
69 else
70         _PIDFILE="$PIDFILE"
71 fi
73 check_config() {
74         if ! $DAEMON -t -C "$CONFIGFILE"; then
75                 return 1
76         fi
77 }
79 d_start() {
80         if test "$DISABLE" != 0; then
81                 # we get here during restart
82                 log_progress_msg "disabled by /etc/default/$NAME"
83                 return 2
84         fi
86         if test ! -e "$CONFIGFILE"; then
87                 # we get here during restart
88                 log_progress_msg "disabled, no configuration ($CONFIGFILE) found"
89                 return 2
90         fi
92         check_config
93         rc="$?"
94         if test "$rc" -eq 1; then
95                 log_progress_msg "not starting, configuration error"
96                 return 2
97         fi
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
110 still_running_warning="
111 WARNING: $NAME might still be running.
112 In large setups it might take some time to write all pending data to
113 the disk. You can adjust the waiting time in /etc/default/collectd."
115 d_stop() {
116         PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
118         start-stop-daemon --stop --quiet --oknodo --pidfile "$_PIDFILE"
119         rc="$?"
121         if test "$rc" -eq 2; then
122                 return 2
123         fi
125         sleep 1
126         if test -n "$PID" && kill -0 $PID 2> /dev/null; then
127                 i=0
128                 while kill -0 $PID 2> /dev/null; do
129                         i=$(( $i + 2 ))
130                         echo -n " ."
132                         if test $i -gt $MAXWAIT; then
133                                 log_progress_msg "$still_running_warning"
134                                 return 2
135                         fi
137                         sleep 2
138                 done
139                 return "$rc"
140         fi
141         return "$rc"
144 case "$1" in
145         start)
146                 log_daemon_msg "Starting $DESC" "$NAME"
147                 d_start
148                 case "$?" in
149                         0|1) log_end_msg 0 ;;
150                         2) log_end_msg 1 ;;
151                 esac
152                 ;;
153         stop)
154                 log_daemon_msg "Stopping $DESC" "$NAME"
155                 d_stop
156                 case "$?" in
157                         0|1) log_end_msg 0 ;;
158                         2) log_end_msg 1 ;;
159                 esac
160                 ;;
161         status)
162                 status_of_proc -p "$_PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
163                 ;;
164         restart|force-reload)
165                 log_daemon_msg "Restarting $DESC" "$NAME"
166                 check_config
167                 rc="$?"
168                 if test "$rc" -eq 1; then
169                         log_progress_msg "not restarting, configuration error"
170                         log_end_msg 1
171                         exit 1
172                 fi
173                 d_stop
174                 rc="$?"
175                 case "$rc" in
176                         0|1)
177                                 sleep 1
178                                 d_start
179                                 rc2="$?"
180                                 case "$rc2" in
181                                         0) log_end_msg 0 ;;
182                                         *) log_end_msg 1 ;;
183                                 esac
184                                 ;;
185                         *)
186                                 log_end_msg 1
187                                 ;;
188                 esac
189                 ;;
190         *)
191                 echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
192                 exit 3
193                 ;;
194 esac
196 exit 0
198 # vim: syntax=sh noexpandtab sw=4 ts=4 :