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 echo "$NAME has been disabled - see /etc/default/$NAME."
55 exit 0
56 fi
58 if test ! -e "$CONFIGFILE" -a "$1" == "start"; then
59 echo "Not starting $NAME - 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 if test -n "$1"; then
76 echo "$1" >&2
77 fi
78 exit 1
79 fi
80 }
82 d_start() {
83 if test "$DISABLE" != 0; then
84 # we get here during restart
85 echo -n " - disabled by /etc/default/$NAME"
86 return 0
87 fi
89 if test ! -e "$CONFIGFILE"; then
90 # we get here during restart
91 echo -n " - no configuration ($CONFIGFILE) found."
92 return 0
93 fi
95 check_config
97 if test "$USE_COLLECTDMON" == 1; then
98 start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
99 --exec $COLLECTDMON_DAEMON -- -P "$_PIDFILE" -- -C "$CONFIGFILE"
100 else
101 start-stop-daemon --start --quiet --oknodo --pidfile "$_PIDFILE" \
102 --exec $DAEMON -- -C "$CONFIGFILE" -P "$_PIDFILE"
103 fi
104 }
106 still_running_warning="
107 WARNING: $NAME might still be running.
108 In large setups it might take some time to write all pending data to
109 the disk. You can adjust the waiting time in /etc/default/collectd."
111 d_stop() {
112 PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
114 start-stop-daemon --stop --quiet --oknodo --pidfile "$_PIDFILE"
116 sleep 1
117 if test -n "$PID" && kill -0 $PID 2> /dev/null; then
118 i=0
119 while kill -0 $PID 2> /dev/null; do
120 i=$(( $i + 2 ))
121 echo -n " ."
123 if test $i -gt $MAXWAIT; then
124 echo "$still_running_warning" >&2
125 return 1
126 fi
128 sleep 2
129 done
130 return 0
131 fi
132 }
134 d_status() {
135 PID=$( cat "$_PIDFILE" 2> /dev/null ) || true
137 if test -n "$PID" && kill -0 $PID 2> /dev/null; then
138 echo "collectd ($PID) is running."
139 exit 0
140 else
141 PID=$( pidof collectd ) || true
143 if test -n "$PID"; then
144 echo "collectd ($PID) is running."
145 exit 0
146 else
147 if test -f "$_PIDFILE"; then
148 echo "collectd is stopped but PID file exists."
149 exit 1
150 fi
151 echo "collectd is stopped."
152 exit 3
153 fi
154 fi
155 echo "status of collectd unknown."
156 exit 4
157 }
159 case "$1" in
160 start)
161 echo -n "Starting $DESC: $NAME"
162 d_start
163 echo "."
164 ;;
165 stop)
166 echo -n "Stopping $DESC: $NAME"
167 d_stop
168 echo "."
169 ;;
170 status)
171 d_status
172 ;;
173 restart|force-reload)
174 echo -n "Restarting $DESC: $NAME"
175 check_config "Not restarting collectd."
176 d_stop
177 sleep 1
178 d_start
179 echo "."
180 ;;
181 *)
182 echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
183 exit 1
184 ;;
185 esac
187 exit 0
189 # vim: syntax=sh noexpandtab sw=4 ts=4 :