summaryrefslogtreecommitdiff
path: root/torrus/init.d/torrus.in
blob: 91e14be479ba8009330902fdad57d5d69ab21885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!@SHELL@
#
# init.d script for Torrus
# Install it as /etc/init.d/torrus (most UNIXes),
# or as /usr/local/etc/rc.d/torrus.sh (FreeBSD), or probably somewhere else.
#
# $Id: torrus.in,v 1.1 2010-12-27 00:04:39 ivan Exp $
# Stanislav Sinyagin <ssinyagin@yahoo.com>
#
#
### chkconfig info
# chkconfig: 2345 90 10
# description: Starts/Stops Torrus collectors and monitors
###
#
### BEGIN INIT INFO
# Provides: collector monitor
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Torrus collectors and monitors
# Description: Start/stop Torrus collectors and monitors
### END INIT INFO

prefix=@prefix@
sysconfdir=@sysconfdir@
pkghome=@pkghome@
cmddir=@pkgbindir@
piddir=@piddir@
sitedir=@sitedir@
torrus_config_pl=@cfgdefdir@/torrus-config.pl

. @cfgdefdir@/initscript.conf
if test -f @siteconfdir@/initscript.siteconf; then
  . @siteconfdir@/initscript.siteconf
fi

if test "$TORRUS_CHANGE_UID" = yes -a \
        "${LOGNAME:-root}" = root -a \
        "@SU@" != "no"; then
  user=@torrus_user@
  su="@SU@ ${user} -c"
else
  su="@SHELL@ -c"
fi


# Second argument can be the daemon name
if test x"$2" = x; then
  daemons="collector monitor"
  monitor_cmdopts="$TORRUS_MONITOR_DELAY"
  all_daemons=yes
else
  daemons=$2
    # Third and fourth arguments may be the tree name and instance
  if test x"$3" != x -a x"$4" != x; then
    eval trees_${daemons}=$3
    eval col_inst_${3}=$4
    read_treenames=no
  fi
fi

if test x"$TORRUS_COLLECTOR_CMDOPTS" != x; then
  collector_cmdopts="$TORRUS_COLLECTOR_CMDOPTS"
fi

if test x${read_treenames} != xno; then

  # Get the names of the trees for each daemon
  for d in ${daemons}; do
    eval trees_${d}=\"`@PERL@ -e 'require "'$torrus_config_pl'";
    while((my $key, $val) = each %Torrus::Global::treeConfig) {
      print "$key " if $val->{run}{'${d}'};
    };'`\"
  done

  # Get the collector instance numbers for each tree
  eval trees=\"\$\{trees_collector\}\"  
  for t in ${trees}; do
    eval col_inst_${t}=\"`@PERL@ -e 'require "'$torrus_config_pl'";
    print join(" ",
    (0 .. $Torrus::Global::treeConfig{'${t}'}{run}{collector}-1))'`\"
  done
fi

start_daemons () {
  for d in ${daemons}; do
    eval trees=\"\$\{trees_${d}\}\"
    eval daemon_cmdopts=\"\$\{${d}_cmdopts\}\"
        
    for t in ${trees}; do
      if test ${d} = collector; then
        eval instances=\"\$\{col_inst_${t}\}\"
        for i in ${instances}; do
          echo "starting Torrus collector instance ${i} for tree ${t}"
          ${su} "${cmddir}/${d} --tree=${t} --instance=${i} \
               ${daemon_cmdopts} ${TORRUS_CMDOPTS}"
        done
      else
        echo "starting Torrus ${d} for tree ${t} ${daemon_cmdopts}"
        ${su} "${cmddir}/${d} --tree=${t} ${daemon_cmdopts} ${TORRUS_CMDOPTS}"
      fi
    done
  done

  # RHEL based systems (RHEL, CentOS, Fedora) ignore the KXXtorrus script
  # unless the corresponding lock is present
  if test -d /var/lock/subsys; then
    touch /var/lock/subsys/torrus
  fi
}

stop_daemons () {
  tokill=""
  for d in ${daemons}; do
  
    eval trees=\"\$\{trees_${d}\}\"
    for t in ${trees}; do
    
      if test ${d} = collector; then
        eval instances=\"\$\{col_inst_${t}\}\"
        
        for i in ${instances}; do
          pidfile="${piddir}/${d}.${t}_${i}.pid"
          if test -r ${pidfile}; then
            tokill=${tokill}' t='${t}'_'${i}';d='${d}
            echo "stopping Torrus collector instance ${i} for tree ${t}"
            pid=`cat ${pidfile}`
            @KILL@ ${pid} || \
              echo "Error: Cannot kill collector instance ${i} for tree ${t}"
          fi
        done
        
      else
            
        pidfile="${piddir}/${d}.${t}.pid"
        if test -r ${pidfile}; then
          tokill=${tokill}' t='${t}';d='${d}
          echo "stopping Torrus ${d} for tree ${t}"
          pid=`cat ${pidfile}`
          @KILL@ ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
        fi
        
      fi
    done
  done

  killed=`echo $tokill | wc -w`
  notdead=1
  kc=$TORRUS_KILL_COUNT
  while test $killed -gt 0 -a $kc -gt 0; do
    echo "Sleeping for $TORRUS_KILL_SLEEP seconds to allow processes to exit"
    @SLEEP@ $TORRUS_KILL_SLEEP
    echo "Checking for kill resistant processes [$kc/$TORRUS_KILL_COUNT]"
    kc=`expr $kc - 1`
    for tuple in $tokill; do
      eval $tuple
      pidfile="${piddir}/${d}.${t}.pid"
      if test -r ${pidfile}; then
        echo "  Sending kill signal to Torrus ${d} for tree ${t}"
        pid=`cat ${pidfile}`
        @KILL@ ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
      else
        echo "${d} for ${t} has stopped"
        killed=`expr $killed - 1`
      fi
    done
  done

  if test \( $killed -gt 0 \); then
    echo "  Killing Remaining Processes"
   for tuple in $tokill; do
     eval $tuple
     pidfile="${piddir}/${d}.${t}.pid"
     if test -r ${pidfile}; then
       echo "  Sending final kill -9 to Torrus ${d} for tree ${t}"
       pid=`cat ${pidfile}`
       @KILL@ -9 ${pid} || echo "Error: Cannot kill ${d} for tree ${t}"
       @RM@ -f $pidfile
     else
       echo "${d} for ${t} has stopped"
     fi
   done
  fi

  # RHEL specifics
  if test x"$all_daemons" = xyes -a -d /var/lock/subsys; then
    rm -f /var/lock/subsys/torrus
  fi
}

case "$1" in
  'start')    start_daemons
  ;;

  'stop')     stop_daemons
  ;;
  'restart')  stop_daemons; start_daemons
  ;;

  *)  echo "Usage: $0 [start|stop|restart] [collector|monitor] [tree]"
  ;;
  esac

# Local Variables:
# mode: shell-script
# sh-shell: sh
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End: