Statistics
| Branch: | Tag: | Revision:

one / src / im_mad / remotes / common.d / collectd-client_control.sh @ 5db34212

History | View | Annotate | Download (2.87 KB)

1
#!/bin/bash
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19

    
20
#--------------------------------------------------------------------------- #
21
# Process Arguments
22
#--------------------------------------------------------------------------- #
23

    
24
ACTION="start"
25

    
26
if [ "$1" = "stop" ]; then
27
    shift
28
    ACTION="stop"
29
fi
30

    
31
ARGV=$*
32

    
33
#--------------------------------------------------------------------------- #
34
#--------------------------------------------------------------------------- #
35

    
36
# Directory that contains this file
37
DIR=$(pwd)
38

    
39
# Basename
40
BASENAME=$(basename $0 _control.sh)
41

    
42
# Collectd client (Ruby)
43
CLIENT=$DIR/${BASENAME}.rb
44

    
45
# Collectd client PID
46
CLIENT_PID_FILE=/tmp/one-collectd-client.pid
47

    
48
# Launch the client
49
function start_client() {
50
    nohup /usr/bin/env ruby $CLIENT $ARGV >/dev/null 2>&1 &
51
}
52

    
53
function stop_client() {
54
    PID=$(get_pid)
55
    kill $PID
56
}
57

    
58
function remove_pid_file() {
59
    rm -f $CLIENT_PID_FILE
60
}
61

    
62
# Write the PID
63
function write_pid() {
64
    echo $1 > $CLIENT_PID_FILE
65
}
66

    
67
function get_pid() {
68
    cat $CLIENT_PID_FILE
69
}
70

    
71
# Check if running process
72
function check_running() {
73
    # Assume the process is not running if there is no pid file
74
    test ! -f $CLIENT_PID_FILE && return 1
75

    
76
    PID=$(get_pid)
77

    
78
    if ps --no-headers -o command $PID 2>/dev/null | grep -q $BASENAME; then
79
        return 0
80
    else
81
        # Stale PID file
82
        rm -f $CLIENT_PID_FILE
83
        return 1
84
    fi
85
}
86

    
87
case $ACTION in
88
start)
89
    if check_running; then
90
        stop_client
91
        remove_pid_file
92
    fi
93

    
94
    start_client
95
    write_pid $!
96

    
97
    # This script returns the run_probes execution
98
    HYPERVISOR=$1
99
    shift
100
    set $HYPERVISOR-probes $@
101

    
102
    $DIR/../run_probes $@
103

    
104
    ;;
105

    
106
stop)
107
    if check_running; then
108
        stop_client
109
        remove_pid_file
110
    fi
111
    ;;
112
esac