summaryrefslogtreecommitdiffstats
path: root/heartbeat/storage-mon.in
blob: 284dec30f2662898797509f503873988f74f68cc (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
#!@BASH_SHELL@
#
# Copyright (C) 2021 Red Hat, Inc.  All rights reserved.
#
# Authors: Christine Caulfield <ccaulfie@redhat.com>
#          Fabio M. Di Nitto <fdinitto@redhat.com>
#
# 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.
#

#
# Checks storage I/O status of all given drives and writes the #health-storage
# status into the CIB
# Implementation is heavily based on ocf:pacemaker:HealtSMART
#
# It sends a single block on IO to a radom location on the device and reports any errors returned.
# If the IO hangs, that will also be returned. (bear in mind tha tmay also hang the C app in some
# instances).
#
# It's worth making a note in the RA description that the smartmon RA is also recommended (this
# does not replace it), and that Pacemaker health checking should be configued.
#
# https://clusterlabs.org/pacemaker/doc/2.1/Pacemaker_Explained/singlehtml/index.html#tracking-node-health

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

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

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

#
STORAGEMON=${HA_BIN}/storage_mon
ATTRDUP=${HA_SBIN_DIR}/attrd_updater
PIDFILE=${HA_VARRUN}/storage-mon-${OCF_RESOURCE_INSTANCE}.pid
ATTRNAME="#health-${OCF_RESOURCE_INSTANCE}"

OCF_RESKEY_CRM_meta_interval_default="0"
OCF_RESKEY_io_timeout_default="10"
OCF_RESKEY_check_interval_default="30"
OCF_RESKEY_inject_errors_default=""
OCF_RESKEY_state_file_default="${HA_RSCTMP%%/}/storage-mon-${OCF_RESOURCE_INSTANCE}.state"
OCF_RESKEY_daemonize_default="false"

# Explicitly list all environment variables used, to make static analysis happy
: ${OCF_RESKEY_CRM_meta_interval:=${OCF_RESKEY_CRM_meta_interval_default}}
: ${OCF_RESKEY_drives:=""}
: ${OCF_RESKEY_io_timeout:=${OCF_RESKEY_io_timeout_default}}
: ${OCF_RESKEY_check_interval:=${OCF_RESKEY_check_interval_default}}
: ${OCF_RESKEY_inject_errors:=${OCF_RESKEY_inject_errors_default}}
: ${OCF_RESKEY_state_file:=${OCF_RESKEY_state_file_default}}
: ${OCF_RESKEY_daemonize:=${OCF_RESKEY_daemonize_default}}

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

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

<longdesc lang="en">
System health agent that checks the storage I/O status of the given drives and
updates the #health-storage attribute. Usage is highly recommended in combination
with the HealthSMART monitoring agent. The agent currently support a maximum of 25
devices per instance.
</longdesc>
<shortdesc lang="en">storage I/O health status</shortdesc>

<parameters>

<parameter name="state_file" unique="1">
<longdesc lang="en">
Location to store the resource state in.
</longdesc>
<shortdesc lang="en">State file</shortdesc>
<content type="string" default="${OCF_RESKEY_state_file_default}" />
</parameter>

<parameter name="drives" unique="1" required="1">
<longdesc lang="en">
The drive(s) to check as a SPACE separated list. Enter the full path to the device, e.g. "/dev/sda".
</longdesc>
<shortdesc lang="en">Drives to check</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="io_timeout" unique="0">
<longdesc lang="en">
Specify disk I/O timeout in seconds. Minimum 1, recommended 10 (default).
</longdesc>
<shortdesc lang="en">Disk I/O timeout</shortdesc>
<content type="integer" default="${OCF_RESKEY_io_timeout_default}" />
</parameter>

<parameter name="check_interval" unique="0">
<longdesc lang="en">
Specify interval between I/O checks in seconds.(Only supported with the damonize option.)
</longdesc>
<shortdesc lang="en">I/O check interval</shortdesc>
<content type="integer" default="${OCF_RESKEY_check_interval_default}" />
</parameter>

<parameter name="inject_errors" unique="0">
<longdesc lang="en">
Used only for testing! Specify % of I/O errors to simulate drives failures.
</longdesc>
<shortdesc lang="en">Specify % of I/O errors to simulate drives failures</shortdesc>
<content type="integer" default="${OCF_RESKEY_inject_errors_default}" />
</parameter>

<parameter name="daemonize" unique="0">
<longdesc lang="en">
Specifies to start storage-mon as a daemon and check for devices.
</longdesc>
<shortdesc lang="en">start storage-mon with daemon</shortdesc>
<content type="boolean" default="${OCF_RESKEY_daemonize_default}" />
</parameter>

</parameters>

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

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

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

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

storage-mon_init() {
	#Test for presence of storage_mon helper
	if [ ! -x "$STORAGEMON" ] ; then
		ocf_log err "${STORAGEMON} not installed."
		exit $OCF_ERR_INSTALLED
	fi

	if [ ! -x "$ATTRDUP" ] ; then
		ocf_log err "${ATTRDUP} not installed."
		exit $OCF_ERR_INSTALLED
	fi

	i=0
	for DRIVE in ${OCF_RESKEY_drives}; do
		if [ ! -e "$DRIVE" ] ; then
			ocf_log err "${DRIVE} not found on the system"
			exit $OCF_ERR_INSTALLED
		fi
		i=$((i + 1))
	done

	if [ "$i" -gt "25" ]; then
		ocf_log err "Too many drives ($i) configured for this agent. Max 25."
		exit $OCF_ERR_CONFIGURED
	fi

	if [ "${OCF_RESKEY_io_timeout}" -lt "1" ]; then
		ocf_log err "Minimum timeout is 1. Recommended ${OCF_RESKEY_io_timeout_default} (default)."
		exit $OCF_ERR_CONFIGURED
	fi

	if [ "${OCF_RESKEY_check_interval}" -lt "1" ]; then
		ocf_log err "Minimum interval to check is 1. default ${OCF_RESKEY_check_interval_default}."
		exit $OCF_ERR_CONFIGURED
	fi

	if [ -n "${OCF_RESKEY_inject_errors}" ]; then
		if [ "${OCF_RESKEY_inject_errors}" -lt "1" ] || [ "${OCF_RESKEY_inject_errors}" -gt "100" ]; then
			ocf_log err "Inject errors % has to be a value between 1 and 100."
			exit $OCF_ERR_CONFIGURED
		fi
	fi
}

storage-mon_update_attribute() {

	while :
	do
		"$ATTRDUP" -n ${ATTRNAME} -U "$1" -d "5s"
		rc=$?
		if [ $rc -eq 0 ]; then
			break
		fi

		ocf_log debug "${1} attribute by attrd_updater failed"
		if [ "$1" = "red" ]; then
			# If the attrd_updater fails with the red attribute, return an error to let pacemaker handle the failure immediately.
			return $OCF_ERR_GENERIC
		fi
	done
	return $OCF_SUCCESS
}

storage-mon_monitor() {
	if ! ocf_is_true "$OCF_RESKEY_daemonize"; then
		storage-mon_init

		# Monitor _MUST!_ differentiate correctly between running
		# (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
		# That is THREE states, not just yes/no.

		if [ ! -f "${OCF_RESKEY_state_file}" ]; then
			return $OCF_NOT_RUNNING
		fi

		# generate command line
		cmdline=""
		for DRIVE in ${OCF_RESKEY_drives}; do
			cmdline="$cmdline --device $DRIVE --score 1"
		done
		cmdline="$cmdline --timeout ${OCF_RESKEY_io_timeout}"
		if [ -n "${OCF_RESKEY_inject_errors}" ]; then
			cmdline="$cmdline --inject-errors-percent ${OCF_RESKEY_inject_errors}"
		fi
		$STORAGEMON $cmdline
		if [ $? -ne 0 ]; then
			status="red"
		else
			status="green"
		fi

		storage-mon_update_attribute $status
		return "$?"
	else
		ocf_pidfile_status "${PIDFILE}" > /dev/null 2>&1
		case "$?" in
			0)	rc=$OCF_SUCCESS;;
			1|2)	rc=$OCF_NOT_RUNNING;;
			*)	rc=$OCF_ERR_GENERIC;;
		esac

		if [ $rc -ne $OCF_SUCCESS ]; then
			return "$rc"
		fi
		if [ "$1" = "pid_check_only" ]; then
			return "$rc"
		fi

		# generate client command line
		cmdline=""
		cmdline="$cmdline --client --attrname ${ATTRNAME}"
		while :
		do
			# 0			: Normal.
			# greater than 0	: monitoring error.
			# 255(-1)		: communication system error.
			# 254(-2)		: Not all checks completed for first device in daemon mode.
			$STORAGEMON $cmdline
			rc=$?
			case "$rc" in
				254|255)
					# If there is a communication error or the initial check of all devices has not been completed, 
					# it will loop and try to reconnect.
					# When everything ends with a communication error during monitor, a monitor timeout occurs.
					ocf_log debug "client monitor error : $rc"
					;;
				0)
					status="green"
					break
					;;
				*)
					status="red"
					break
					;;
			esac
		done

		storage-mon_update_attribute $status
		return "$?"
	fi	
}

storage-mon_start() {
	if ! ocf_is_true "$OCF_RESKEY_daemonize"; then
		storage-mon_monitor
		if [ $? -eq $OCF_SUCCESS ]; then
			return $OCF_SUCCESS
		fi
		touch "${OCF_RESKEY_state_file}"
	else
		storage-mon_init
		# generate command line
		cmdline=""
		for DRIVE in ${OCF_RESKEY_drives}; do
			cmdline="$cmdline --device $DRIVE --score 1"
		done
		cmdline="$cmdline --daemonize --timeout ${OCF_RESKEY_io_timeout} --interval ${OCF_RESKEY_check_interval} --pidfile ${PIDFILE} --attrname ${ATTRNAME}"
		if [ -n "${OCF_RESKEY_inject_errors}" ]; then
			cmdline="$cmdline --inject-errors-percent ${OCF_RESKEY_inject_errors}"
		fi
		$STORAGEMON $cmdline
		if [ "$?" -ne 0 ]; then
			return $OCF_ERR_GENERIC
		fi
	fi
}

storage-mon_stop() {
	storage-mon_monitor
	rc=$?

	if ! ocf_is_true "$OCF_RESKEY_daemonize"; then
		if [ $rc -eq $OCF_SUCCESS ]; then
			rm "${OCF_RESKEY_state_file}"
		fi
	else
		case "$rc" in
			$OCF_SUCCESS)
				;;
			$OCF_NOT_RUNNING)
				return "$OCF_SUCCESS";;
			*)
				return "$rc";;
		esac

		kill -TERM $(cat "${PIDFILE}")
		if [ "$?" -ne 0 ]; then
			return $OCF_ERR_GENERIC
		fi

		while true; do
			storage-mon_monitor pid_check_only
			rc="$?"
			case "$rc" in
				$OCF_SUCCESS)
					;;
				$OCF_NOT_RUNNING)
					return "$OCF_SUCCESS";;
				*)
					return "$rc";;
			esac
			sleep 1
		done
	fi
	return $OCF_SUCCESS
}

storage-mon_validate() {
	storage-mon_init

	if ! ocf_is_true "$OCF_RESKEY_daemonize"; then
		# Is the state directory writable?
		state_dir=$(dirname "${OCF_RESKEY_state_file}")
		touch "$state_dir/$$"
		if [ $? -ne 0 ]; then
			return $OCF_ERR_CONFIGURED
		fi
		rm "$state_dir/$$"
	fi

	return $OCF_SUCCESS
}

case "$__OCF_ACTION" in
	start)		storage-mon_start;;
	stop)		storage-mon_stop;;
	monitor)	storage-mon_monitor;;
	validate-all)	storage-mon_validate;;
	meta-data)	meta_data;;
	usage|help)	storage-mon_usage $OCF_SUCCESS;;
	*)		storage-mon_usage $OCF_ERR_UNIMPLEMENTED;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc
# vim: set filetype=sh: