This commit was generated by cvs2svn to compensate for changes in r10640,
[freeside.git] / torrus / init.d / torrus.in
1 #!@SHELL@
2 #
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.
6 #
7 # $Id: torrus.in,v 1.1 2010-12-27 00:04:39 ivan Exp $
8 # Stanislav Sinyagin <ssinyagin@yahoo.com>
9 #
10 #
11 ### chkconfig info
12 # chkconfig: 2345 90 10
13 # description: Starts/Stops Torrus collectors and monitors
14 ###
15 #
16 ### BEGIN INIT INFO
17 # Provides: collector monitor
18 # Required-Start: $network
19 # Required-Stop: $network
20 # Default-Start: 3 5
21 # Default-Stop: 0 1 2 6
22 # Short-Description: Torrus collectors and monitors
23 # Description: Start/stop Torrus collectors and monitors
24 ### END INIT INFO
25
26 prefix=@prefix@
27 sysconfdir=@sysconfdir@
28 pkghome=@pkghome@
29 cmddir=@pkgbindir@
30 piddir=@piddir@
31 sitedir=@sitedir@
32 torrus_config_pl=@cfgdefdir@/torrus-config.pl
33
34 . @cfgdefdir@/initscript.conf
35 if test -f @siteconfdir@/initscript.siteconf; then
36   . @siteconfdir@/initscript.siteconf
37 fi
38
39 if test "$TORRUS_CHANGE_UID" = yes -a \
40         "${LOGNAME:-root}" = root -a \
41         "@SU@" != "no"; then
42   user=@torrus_user@
43   su="@SU@ ${user} -c"
44 else
45   su="@SHELL@ -c"
46 fi
47
48
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"
53   all_daemons=yes
54 else
55   daemons=$2
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
59     eval col_inst_${3}=$4
60     read_treenames=no
61   fi
62 fi
63
64 if test x"$TORRUS_COLLECTOR_CMDOPTS" != x; then
65   collector_cmdopts="$TORRUS_COLLECTOR_CMDOPTS"
66 fi
67
68 if test x${read_treenames} != xno; then
69
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}'};
75     };'`\"
76   done
77
78   # Get the collector instance numbers for each tree
79   eval trees=\"\$\{trees_collector\}\"  
80   for t in ${trees}; do
81     eval col_inst_${t}=\"`@PERL@ -e 'require "'$torrus_config_pl'";
82     print join(" ",
83     (0 .. $Torrus::Global::treeConfig{'${t}'}{run}{collector}-1))'`\"
84   done
85 fi
86
87 start_daemons () {
88   for d in ${daemons}; do
89     eval trees=\"\$\{trees_${d}\}\"
90     eval daemon_cmdopts=\"\$\{${d}_cmdopts\}\"
91         
92     for t in ${trees}; do
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}"
99         done
100       else
101         echo "starting Torrus ${d} for tree ${t} ${daemon_cmdopts}"
102         ${su} "${cmddir}/${d} --tree=${t} ${daemon_cmdopts} ${TORRUS_CMDOPTS}"
103       fi
104     done
105   done
106
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
111   fi
112 }
113
114 stop_daemons () {
115   tokill=""
116   for d in ${daemons}; do
117   
118     eval trees=\"\$\{trees_${d}\}\"
119     for t in ${trees}; do
120     
121       if test ${d} = collector; then
122         eval instances=\"\$\{col_inst_${t}\}\"
123         
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}"
129             pid=`cat ${pidfile}`
130             @KILL@ ${pid} || \
131               echo "Error: Cannot kill collector instance ${i} for tree ${t}"
132           fi
133         done
134         
135       else
136             
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}"
141           pid=`cat ${pidfile}`
142           @KILL@ ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
143         fi
144         
145       fi
146     done
147   done
148
149   killed=`echo $tokill | wc -w`
150   notdead=1
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]"
156     kc=`expr $kc - 1`
157     for tuple in $tokill; do
158       eval $tuple
159       pidfile="${piddir}/${d}.${t}.pid"
160       if test -r ${pidfile}; then
161         echo "  Sending kill signal to Torrus ${d} for tree ${t}"
162         pid=`cat ${pidfile}`
163         @KILL@ ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
164       else
165         echo "${d} for ${t} has stopped"
166         killed=`expr $killed - 1`
167       fi
168     done
169   done
170
171   if test \( $killed -gt 0 \); then
172     echo "  Killing Remaining Processes"
173    for tuple in $tokill; do
174      eval $tuple
175      pidfile="${piddir}/${d}.${t}.pid"
176      if test -r ${pidfile}; then
177        echo "  Sending final kill -9 to Torrus ${d} for tree ${t}"
178        pid=`cat ${pidfile}`
179        @KILL@ -9 ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
180        @RM@ -f $pidfile
181      else
182        echo "${d} for ${t} has stopped"
183      fi
184    done
185   fi
186
187   # RHEL specifics
188   if test x"$all_daemons" = xyes -a -d /var/lock/subsys; then
189     rm -f /var/lock/subsys/torrus
190   fi
191 }
192
193 case "$1" in
194   'start')    start_daemons
195   ;;
196
197   'stop')     stop_daemons
198   ;;
199   'restart')  stop_daemons; start_daemons
200   ;;
201
202   *)  echo "Usage: $0 [start|stop|restart] [collector|monitor] [tree]"
203   ;;
204   esac
205
206 # Local Variables:
207 # mode: shell-script
208 # sh-shell: sh
209 # indent-tabs-mode: nil
210 # sh-basic-offset: 2
211 # End: