Code

Merged branch 'collectd-4'.
[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 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 "$DISABLE" != 0 -a "$1" == "start"; then
52         echo "$NAME has been disabled - see /etc/default/$NAME."
53         exit 0
54 fi
56 if test ! -e "$CONFIGFILE" -a "$1" == "start"; then
57         echo "Not starting $NAME - no configuration ($CONFIGFILE) found."
58         exit 0
59 fi
61 if test "$ENABLE_COREFILES" == 1; then
62         ulimit -c unlimited
63 fi
65 if test "$USE_COLLECTDMON" == 1; then
66         _PIDFILE="$COLLECTDMON_PIDFILE"
67 else
68         _PIDFILE="$PIDFILE"
69 fi
71 check_config() {
72         if ! $DAEMON -t -C "$CONFIGFILE"; then
73                 if test -n "$1"; then
74                         echo "$1" >&2
75                 fi
76                 exit 1
77         fi
78 }
80 d_start() {
81         if test "$DISABLE" != 0; then
82                 # we get here during restart
83                 echo -n " - disabled by /etc/default/$NAME"
84                 return 0
85         fi
87         if test ! -e "$CONFIGFILE"; then
88                 # we get here during restart
89                 echo -n " - no configuration ($CONFIGFILE) found."
90                 return 0
91         fi
93         check_config
95         if test "$USE_COLLECTDMON" == 1; then
96                 start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
97                         --exec $COLLECTDMON_DAEMON -- -P "$_PIDFILE" -- -C "$CONFIGFILE"
98         else
99                 start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
100                         --exec $DAEMON -- -C "$CONFIGFILE" -P "$_PIDFILE"
101         fi
104 still_running_warning="
105 WARNING: $NAME might still be running.
106 In large setups it might take some time to write all pending data to
107 the disk. You can adjust the waiting time in /etc/default/collectd."
109 d_stop() {
110         PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
112         start-stop-daemon --stop --quiet --oknodo --pidfile "$_PIDFILE"
114         sleep 1
115         if test -n "$PID" && kill -0 $PID 2> /dev/null; then
116                 i=0
117                 while kill -0 $PID 2> /dev/null; do
118                         i=$(( $i + 2 ))
119                         echo -n " ."
121                         if test $i -gt $MAXWAIT; then
122                                 echo "$still_running_warning" >&2
123                                 return 1
124                         fi
126                         sleep 2
127                 done
128                 return 0
129         fi
132 d_status() {
133         PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
135         if test -n "$PID" && kill -0 $PID 2> /dev/null; then
136                 echo "collectd ($PID) is running."
137                 exit 0
138         else
139                 PID=$( pidof collectd ) || true
141                 if test -n "$PID"; then
142                         echo "collectd ($PID) is running."
143                         exit 0
144                 else
145                         if test -f "$_PIDFILE"; then
146                                 echo "collectd is stopped but PID file exists."
147                                 exit 1
148                         fi
149                         echo "collectd is stopped."
150                         exit 3
151                 fi
152         fi
153         echo "status of collectd unknown."
154         exit 4
157 case "$1" in
158         start)
159                 echo -n "Starting $DESC: $NAME"
160                 d_start
161                 echo "."
162                 ;;
163         stop)
164                 echo -n "Stopping $DESC: $NAME"
165                 d_stop
166                 echo "."
167                 ;;
168         status)
169                 d_status
170                 ;;
171         restart|force-reload)
172                 echo -n "Restarting $DESC: $NAME"
173                 check_config "Not restarting collectd."
174                 d_stop
175                 sleep 1
176                 d_start
177                 echo "."
178                 ;;
179         *)
180                 echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
181                 exit 1
182                 ;;
183 esac
185 exit 0
187 # vim: syntax=sh noexpandtab sw=4 ts=4 :