summaryrefslogtreecommitdiffstats
path: root/heartbeat/zabbixserver
blob: cb9e023a2d5c67e29b0d07b67edb2716506c16b3 (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
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
#!/bin/sh
#
#
#   zabbixserver OCF RA for zabbix_server daemon
#
# Copyright (c) 2012 Krzysztof Gajdemski <songo@debian.org.pl>
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

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

#
# Defaults
#
OCF_RESKEY_binary_default="zabbix_server"
OCF_RESKEY_pid_default="/var/run/zabbix-server/zabbix_server.pid"
OCF_RESKEY_config_default=""

: ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}
: ${OCF_RESKEY_pid=${OCF_RESKEY_pid_default}}
: ${OCF_RESKEY_config=${OCF_RESKEY_config_default}}

# sleep interval when waiting for threads cleanup
sleepint=1

#
# Functions
#
zabbixserver_meta_data() {
    cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="zabbixserver" version="0.0.1">
<version>1.0</version>

<longdesc lang="en">
This is a Zabbix server Resource Agent for zabbix_server monitoring
daemon. See: http://www.zabbix.com/
</longdesc>
<shortdesc lang="en">Zabbix server resource agent</shortdesc>

<parameters>

<parameter name="binary" unique="0" required="0">
<longdesc lang="en">
Location of the zabbix_server binary.
</longdesc>
<shortdesc lang="en">Zabbix server binary</shortdesc>
<content type="string" default="${OCF_RESKEY_binary_default}" />
</parameter>

<parameter name="pid" unique="1" required="0">
<longdesc lang="en">
Path to zabbix_server pidfile. As it's created by daemon itself
it must be the same as specified in the Zabbix configuration file
with parameter 'PidFile='.
</longdesc>
<shortdesc lang="en">Path to pidfile</shortdesc>
<content type="string" default="${OCF_RESKEY_pid_default}" />
</parameter>

<parameter name="config" unique="1" required="0">
<longdesc lang="en">
Path to zabbix_server configuration file. Assumed server default
if not specified.
</longdesc>
<shortdesc lang="en">Path to configuration file</shortdesc>
<content type="string" default="${OCF_RESKEY_config_default}" />
</parameter>

</parameters>

<actions>
<action name="start"        timeout="20s" />
<action name="stop"         timeout="20s" />
<action name="monitor"      timeout="20s" interval="10s" depth="0"/>
<action name="validate-all" timeout="20s" />
<action name="meta-data"    timeout="5s" />
</actions>
</resource-agent>
END
}

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


zabbixserver_usage() {
    cat <<END
usage: $0 {start|stop|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

#
# Get an actual PID from a given pidfile. If it can't
# be found then return 1
#
getpid() {
    # pidfile doesn't exists
    [ -f $1 ] || return 1
    sed -n '1 { /[0-9]/p }' $1

    return 0
}

#
# Check for the server configuration file
#
check_config() {
    # check only when it is specified by user
    if [ ! -z "$1" ] && [ ! -f "$1" ]; then
        if ocf_is_probe; then
            ocf_log info "Can't read configuration file $1 during probe"
        else
            ocf_exit_reason "Can't read configuration file $1"
            return 1
        fi
    fi

    return 0
}

#
# Start Zabbix daemon
#
startserver() {
    local command
    local params

    command=$OCF_RESKEY_binary

    # use additional parameters if specified
    if [ "$OCF_RESKEY_config" ]; then
        params="--config $OCF_RESKEY_config"
        command="$command $params"
    fi

    ocf_log debug "Starting server using command: $command"

    ocf_run $command
}

#
# Check the process status (PID is given as an argument)
#
process_status() {
    local pid

    pid=$1

    # check if parent process is running
    ocf_run -q kill -s 0 $pid 2> /dev/null 1>&2
}

#
# start the agent
#
zabbixserver_start() {
    local rc

    # check the resource status
    zabbixserver_monitor
    rc=$?
    case "$rc" in
    $OCF_SUCCESS)
        ocf_log info "Resource is already running"
        return $OCF_SUCCESS
        ;;
    $OCF_NOT_RUNNING)
        ;;
    *)
        exit $OCF_ERR_GENERIC
        ;;
    esac

    # remove stale pidfile if it exists
    if [ -f $OCF_RESKEY_pid ]; then
        ocf_log info "Removing stale pidfile"
        rm $OCF_RESKEY_pid
    fi

    startserver
    if [ $? -ne 0 ]; then
        ocf_exit_reason "Can't start Zabbix server"
        return $OCF_ERR_GENERIC
    fi

    # wait if it starts really
    while ! zabbixserver_monitor; do
        ocf_log debug "Resource has not started yet, waiting"
        sleep $sleepint
    done

    return $OCF_SUCCESS
}

#
# stop the agent
#
zabbixserver_stop() {
    local pid
    local rc

    # check the resource status
    zabbixserver_monitor
    rc=$?
    case "$rc" in
    $OCF_SUCCESS)
        ;;
    $OCF_NOT_RUNNING)
        ocf_log info "Resource is already stopped"
        return $OCF_SUCCESS
        ;;
    *)
        exit $OCF_ERR_GENERIC
        ;;
    esac

    pid=`getpid $OCF_RESKEY_pid`
    if [ $? -ne 0 ]; then
        ocf_exit_reason "Can't find process PID"
        return $OCF_ERR_GENERIC
    fi

    # kill the process
    ocf_run -q kill $pid
    if [ $? -ne 0 ]; then
        ocf_exit_reason "Can't stop process (PID $pid)"
        return $OCF_ERR_GENERIC
    fi

    # Wait until the parent process terminates.
    # NOTE: The parent may be still waiting for its children. A regular monitor
    # function will not detect this condition because the pidfile may be
    # removed just now.
    while process_status $pid; do
        ocf_log debug "Waiting for process to terminate..."
        sleep $sleepint
    done

    # wait if it stops really
    while zabbixserver_monitor; do
        ocf_log debug "Resource has not stopped yet, waiting"
        sleep $sleepint
    done

    # remove stale pidfile if it exists
    if [ -f $OCF_RESKEY_pid ]; then
        ocf_log debug "Pidfile still exists, removing"
        rm $OCF_RESKEY_pid
    fi

    return $OCF_SUCCESS
}

#
# resource monitor
#
zabbixserver_monitor() {
    local pid

    pid=`getpid $OCF_RESKEY_pid`
    if [ $? -eq 0 ]; then
        process_status $pid
        if [ $? -eq 0 ]; then
            ocf_log debug "Resource is running"
            return $OCF_SUCCESS
        fi
    fi

    ocf_log info "Resource is not running"
    return $OCF_NOT_RUNNING
}

#
# validate configuration
#
zabbixserver_validate_all() {
    check_config $OCF_RESKEY_config || return $OCF_ERR_INSTALLED
    ocf_mkstatedir root 755 `dirname $OCF_RESKEY_pid` || return $OCF_ERR_INSTALLED
    return $OCF_SUCCESS
}

#
# main
#
OCF_REQUIRED_PARAMS=""
OCF_REQUIRED_BINARIES="$OCF_RESKEY_binary"
ocf_rarun $*