File: tomcat

package info (click to toggle)
cluster-agents 1%3A1.0.3-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,828 kB
  • sloc: sh: 19,423; ansic: 3,174; perl: 3,135; makefile: 367
file content (465 lines) | stat: -rwxr-xr-x 13,606 bytes parent folder | download | duplicates (2)
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
454
455
456
457
458
459
460
461
462
463
464
465
#!/bin/sh
#
# Description:  Manages a Tomcat Server as an OCF High-Availability
#               resource under Heartbeat/LinuxHA control
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
# 02110-1301, USA.
#
# Copyright (c) 2007 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
#
#######################################################################
# OCF parameters:
#   OCF_RESKEY_tomcat_name - The name of the resource. Default is tomcat
#   OCF_RESKEY_script_log  - A destination of the log of this script. Default /var/log/OCF_RESKEY_tomcat_name.log
#   OCF_RESKEY_tomcat_stop_timeout  - Time-out at the time of the stop. Default is 5
#   OCF_RESKEY_tomcat_suspend_trialcount  - The re-try number of times awaiting a stop. Default is 10
#   OCF_RESKEY_tomcat_user  - A user name to start a resource. Default is root
#   OCF_RESKEY_statusurl - URL for state confirmation. Default is http://127.0.0.1:8080
#   OCF_RESKEY_java_home - Home directory of the Java. Default is None
#   OCF_RESKEY_catalina_home - Home directory of Tomcat. Default is None
#   OCF_RESKEY_catalina_pid  - A PID file name of Tomcat. Default is OCF_RESKEY_catalina_home/logs/catalina.pid
#   OCF_RESKEY_tomcat_start_opts - Start options of the tomcat. Default is None.
#   OCF_RESKEY_catalina_opts - CATALINA_OPTS environment variable. Default is None.
#   OCF_RESKEY_catalina_rotate_log - Control catalina.out logrotation flag. Default is NO.
#   OCF_RESKEY_catalina_rotatetime - catalina.out logrotation time span(seconds). Default is 86400.
###############################################################################


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

############################################################################
# Usage
usage() 
{
	cat <<-!
usage: $0 action

action:
        start   start tomcat

        stop    stop the tomcat

        status  return the status of tomcat, run or down

        monitor  return TRUE if the tomcat appears to be working.
                 You have to have installed $WGETNAME for this to work.

        meta-data       show meta data message

        validate-all    validate the instance parameters
!
}

############################################################################
# Check tomcat service availability
isrunning_tomcat()
{
	if ! have_binary $WGET; then
		ocf_log err "Monitoring not supported by $OCF_RESOURCE_INSTANCE"
		ocf_log info "Please make sure that wget is available"
		return $OCF_ERR_CONFIGURED
	fi
	$WGET -O /dev/null $RESOURCE_STATUSURL >/dev/null 2>&1
}

############################################################################
# 
isalive_tomcat()
{
	pgrep -f "${SEARCH_STR}" > /dev/null 
}
############################################################################
# Check tomcat process and service availability
monitor_tomcat()
{
	isalive_tomcat ||
		return $OCF_NOT_RUNNING
	isrunning_tomcat ||
		return $OCF_NOT_RUNNING
	return $OCF_SUCCESS
}

############################################################################
# Execute catalina.out log rotation
rotate_catalina_out()
{
	# Look for rotatelogs/rotatelogs2
	if [ -x /usr/sbin/rotatelogs ]; then
		ROTATELOGS=/usr/sbin/rotatelogs
	elif [ -x /usr/sbin/rotatelogs2 ]; then
		ROTATELOGS=/usr/sbin/rotatelogs2
	else
		ocf_log warn "rotatelogs command not found."
		return 1
	fi

	# Clean up and set permissions on required files
	rm -rf "$CATALINA_HOME"/temp/* "$CATALINA_HOME/logs/catalina.out"
	mkfifo -m700 "$CATALINA_HOME/logs/catalina.out"
	chown --dereference "$RESOURCE_TOMCAT_USER" "$CATALINA_HOME/logs/catalina.out" || true

	# -s is required because tomcat5.5's login shell is /bin/false
	su - -s /bin/sh $RESOURCE_TOMCAT_USER \
        	-c "$ROTATELOGS -l \"$CATALINA_HOME/logs/catalina_%F.log\" $CATALINA_ROTATETIME" \
        	< "$CATALINA_HOME/logs/catalina.out" > /dev/null 2>&1 &
}

############################################################################
# Start Tomcat
start_tomcat()
{
	cd "$CATALINA_HOME/bin"

	monitor_tomcat
	if [ $? = $OCF_SUCCESS ]; then
		return $OCF_SUCCESS
	fi

	#ocf_log debug "catalina.out rotation FLG = ${CATALINA_ROTATE_LOG}"
	if [ ${CATALINA_ROTATE_LOG} = "YES" ]; then
		rotate_catalina_out
		if [ $? = 0 ]; then
			ocf_log debug "Rotate catalina.out succeeded."
		else
			ocf_log warn "Rotate catalina.out failed. Starting tomcat without catalina.out rotation."
		fi
	fi
	
	echo "`date "+%Y/%m/%d %T"`: start ===========================" >> "$TOMCAT_CONSOLE"

	ocf_log debug "CATALINA_OPTS value = ${CATALINA_OPTS}"
	if [ "$RESOURCE_TOMCAT_USER" = RUNASIS ]; then
		"$CATALINA_HOME/bin/catalina.sh" start $TOMCAT_START_OPTS \
			>> "$TOMCAT_CONSOLE" 2>&1 &
	else
		su - -s /bin/sh "$RESOURCE_TOMCAT_USER" \
			-c "export JAVA_HOME=${OCF_RESKEY_java_home};\n
                            export JAVA_OPTS=-Dname=${TOMCAT_NAME};\n
                            export CATALINA_HOME=${OCF_RESKEY_catalina_home};\n
                            export CATALINA_PID=${OCF_RESKEY_catalina_pid};\n
                            export CATALINA_OPTS=\"${OCF_RESKEY_catalina_opts}\";\n
                            $CATALINA_HOME/bin/catalina.sh start ${OCF_RESKEY_tomcat_start_opts}" \
			>> "$TOMCAT_CONSOLE" 2>&1 &
	fi

	while true; do
		monitor_tomcat
		if [ $? = $OCF_SUCCESS ]; then
			break
		fi
		ocf_log debug "start_tomcat[$TOMCAT_NAME]: retry monitor_tomcat"
		sleep 3
	done

	return $OCF_SUCCESS
}

############################################################################
# Stop Tomcat
stop_tomcat()
{
	cd "$CATALINA_HOME/bin"

	echo "`date "+%Y/%m/%d %T"`: stop  ###########################" >> "$TOMCAT_CONSOLE"

	if [ "$RESOURCE_TOMCAT_USER" = RUNASIS ]; then
		"$CATALINA_HOME/bin/catalina.sh" stop \
			>> "$TOMCAT_CONSOLE" 2>&1 &
		eval $tomcat_stop_cmd >> "$TOMCAT_CONSOLE" 2>&1
	else
		su - -s /bin/sh "$RESOURCE_TOMCAT_USER" \
			-c "export JAVA_HOME=${OCF_RESKEY_java_home};\n
                            export JAVA_OPTS=-Dname=${TOMCAT_NAME};\n
                            export CATALINA_HOME=${OCF_RESKEY_catalina_home};\n
                            export CATALINA_PID=${OCF_RESKEY_catalina_pid};\n
                            $CATALINA_HOME/bin/catalina.sh stop" \
			>> "$TOMCAT_CONSOLE" 2>&1
	fi

	lapse_sec=0
	while isalive_tomcat; do
		sleep 1
		lapse_sec=`expr $lapse_sec + 1`
		ocf_log debug "stop_tomcat[$TOMCAT_NAME]: stop NORM $lapse_sec/$TOMCAT_STOP_TIMEOUT"
		if [ $lapse_sec -ge $TOMCAT_STOP_TIMEOUT ]; then
			break
		fi
	done

	if isalive_tomcat; then
		lapse_sec=0
		while true; do
			sleep 1
			lapse_sec=`expr $lapse_sec + 1`
			ocf_log debug "stop_tomcat[$TOMCAT_NAME]: suspend tomcat by SIGTERM ($lapse_sec/$TOMCAT_SUSPEND_TRIALCOUNT)"
			pkill -TERM -f "${SEARCH_STR}"
			if isalive_tomcat; then
				ocf_log debug "stop_tomcat[$TOMCAT_NAME]: suspend tomcat by SIGQUIT ($lapse_sec/$TOMCAT_SUSPEND_TRIALCOUNT)"
				pkill -QUIT -f "${SEARCH_STR}"
				if isalive_tomcat; then
					if [ $lapse_sec -ge $TOMCAT_SUSPEND_TRIALCOUNT ]; then
						break
					fi
				else
					break
				fi
			else
				break
			fi
		done
	fi

	lapse_sec=0
	while isalive_tomcat; do
		sleep 1
		lapse_sec=`expr $lapse_sec + 1`
		ocf_log debug "stop_tomcat[$TOMCAT_NAME]: suspend tomcat by SIGKILL ($lapse_sec)"
		pkill -KILL -f "${SEARCH_STR}"
	done

	if [ ${CATALINA_ROTATE_LOG} = "YES" ]; then
		rm -f "$CATALINA_PID" "${CATALINA_HOME}/logs/catalina.out"
	else
		rm -f "$CATALINA_PID"
	fi
	return $OCF_SUCCESS
}

status_tomcat()
{
	return $OCF_SUCCESS
}


metadata_tomcat()
{
    cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="tomcat">
<version>1.0</version>

<longdesc lang="en">
Resource script for tomcat. It manages a Tomcat instance as an HA resource.
</longdesc>
<shortdesc lang="en">Manages a Tomcat servlet environment instance</shortdesc>

<parameters>

<parameter name="tomcat_name" unique="1" >
<longdesc lang="en">
The name of the resource
</longdesc>
<shortdesc>The name of the resource</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="script_log" unique="1">
<longdesc lang="en">
A destination of the log of this script
</longdesc>
<shortdesc>A destination of the log of this script</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="tomcat_stop_timeout" unique="0">
<longdesc lang="en">
Time-out at the time of the stop
</longdesc>
<shortdesc>Time-out at the time of the stop</shortdesc>
<content type="integer" default="" />
</parameter>

<parameter name="tomcat_suspend_trialcount" unique="0">
<longdesc lang="en">
The re-try number of times awaiting a stop
</longdesc>
<shortdesc>The re-try number of times awaiting a stop</shortdesc>
<content type="integer" default="" />
</parameter>

<parameter name="tomcat_user" unique="0">
<longdesc lang="en">
A user name to start a resource
</longdesc>
<shortdesc>A user name to start a resource</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="statusurl" unique="0">
<longdesc lang="en">
URL for state confirmation
</longdesc>
<shortdesc>URL for state confirmation</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="java_home" unique="0" required="1">
<longdesc lang="en">
Home directory of the Java
</longdesc>
<shortdesc>Home directory of the Java</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="catalina_home" unique="1" required="1">
<longdesc lang="en">
Home directory of Tomcat
</longdesc>
<shortdesc>Home directory of Tomcat</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="catalina_pid" unique="1">
<longdesc lang="en">
A PID file name of Tomcat
</longdesc>
<shortdesc>A PID file name of Tomcat</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="tomcat_start_opts" unique="0">
<longdesc lang="en">
Tomcat start options
</longdesc>
<shortdesc>Tomcat start options</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="catalina_opts" unique="0">
<longdesc lang="en">
Catalina options
</longdesc>
<shortdesc>Catalina options</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="catalina_rotate_log" unique="0">
<longdesc lang="en">
Rotate catalina.out flag
</longdesc>
<shortdesc>Rotate catalina.out flag</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="catalina_rotatetime" unique="0">
<longdesc lang="en">
Time span of the rotate catalina.out
</longdesc>
<shortdesc>Time span of the rotate catalina.out</shortdesc>
<content type="integer" default="" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="60s" />
<action name="stop" timeout="120s" />
<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30s" interval="10s" />
<action name="meta-data" timeout="5s" />
<action name="validate-all"  timeout="5"/>
</actions>
</resource-agent>
END
	return $OCF_SUCCESS
}

validate_all_tomcat()
{
	ocf_log info "validate_all_tomcat[$TOMCAT_NAME]"
	return $OCF_SUCCESS
}

#
### tomcat RA environment variables
#

TOMCAT_NAME="${OCF_RESKEY_tomcat_name-tomcat}"
TOMCAT_CONSOLE="${OCF_RESKEY_script_log-/var/log/$TOMCAT_NAME.log}"
TOMCAT_STOP_TIMEOUT="${OCF_RESKEY_tomcat_stop_timeout-5}"
TOMCAT_SUSPEND_TRIALCOUNT="${OCF_RESKEY_tomcat_suspend_trialcount-10}"
RESOURCE_TOMCAT_USER="${OCF_RESKEY_tomcat_user-RUNASIS}"
RESOURCE_STATUSURL="${OCF_RESKEY_statusurl-http://127.0.0.1:8080}"

JAVA_HOME="${OCF_RESKEY_java_home}"
JAVA_OPTS="-Dname=$TOMCAT_NAME"
SEARCH_STR="\\""${JAVA_OPTS}"
CATALINA_HOME="${OCF_RESKEY_catalina_home}"
CATALINA_PID="${OCF_RESKEY_catalina_pid-$CATALINA_HOME/logs/catalina.pid}"

TOMCAT_START_OPTS="${OCF_RESKEY_tomcat_start_opts}"
CATALINA_OPTS="${OCF_RESKEY_catalina_opts}"
CATALINA_ROTATE_LOG="${OCF_RESKEY_catalina_rotate_log-NO}"
CATALINA_ROTATETIME="${OCF_RESKEY_catalina_rotatetime-86400}"

export JAVA_HOME JAVA_OPTS CATALINA_HOME CATALINA_PID CATALINA_OPTS

JAVA=${JAVA_HOME}/bin/java

#
# ------------------
# the main script
# ------------------
# 

COMMAND=$1

case "$COMMAND" in
	start)
		ocf_log debug  "[$TOMCAT_NAME] Enter tomcat start"
		start_tomcat
		func_status=$?
		ocf_log debug  "[$TOMCAT_NAME] Leave tomcat start $func_status"
		exit $func_status
		;;
	stop)
		ocf_log debug  "[$TOMCAT_NAME] Enter tomcat stop"
		stop_tomcat
		func_status=$?
		ocf_log debug  "[$TOMCAT_NAME] Leave tomcat stop $func_status"
		exit $func_status
		;;
	status)
		status_tomcat
		exit $?
		;;
	monitor)
		#ocf_log debug  "[$TOMCAT_NAME] Enter tomcat monitor"
		monitor_tomcat
		func_status=$?
		#ocf_log debug  "[$TOMCAT_NAME] Leave tomcat monitor $func_status"
		exit $func_status
		;;
	meta-data)
		metadata_tomcat
		exit $?
		;;
	validate-all)
		validate_all_tomcat
		exit $?
		;;
        usage|help)
                usage
                exit $OCF_SUCCESS
                ;;
	*)
		usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac