This file is indexed.

/etc/init.d/shinken is in shinken-core 0.6.5-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/bin/sh

### BEGIN INIT INFO
# Provides:          shinken
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Shinken monitoring daemon
# Description:       Shinken is a monitoring tool composed of many separated modules:
#     - arbiter     : the main one : control everything else.
#     - scheduler   : receives checks/actions from arbiter. Schedules & forwards them to pollers.
#     - poller      : receives the checks from a scheduler. Launch them and returns results
#     - broker      : manage results by looking at scheduler. Like export to flat file or db.
#     - reactionner : manage the failed checks by looking at scheduler. 
#     - receiver    : manage all passive data
### END INIT INFO


# http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html


NAME="shinken"

AVAIL_MODULES="scheduler poller reactionner broker receiver arbiter"

## SHINKEN_MODULE_FILE is set by shinken-* if it's one of these that's calling us.
if [ -z "$SHINKEN_MODULE_FILE" ]; then
    SCRIPTNAME=$0
    _usage_mods_="[ <$AVAIL_MODULES> ]"
else
    SCRIPTNAME=$SHINKEN_MODULE_FILE
fi

curpath=$(cd $(dirname "$0") && pwd)
#echo curpath is $curpath filename is $(basename "$0")

## Default paths :
test "$BIN" || BIN=$(cd $curpath/.. && pwd)
test "$VAR" || VAR=$(cd $curpath/../../var && pwd)
test "$ETC" || ETC=$(cd $curpath/../../etc && pwd)

export PATH="${PATH:+$PATH:}/usr/sbin:/bin:/sbin"
export LANG=en_US.UTF8

# Uncomment the line below if you got the **lib** shinken installed
# on a non standard place (not in /usr/lib/python*)
#export PYTHONPATH="${PATH:+$PATH:}/opt/shinken"

# default
DEBUG=false
CMD=""
SUBMODULES=""

## This permits to overhidde the default "default shinken cfg file" : 
[ -z "$SHINKEN_DEFAULT_FILE" ] && SHINKEN_DEFAULT_FILE="/etc/default/$NAME" 
## so you can now do:
## bash -c "SHINKEN_DEFAULT_FILE=$your_own_default_file $init_path/shinken $action $args"
## to easily use your own config

#echo "Using $SHINKEN_DEFAULT_FILE .."

usage() {
    cat << END
Usage: $SCRIPTNAME [ -d ] {start|stop|restart|status|check} $_usage_mods_

 -d  start requested module(s) in debug mode, only useful with start|restart

END
}

if [ "$1" = "-d" ]; then
    DEBUG="1"
    shift
fi

if [ $# -eq 0 ]; then
    usage >&2
    exit 2
fi

CMD=$1
shift
SUBMODULES=$*

if [ -z "$SUBMODULES" ]; then
    SUBMODULES=$AVAIL_MODULES
else
    # check given modules
    for mod1 in $SUBMODULES; do
        found=0
        for mod2 in $AVAIL_MODULES; do
            [ $mod1 = $mod2 ] && found=1;
        done
        [ $found = 0 ] && { usage >&2 ; exit 2 ; }
    done
fi


# Reads configuration variable file if it is present
[ -r "$SHINKEN_DEFAULT_FILE" ] && . "$SHINKEN_DEFAULT_FILE"


# Now look if some required variables are pre defined :
if ! test "$SHINKENCFG"
then
    SHINKENCFG="$ETC/nagios.cfg"
fi
if ! test "$SHINKENSPECIFICCFG"
then
    SHINKENSPECIFICCFG="$ETC/shinken-specific.cfg"
fi


# Now place us in our var directory so even our arbiter will be
# happy for opening its pid and cmd files
cd $VAR

#echo BIN=$BIN
#echo VAR=$VAR
#echo ETC=$ETC

#set -xv

# 
echo_success() {
   log_end_msg 0 $*
}

echo_failure() {
    log_end_msg 1 $*
}

#log_end_msg


# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Source function library.
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions



################################################

#
# returns the pid for a submodule
#

getpidfile() {
    mod="$1"
    modPIDVAR=$(echo $mod | tr 'a-z' 'A-Z')"PID"
    pidfile=$(echo $(eval echo \${$modPIDVAR}))
    if test "$pidfile"
    then
        echo "$pidfile"
    else
        echo "$VAR/${mod}d.pid"
    fi
}

getmodpid() {
    mod=$1
    pidfile=$(getpidfile "$mod")
    if [ -s $pidfile ]; then
        cat $pidfile
    fi
}


getdebugfile() {
    mod="$1"
    modDEBUG=$(echo $mod | tr 'a-z' 'A-Z')"DEBUGFILE"
    debugfile=$(echo $(eval echo \${$modDEBUG}))
    if test "$debugfile"
    then
        echo "$debugfile"
    else
        echo "${VAR}/${mod}-debug.log"
    fi
}

#
# Display status
#
do_status() {
    mod=$1
    pidfile=$(getpidfile "$mod")
    [ -e "$pidfile" ] || {
        echo "$mod NOT RUNNING (pidfile ($pidfile) not exist)"
        return 3
    }
    [ -r "$pidfile" ] || {
        echo "$mod NOT RUNNING (pidfile ($pidfile) unreadable)"
        return 3
    }
    pid=$(cat "$pidfile")
    if [ -z "$pid" ]; then
        echo "$mod NOT RUNNING (pid file empty)"
        return 4
    fi
    ps -p "$pid" >/dev/null 2>&1
    rc=$?
    if [ $rc != 0 ]; then
        log_failure_msg  "$mod NOT RUNNING (process $pid doesn't exist ?)"
        return 1
    fi
    echo "$mod RUNNING (pid $pid)"
    return 0
}

#
# starts our modules
#
do_start() {
    mod=$1
    modfilepath="$BIN/shinken-${mod}"
    [ -e "$modfilepath" ] || {
        log_failure_msg "FAILED: did not found $mod file ($modfilepath) ; are you sure shinken-$mod is installed ?"
        return 5
    }
    [ "$DEBUG" = 1 ] && DEBUGCMD="--debug "$(getdebugfile "$mod")
    if [ "$mod" != "arbiter" ]; then
        output=$("$modfilepath" -d -c "$ETC/${mod}d.ini" $DEBUGCMD 2>&1)
        rc=$?
    else
        output=$("$modfilepath" -d -c "$SHINKENCFG" -c "$SHINKENSPECIFICCFG" $DEBUGCMD 2>&1)
        rc=$?
    fi
    # debug:
    #resfile="/tmp/bad_start_for_$mod"
    #echo "$output" > "$resfile" || true
    if [ $rc != 0 ]; then
        resfile="/tmp/bad_start_for_$mod"
        echo "$output" > "$resfile" || true
        output=$(echo "$output" | tail -1)
        echo "FAILED: $output (full output is in $resfile)"
        return 1
    fi
    echo "OK"
    return 0
}

#
# stops modules
#
do_stop() {
    mod=$1
    pid=$(getmodpid "$mod")
    statusoutput=$(do_status "$mod")
    [ $? -ne 0 ] && {
        echo "$statusoutput"
        return 0
    }
    if [ ! -z "$pid" ]; then
        kill "$pid"
        sleep 0.5
        ## TODO: instead of 'sleep 1' : wait up to when pid file is removed (with timeout) ?
        for i in 1 2 3
        do
            # TODO : use a better way to get the children pids..
            allpids="$(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}')"
            if [ -z "$allpids" ]; then
                echo "OK"
                return 0
            fi
            sleep 1
        done
        echo "there are still remaining processes to $mod running.. ; trying to kill them (SIGTERM).."
        allpids="$(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}')"
        for cpid in $(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}'); do
            kill $cpid > /dev/null 2>&1
        done
        for i in 1 2 3
        do
            # TODO : eventually use a better way to get the children pids..
            allpids="$(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}')"
            if [ -z "$allpids" ]; then
                echo "OK"
            	return 0
            fi
            sleep 1
        done
        echo "there are still remaining processes to $mod running.. ; trying to kill -9 them.."
        allpids="$(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}')"
        for cpid in $(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}'); do
            kill -9 $cpid > /dev/null 2>&1
        done
        sleep 1
        allpids="$(ps -aef | grep "$pid" | grep "shinken-$mod" | awk '{print $2}')"
        if [ ! -z "$allpids" ]; then
            echo "FAILED: one or more process for $mod are still running after kill -9 !"
            echo "Remaining processes are (pids="$allpids"):"
            ps -lf $(for p in $allpids ; do echo -n "-p$p " ; done)
            echo "You should check this." 
            return 1
        fi
        echo "OK"
    else
        echo "NOT RUNNING"
    fi
    return 0
}

#
# does the config check
#
do_check() {
    [ "$DEBUG" = 1 ] && DEBUGCMD="--debug $VAR/${mod}-debug.log"
    "$BIN/shinken-arbiter" -v -c "$ETC/nagios.cfg" -c "$ETC/shinken-specific.cfg" $DEBUGCMD 2>&1
    return $?
}


############################

do_start_() {
    echo  "Starting $1: "
    status=$(do_status "$1")
    rc=$?
    if [ $rc -eq 0 ]; then
        log_warning_msg "Already running"
        return
    fi
    if test "$1" = "arbiter"
    then
        # arbiter is special : 
        # it doesn't actually declare a "workdir" properties in its config
        # so we have explicitely to cd to the "VAR" directory.
        # so that the default pidfile ( == nagios lock_file) which is now "arbiterd.pid"
        # will be created at the correct place.  
        cd "$VAR"
        # TODO: check if other possibility wouldn't be better:
        # declare a "workdir" properties for the arbiter module definition.. in shinken-specific.cfg.
        # but if the lock_file path is absolute then this 'cd' isn't required.
    fi
    startoutput=$(do_start "$1")
    rc=$?
    if [ $rc -eq 0 ]; then
        echo_success  
    else
        echo "$startoutput" 
        echo_failure
    fi
    return $rc
}

do_stop_() {
    echo  "Stopping $1"
    statusoutput=$(do_status "$1")
    rc=$?
    if [ $rc -ne 0 ]; then
        failuremsg="Couldn't get status of $1 : Assuming already stopped"
        log_success_msg "$failuremsg"
        rc=0
    else
        stopoutput=$(do_stop "$1" 2>&1)
        rc=$?
        [ $rc -ne 0 ] && failuremsg="Couldn't stop $1 : $stopoutput"
    fi
    if [ $rc -ne 0 ]; then
        log_failure_msg "$failuremsg"
        echo_failure
    else
        echo_success
    fi
    return $rc
}

do_restart_() {
    mod="$1"
    echo "Restarting $mod"
    if [ "$mod" = "arbiter" ]; then
        do_check_ "$mod"
        checkrc=$?
        if [ $checkrc -ne 0 ]; then
           return 1
        fi
    fi 
    stopoutput=$(do_stop "$mod")
    startoutput=$(do_start "$mod")
    rc=$?
    if [ $rc -eq 0 ]; then
        echo_success
    else
        log_failure_msg "$startoutput"
        echo_failure
    fi
    return $rc
}

do_status_() {
    mod=$1
    echo  "Checking status of $mod"
    do_status "$1"
    rc=$?
    if [ $rc -eq 0 ]; then
        echo_success
    else
        echo_failure
    fi

}

do_check_() {
    echo "Doing config check"
    output=$(do_check "$1" 2>&1)
    rc=$?
    if [ $rc -eq 0 ]; then
        echo_success
    else
        check_res_file="/tmp/shinken_checkconfig_result"
        echo "$output" > "$check_res_file"
        output=$(echo "$output" | tail -1)
        log_warning_msg "full result is in ${check_res_file}"
        log_failure_msg "ConfigCheck failed: $output"
        echo_failure
    fi
    return $rc
}
do_checkconfig_() { do_check_ "$1" ; }


############################

do_cmd_on() {
    action=$1
    mods=$2
    for mod in $mods
    do
        do_${action}_ "$mod"
    done
}


############################
## Main:

case "$CMD" in
    start|stop|restart|force-reload|status)
        do_cmd_on "$CMD" "$SUBMODULES"
        ;;
    check|checkconfig)
        do_cmd_on "$CMD" "arbiter"
        ;;
    *)
        usage >&2
        exit 2
        ;;
esac