summaryrefslogtreecommitdiffstats
path: root/heartbeat/Squid.in
blob: e574ad0456d67a5eab8f19279d0f8fb8018b240d (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
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
466
467
468
469
470
471
472
#!@BASH_SHELL@
#
# Description:  Manages a Squid Server provided by NTT OSSC 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) 2008 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
#
#######################################################################
# OCF parameters:
#   OCF_RESKEY_squid_exe    : Executable file
#   OCF_RESKEY_squid_conf   : Configuration file
#   OCF_RESKEY_squid_opts   : Start options 
#   OCF_RESKEY_squid_pidfile: Process id file
#   OCF_RESKEY_squid_port   : Port number
#   OCF_RESKEY_debug_mode   : Debug mode
#   OCF_RESKEY_debug_log    : Debug log file
#   OCF_RESKEY_squid_stop_timeout:
#                             Number of seconds to await to confirm a
#                             normal stop method
#
#   OCF_RESKEY_squid_exe, OCF_RESKEY_squid_conf, OCF_RESKEY_squid_pidfile
#   and OCF_RESKEY_squid_port must be specified. Each of the rests
#   has its default value or refers OCF_RESKEY_squid_conf to make
#   its value when no explicit value is given.
###############################################################################

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

# Parameter defaults

OCF_RESKEY_squid_exe_default=""
OCF_RESKEY_squid_conf_default=""
OCF_RESKEY_squid_opts_default=""
OCF_RESKEY_squid_pidfile_default=""
OCF_RESKEY_squid_port_default=""
OCF_RESKEY_squid_stop_timeout_default="10"
OCF_RESKEY_debug_mode_default=""
OCF_RESKEY_debug_log_default=""

: ${OCF_RESKEY_squid_exe=${OCF_RESKEY_squid_exe_default}}
: ${OCF_RESKEY_squid_conf=${OCF_RESKEY_squid_conf_default}}
: ${OCF_RESKEY_squid_pidfile=${OCF_RESKEY_squid_pidfile_default}}
: ${OCF_RESKEY_squid_port=${OCF_RESKEY_squid_port_default}}
: ${OCF_RESKEY_squid_stop_timeout=${OCF_RESKEY_squid_stop_timeout_default}}
: ${OCF_RESKEY_debug_mode=${OCF_RESKEY_debug_mode_default}}
: ${OCF_RESKEY_debug_log=${OCF_RESKEY_debug_log_default}}

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

action:
        start       : start a new squid instance

        stop        : stop the running squid instance

        status      : return the status of squid, run or down

        monitor     : return TRUE if the squid appears to be working.

        meta-data   : show meta data message

        validate-all: validate the instance parameters
!
	return $OCF_ERR_ARGS
}

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

<longdesc lang="en">
The resource agent of Squid.
This manages a Squid instance as an HA resource.
</longdesc>
<shortdesc lang="en">Manages a Squid proxy server instance</shortdesc>

<parameters>

<parameter name="squid_exe" required="1" unique="0">
<longdesc lang="en">
This is a required parameter. This parameter specifies squid's
executable file.
</longdesc>
<shortdesc lang="en">Executable file</shortdesc>
<content type="string" default="${OCF_RESKEY_squid_exe_default}"/>
</parameter>

<parameter name="squid_conf" required="1" unique="1">
<longdesc lang="en">
This is a required parameter. This parameter specifies a configuration file
for a squid instance managed by this RA.
</longdesc>
<shortdesc lang="en">Configuration file</shortdesc>
<content type="string" default="${OCF_RESKEY_squid_conf_default}"/>
</parameter>

<parameter name="squid_opts" required="0" unique="0">
<longdesc lang="en">
This is a optional parameter. This parameter specifies the start options.
</longdesc>
<shortdesc lang="en">Start options</shortdesc>
<content type="string" default="${OCF_RESKEY_squid_opts_default}"/>
</parameter>

<parameter name="squid_pidfile" required="0" unique="1">
<longdesc lang="en">Deprecated - do not use anymore</longdesc>
<shortdesc lang="en">deprecated - do not use anymore</shortdesc>
<content type="string" default="${OCF_RESKEY_squid_pidfile_default}"/>
</parameter>

<parameter name="squid_port" required="1" unique="1">
<longdesc lang="en">
This is a required parameter. This parameter specifies a port number
for a squid instance managed by this RA. If multiple ports are used,
you must specify only one of them.
</longdesc>
<shortdesc lang="en">Port number</shortdesc>
<content type="integer" default="${OCF_RESKEY_squid_port_default}"/>
</parameter>

<parameter name="squid_stop_timeout" unique="0">
<longdesc lang="en">
On stop, a squid shutdown is invoked first. If the resource
doesn't stop within this timeout, we resort to stopping
processes by sending signals and finally KILLing them.
</longdesc>
<shortdesc lang="en">how long to wait for squid shutdown to stop the
instance before resorting to kill</shortdesc>
<content type="integer" default="${OCF_RESKEY_squid_stop_timeout_default}"/>
</parameter>

<parameter name="debug_mode" unique="0">
<longdesc lang="en">
This is an optional parameter.
This RA runs in debug mode when this parameter includes 'x' or 'v'.
If 'x' is included, both of STDOUT and STDERR redirect to the logfile
specified by "debug_log", and then the builtin shell option 'x' is turned on.
It is similar about 'v'.
</longdesc>
<shortdesc lang="en">Debug mode</shortdesc>
<content type="string" default="${OCF_RESKEY_debug_mode_default}"/>
</parameter>

<parameter name="debug_log" unique="0">
<longdesc lang="en">
This is an optional parameter.
This parameter specifies a destination file for debug logs
and works only if this RA run in debug mode.  Refer to "debug_mode"
about debug mode. If no value is given but is required, it's constructed
according to the following rules: "/var/log/" as a directory part,
the basename of the configuration file given by "syslog_ng_conf"
as a basename part, ".log" as a suffix.
</longdesc>
<shortdesc lang="en">A destination of the debug log</shortdesc>
<content type="string" default="${OCF_RESKEY_debug_log_default}"/>
</parameter>

</parameters>

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

	return $OCF_SUCCESS
}

get_pids()
{
	SQUID_PIDS=( )

	# Seek by pattern
	SQUID_PIDS[0]=$(pgrep -f "$PROCESS_PATTERN")

	# Seek by child process
	if [[ -n "${SQUID_PIDS[0]}" ]]; then
		SQUID_PIDS[1]=$(pgrep -P ${SQUID_PIDS[0]})
	fi

	if [[ -n "${SQUID_PIDS[1]}" ]]; then
		typeset exe
		exe=$(ls -l "/proc/${SQUID_PIDS[1]}/exe")
		if [[ $? = 0 ]]; then
			exe=${exe##*-> }
			if ! [[ "$exe" = $SQUID_EXE ]]; then
				SQUID_PIDS[1]=""
			fi
		else
			SQUID_PIDS[1]=""
		fi
	fi

	# Seek by port
	if have_binary netstat; then
		SQUID_PIDS[2]=$(
			netstat -apn |
			awk '/tcp.*:'$SQUID_PORT' .*LISTEN/ && $7~/^[1-9]/ {
				sub("\\/.*", "", $7); print $7; exit}')
	else
		SQUID_PIDS[2]=$(
			ss -apn |
			awk '/tcp.*LISTEN.*:'$SQUID_PORT'/ {
				sub(".*pid=", "", $7); sub(",fd=.*", "", $7); print $7 }')
	fi
}

are_all_pids_found()
{
	if 
		[[ -n "${SQUID_PIDS[0]}" ]] &&
		[[ -n "${SQUID_PIDS[1]}" ]] &&
		[[ -n "${SQUID_PIDS[2]}" ]]
	then
		return 0
	else
		return 1
	fi
}

are_pids_sane()
{
	if [[ "${SQUID_PIDS[1]}" = "${SQUID_PIDS[2]}" ]]; then
		return $OCF_SUCCESS
	else
		ocf_exit_reason "$SQUID_NAME:Pid unmatch"
		return $OCF_ERR_GENERIC
	fi
}

is_squid_dead()
{
	if 
		[[ -z "${SQUID_PIDS[0]}" ]] &&
		[[ -z "${SQUID_PIDS[2]}" ]]
	then
		return 0
	else
		return 1
	fi
}

monitor_squid()
{
	typeset trialcount=0

	while true; do
		get_pids

		if are_all_pids_found; then
			are_pids_sane
			return $OCF_SUCCESS
		fi

		if is_squid_dead; then
			return $OCF_NOT_RUNNING
		fi

		ocf_log info "$SQUID_NAME:Inconsistent processes:" \
			"${SQUID_PIDS[0]},${SQUID_PIDS[1]},${SQUID_PIDS[2]}"
		(( trialcount = trialcount + 1 ))
		if (( trialcount > SQUID_CONFIRM_TRIALCOUNT )); then
			ocf_exit_reason "$SQUID_NAME:Inconsistency of processes remains unsolved"
			return $OCF_ERR_GENERIC
		fi
		sleep 1
	done
}

start_squid()
{
	typeset status

	monitor_squid
	status=$?

	if [[ $status != $OCF_NOT_RUNNING ]]; then
		return $status
	fi

	set -- "$SQUID_OPTS"
	ocf_run $SQUID_EXE -f "$SQUID_CONF" "$@"
	status=$?
	if [[ $status != $OCF_SUCCESS ]]; then
		return $OCF_ERR_GENERIC
	fi

	while true; do
		get_pids
		if are_all_pids_found && are_pids_sane; then
			return $OCF_SUCCESS
		fi
		ocf_log info "$SQUID_NAME:Waiting for squid to be invoked"
		sleep 1
	done

	return $OCF_ERR_GENERIC
}

stop_squid()
{
	typeset lapse_sec

	if ocf_run $SQUID_EXE -f $SQUID_CONF -k shutdown; then
		lapse_sec=0
		while true; do
			get_pids
			if is_squid_dead; then
				return $OCF_SUCCESS
			fi
			(( lapse_sec = lapse_sec + 1 ))
			if (( lapse_sec > SQUID_STOP_TIMEOUT )); then
				break
			fi
			sleep 1
			ocf_log info "$SQUID_NAME:$FUNCNAME:$LINENO: " \
				"stop NORM $lapse_sec/$SQUID_STOP_TIMEOUT"
		done
	fi

	while true; do
		get_pids
		ocf_log info "$SQUID_NAME:$FUNCNAME:$LINENO: " \
			"try to stop by SIGKILL:${SQUID_PIDS[0]} ${SQUID_PIDS[2]}"
		kill -KILL ${SQUID_PIDS[0]} ${SQUID_PIDS[2]}
		sleep 1
		if is_squid_dead; then
			return $OCF_SUCCESS
		fi
	done

	return $OCF_ERR_GENERIC
}

status_squid()
{
	return $OCF_SUCCESS
}


validate_all_squid()
{
	ocf_log info "validate_all_squid[$SQUID_NAME]"
	return $OCF_SUCCESS
}

: "=== Debug ${0##*/} $1 ==="

if [[ "$1" = "meta-data" ]]; then
	metadata_squid
	exit $?
fi

SQUID_CONF="${OCF_RESKEY_squid_conf}"
if [[ -z "$SQUID_CONF" ]]; then
	ocf_exit_reason "SQUID_CONF is not defined"
	exit $OCF_ERR_CONFIGURED
fi

SQUID_NAME="${SQUID_CONF##*/}"
SQUID_NAME="${SQUID_NAME%.*}"

DEBUG_LOG="${OCF_RESKEY_debug_log-/var/log/squid_${SQUID_NAME}_debug}.log"

DEBUG_MODE=""
case $OCF_RESKEY_debug_mode in
	*x*) DEBUG_MODE="${DEBUG_MODE}x";;
esac
case $OCF_RESKEY_debug_mode in
	*v*) DEBUG_MODE="${DEBUG_MODE}v";;
esac

if [ -n "$DEBUG_MODE" ]; then
	PS4='\d \t \h '"${1-unknown} "
	export PS4
	exec 1>>$DEBUG_LOG 2>&1
	set -$DEBUG_MODE
fi

SQUID_EXE="${OCF_RESKEY_squid_exe}"
if [[ -z "$SQUID_EXE" ]]; then
	ocf_exit_reason "SQUID_EXE is not defined"
	exit $OCF_ERR_CONFIGURED
fi
if [[ ! -x "$SQUID_EXE" ]]; then
	ocf_exit_reason "$SQUID_EXE is not found"
	exit $OCF_ERR_CONFIGURED
fi

SQUID_PORT="${OCF_RESKEY_squid_port}"
if [[ -z "$SQUID_PORT" ]]; then
	ocf_exit_reason "SQUID_PORT is not defined"
	exit $OCF_ERR_CONFIGURED
fi

SQUID_OPTS="${OCF_RESKEY_squid_opts}"

SQUID_PIDS=( )

SQUID_CONFIRM_TRIALCOUNT="${OCF_RESKEY_squid_confirm_trialcount-3}"

SQUID_STOP_TIMEOUT="${OCF_RESKEY_squid_stop_timeout-10}"
SQUID_SUSPEND_TRIALCOUNT="${OCF_RESKEY_squid_suspend_trialcount-10}"

PROCESS_PATTERN="$SQUID_EXE -f $SQUID_CONF"

COMMAND=$1

case "$COMMAND" in
	start)
		ocf_log debug  "[$SQUID_NAME] Enter squid start"
		start_squid
		func_status=$?
		ocf_log debug  "[$SQUID_NAME] Leave squid start $func_status"
		exit $func_status
		;;
	stop)
		ocf_log debug  "[$SQUID_NAME] Enter squid stop"
		stop_squid
		func_status=$?
		ocf_log debug  "[$SQUID_NAME] Leave squid stop $func_status"
		exit $func_status
		;;
	status)
		status_squid
		exit $?
		;;
	monitor)
		#ocf_log debug  "[$SQUID_NAME] Enter squid monitor"
		monitor_squid
		func_status=$?
		#ocf_log debug  "[$SQUID_NAME] Leave squid monitor $func_status"
		exit $func_status
		;;
	validate-all)
		validate_all_squid
		exit $?
		;;
	*)
		usage
		;;
esac

# vim: set sw=4 ts=4 :