3 # init.d script for Torrus
4 # Install it as /etc/init.d/torrus (most UNIXes),
5 # or as /usr/local/etc/rc.d/torrus.sh (FreeBSD), or probably somewhere else.
7 # $Id: torrus.in,v 1.1 2010-12-27 00:04:39 ivan Exp $
8 # Stanislav Sinyagin <ssinyagin@yahoo.com>
12 # chkconfig: 2345 90 10
13 # description: Starts/Stops Torrus collectors and monitors
17 # Provides: collector monitor
18 # Required-Start: $network
19 # Required-Stop: $network
21 # Default-Stop: 0 1 2 6
22 # Short-Description: Torrus collectors and monitors
23 # Description: Start/stop Torrus collectors and monitors
27 sysconfdir=@sysconfdir@
32 torrus_config_pl=@cfgdefdir@/torrus-config.pl
34 . @cfgdefdir@/initscript.conf
35 if test -f @siteconfdir@/initscript.siteconf; then
36 . @siteconfdir@/initscript.siteconf
39 if test "$TORRUS_CHANGE_UID" = yes -a \
40 "${LOGNAME:-root}" = root -a \
49 # Second argument can be the daemon name
50 if test x"$2" = x; then
51 daemons="collector monitor"
52 monitor_cmdopts="$TORRUS_MONITOR_DELAY"
56 # Third and fourth arguments may be the tree name and instance
57 if test x"$3" != x -a x"$4" != x; then
58 eval trees_${daemons}=$3
64 if test x"$TORRUS_COLLECTOR_CMDOPTS" != x; then
65 collector_cmdopts="$TORRUS_COLLECTOR_CMDOPTS"
68 if test x${read_treenames} != xno; then
70 # Get the names of the trees for each daemon
71 for d in ${daemons}; do
72 eval trees_${d}=\"`@PERL@ -e 'require "'$torrus_config_pl'";
73 while((my $key, $val) = each %Torrus::Global::treeConfig) {
74 print "$key " if $val->{run}{'${d}'};
78 # Get the collector instance numbers for each tree
79 eval trees=\"\$\{trees_collector\}\"
81 eval col_inst_${t}=\"`@PERL@ -e 'require "'$torrus_config_pl'";
83 (0 .. $Torrus::Global::treeConfig{'${t}'}{run}{collector}-1))'`\"
88 for d in ${daemons}; do
89 eval trees=\"\$\{trees_${d}\}\"
90 eval daemon_cmdopts=\"\$\{${d}_cmdopts\}\"
93 if test ${d} = collector; then
94 eval instances=\"\$\{col_inst_${t}\}\"
95 for i in ${instances}; do
96 echo "starting Torrus collector instance ${i} for tree ${t}"
97 ${su} "${cmddir}/${d} --tree=${t} --instance=${i} \
98 ${daemon_cmdopts} ${TORRUS_CMDOPTS}"
101 echo "starting Torrus ${d} for tree ${t} ${daemon_cmdopts}"
102 ${su} "${cmddir}/${d} --tree=${t} ${daemon_cmdopts} ${TORRUS_CMDOPTS}"
107 # RHEL based systems (RHEL, CentOS, Fedora) ignore the KXXtorrus script
108 # unless the corresponding lock is present
109 if test -d /var/lock/subsys; then
110 touch /var/lock/subsys/torrus
116 for d in ${daemons}; do
118 eval trees=\"\$\{trees_${d}\}\"
119 for t in ${trees}; do
121 if test ${d} = collector; then
122 eval instances=\"\$\{col_inst_${t}\}\"
124 for i in ${instances}; do
125 pidfile="${piddir}/${d}.${t}_${i}.pid"
126 if test -r ${pidfile}; then
127 tokill=${tokill}' t='${t}'_'${i}';d='${d}
128 echo "stopping Torrus collector instance ${i} for tree ${t}"
131 echo "Error: Cannot kill collector instance ${i} for tree ${t}"
137 pidfile="${piddir}/${d}.${t}.pid"
138 if test -r ${pidfile}; then
139 tokill=${tokill}' t='${t}';d='${d}
140 echo "stopping Torrus ${d} for tree ${t}"
142 @KILL@ ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
149 killed=`echo $tokill | wc -w`
151 kc=$TORRUS_KILL_COUNT
152 while test $killed -gt 0 -a $kc -gt 0; do
153 echo "Sleeping for $TORRUS_KILL_SLEEP seconds to allow processes to exit"
154 @SLEEP@ $TORRUS_KILL_SLEEP
155 echo "Checking for kill resistant processes [$kc/$TORRUS_KILL_COUNT]"
157 for tuple in $tokill; do
159 pidfile="${piddir}/${d}.${t}.pid"
160 if test -r ${pidfile}; then
161 echo " Sending kill signal to Torrus ${d} for tree ${t}"
163 @KILL@ ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
165 echo "${d} for ${t} has stopped"
166 killed=`expr $killed - 1`
171 if test \( $killed -gt 0 \); then
172 echo " Killing Remaining Processes"
173 for tuple in $tokill; do
175 pidfile="${piddir}/${d}.${t}.pid"
176 if test -r ${pidfile}; then
177 echo " Sending final kill -9 to Torrus ${d} for tree ${t}"
179 @KILL@ -9 ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
182 echo "${d} for ${t} has stopped"
188 if test x"$all_daemons" = xyes -a -d /var/lock/subsys; then
189 rm -f /var/lock/subsys/torrus
194 'start') start_daemons
199 'restart') stop_daemons; start_daemons
202 *) echo "Usage: $0 [start|stop|restart] [collector|monitor] [tree]"
209 # indent-tabs-mode: nil