Blame SOURCES/bz1509319-storage-mon-new-ra.patch

3ff689
From 90b595650d7d8a6f6a69a9f7060c6406aa731c18 Mon Sep 17 00:00:00 2001
3ff689
From: "Fabio M. Di Nitto" <fdinitto@redhat.com>
3ff689
Date: Wed, 28 Jul 2021 10:08:10 +0200
3ff689
Subject: [PATCH] Add storage-mon pacemaker health check
3ff689
3ff689
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
3ff689
---
3ff689
 .gitignore               |  41 ++++++
3ff689
 configure.ac             |   1 +
3ff689
 doc/man/Makefile.am      |   3 +-
3ff689
 heartbeat/Makefile.am    |  17 +--
3ff689
 heartbeat/storage-mon.in | 263 +++++++++++++++++++++++++++++++++++++++
3ff689
 tools/Makefile.am        |   5 +-
3ff689
 tools/storage_mon.c      | 263 +++++++++++++++++++++++++++++++++++++++
3ff689
 7 files changed, 583 insertions(+), 10 deletions(-)
3ff689
 create mode 100644 heartbeat/storage-mon.in
3ff689
 create mode 100644 tools/storage_mon.c
3ff689
3ff689
diff --git a/.gitignore b/.gitignore
3ff689
index 38d3566205..f7277bf04e 100644
3ff689
--- a/.gitignore
3ff689
+++ b/.gitignore
3ff689
@@ -45,6 +45,46 @@ heartbeat/ocf-shellfuncs
3ff689
 heartbeat/send_ua
3ff689
 heartbeat/shellfuncs
3ff689
 heartbeat/*.pyc
3ff689
+heartbeat/AoEtarget
3ff689
+heartbeat/CTDB
3ff689
+heartbeat/ManageRAID
3ff689
+heartbeat/ManageVE
3ff689
+heartbeat/Squid
3ff689
+heartbeat/SysInfo
3ff689
+heartbeat/aws-vpc-route53
3ff689
+heartbeat/azure-events
3ff689
+heartbeat/clvm
3ff689
+heartbeat/conntrackd
3ff689
+heartbeat/dnsupdate
3ff689
+heartbeat/dummypy
3ff689
+heartbeat/eDir88
3ff689
+heartbeat/fio
3ff689
+heartbeat/galera
3ff689
+heartbeat/gcp-pd-move
3ff689
+heartbeat/gcp-vpc-move-ip
3ff689
+heartbeat/gcp-vpc-move-route
3ff689
+heartbeat/gcp-vpc-move-vip
3ff689
+heartbeat/iSCSILogicalUnit
3ff689
+heartbeat/iSCSITarget
3ff689
+heartbeat/jira
3ff689
+heartbeat/kamailio
3ff689
+heartbeat/lxc
3ff689
+heartbeat/lxd-info
3ff689
+heartbeat/machine-info
3ff689
+heartbeat/mariadb
3ff689
+heartbeat/mpathpersist
3ff689
+heartbeat/nfsnotify
3ff689
+heartbeat/openstack-info
3ff689
+heartbeat/rabbitmq-cluster
3ff689
+heartbeat/redis
3ff689
+heartbeat/rsyslog
3ff689
+heartbeat/sg_persist
3ff689
+heartbeat/slapd
3ff689
+heartbeat/smb-share
3ff689
+heartbeat/storage-mon
3ff689
+heartbeat/sybaseASE
3ff689
+heartbeat/syslog-ng
3ff689
+heartbeat/vsftpd
3ff689
 include/agent_config.h
3ff689
 include/config.h
3ff689
 include/config.h.in
3ff689
@@ -61,6 +101,7 @@ systemd/resource-agents.conf
3ff689
 tools/findif
3ff689
 tools/ocf-tester
3ff689
 tools/send_arp
3ff689
+tools/storage_mon
3ff689
 tools/tickle_tcp
3ff689
 tools/ocft/README
3ff689
 tools/ocft/README.zh_CN
3ff689
diff --git a/configure.ac b/configure.ac
3ff689
index 717fb95432..c125df98f6 100644
3ff689
--- a/configure.ac
3ff689
+++ b/configure.ac
3ff689
@@ -1002,6 +1002,7 @@ AC_CONFIG_FILES([heartbeat/rsyslog], [chmod +x heartbeat/rsyslog])
3ff689
 AC_CONFIG_FILES([heartbeat/smb-share], [chmod +x heartbeat/smb-share])
3ff689
 AC_CONFIG_FILES([heartbeat/sg_persist], [chmod +x heartbeat/sg_persist])
3ff689
 AC_CONFIG_FILES([heartbeat/slapd], [chmod +x heartbeat/slapd])
3ff689
+AC_CONFIG_FILES([heartbeat/storage-mon], [chmod +x heartbeat/storage-mon])
3ff689
 AC_CONFIG_FILES([heartbeat/sybaseASE], [chmod +x heartbeat/sybaseASE])
3ff689
 AC_CONFIG_FILES([heartbeat/syslog-ng], [chmod +x heartbeat/syslog-ng])
3ff689
 AC_CONFIG_FILES([heartbeat/vsftpd], [chmod +x heartbeat/vsftpd])
3ff689
diff --git a/doc/man/Makefile.am b/doc/man/Makefile.am
3ff689
index 947d83cb2b..97904ccb16 100644
3ff689
--- a/doc/man/Makefile.am
3ff689
+++ b/doc/man/Makefile.am
3ff689
@@ -138,6 +138,7 @@ man_MANS                = ocf_heartbeat_AoEtarget.7 \
3ff689
                           ocf_heartbeat_mariadb.7 \
3ff689
                           ocf_heartbeat_mdraid.7 \
3ff689
                           ocf_heartbeat_minio.7 \
3ff689
+                          ocf_heartbeat_mpathpersist.7 \
3ff689
                           ocf_heartbeat_mysql.7 \
3ff689
                           ocf_heartbeat_mysql-proxy.7 \
3ff689
                           ocf_heartbeat_nagios.7 \
3ff689
@@ -175,7 +176,7 @@ man_MANS                = ocf_heartbeat_AoEtarget.7 \
3ff689
                           ocf_heartbeat_smb-share.7 \
3ff689
                           ocf_heartbeat_sybaseASE.7 \
3ff689
                           ocf_heartbeat_sg_persist.7 \
3ff689
-                          ocf_heartbeat_mpathpersist.7 \
3ff689
+                          ocf_heartbeat_storage-mon.7 \
3ff689
                           ocf_heartbeat_symlink.7 \
3ff689
                           ocf_heartbeat_syslog-ng.7 \
3ff689
                           ocf_heartbeat_tomcat.7 \
3ff689
diff --git a/heartbeat/Makefile.am b/heartbeat/Makefile.am
3ff689
index 9af44cc127..5d52d211f2 100644
3ff689
--- a/heartbeat/Makefile.am
3ff689
+++ b/heartbeat/Makefile.am
3ff689
@@ -32,22 +32,22 @@ ocfdir		        = $(OCF_RA_DIR_PREFIX)/heartbeat
3ff689
 dtddir			= $(datadir)/$(PACKAGE_NAME)
3ff689
 dtd_DATA		= ra-api-1.dtd metadata.rng
3ff689
 
3ff689
+ocf_PROGRAMS		=
3ff689
+
3ff689
 if USE_IPV6ADDR_AGENT
3ff689
-ocf_PROGRAMS           = IPv6addr
3ff689
-else
3ff689
-ocf_PROGRAMS           =
3ff689
+ocf_PROGRAMS		+= IPv6addr
3ff689
 endif
3ff689
 
3ff689
+halib_PROGRAMS		=
3ff689
+
3ff689
 if IPV6ADDR_COMPATIBLE
3ff689
-halib_PROGRAMS         = send_ua
3ff689
-else
3ff689
-halib_PROGRAMS         =
3ff689
+halib_PROGRAMS		+= send_ua
3ff689
 endif
3ff689
 
3ff689
 IPv6addr_SOURCES        = IPv6addr.c IPv6addr_utils.c
3ff689
-send_ua_SOURCES         = send_ua.c IPv6addr_utils.c
3ff689
-
3ff689
 IPv6addr_LDADD          = -lplumb $(LIBNETLIBS)
3ff689
+
3ff689
+send_ua_SOURCES         = send_ua.c IPv6addr_utils.c
3ff689
 send_ua_LDADD           = $(LIBNETLIBS)
3ff689
3ff689
 osp_SCRIPTS	     =  nova-compute-wait	\
3ff689
@@ -170,6 +170,7 @@ ocf_SCRIPTS	      = AoEtarget		\
3ff689
 			mpathpersist		\
3ff689
 			slapd			\
3ff689
+			storage-mon		\
3ff689
 			sybaseASE		\
3ff689
 			symlink			\
3ff689
 			syslog-ng		\
3ff689
 			tomcat			\
3ff689
diff --git a/heartbeat/storage-mon.in b/heartbeat/storage-mon.in
3ff689
new file mode 100644
3ff689
index 0000000000..5b289fe554
3ff689
--- /dev/null
3ff689
+++ b/heartbeat/storage-mon.in
3ff689
@@ -0,0 +1,263 @@
3ff689
+#!@BASH_SHELL@
3ff689
+#
3ff689
+# Copyright (C) 2021 Red Hat, Inc.  All rights reserved.
3ff689
+#
3ff689
+# Authors: Christine Caulfield <ccaulfie@redhat.com>
3ff689
+#          Fabio M. Di Nitto <fdinitto@redhat.com>
3ff689
+#
3ff689
+# This program is free software; you can redistribute it and/or modify
3ff689
+# it under the terms of version 2 of the GNU General Public License as
3ff689
+# published by the Free Software Foundation.
3ff689
+#
3ff689
+# This program is distributed in the hope that it would be useful, but
3ff689
+# WITHOUT ANY WARRANTY; without even the implied warranty of
3ff689
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
3ff689
+#
3ff689
+# Further, this software is distributed without any warranty that it is
3ff689
+# free of the rightful claim of any third person regarding infringement
3ff689
+# or the like.  Any license provided herein, whether implied or
3ff689
+# otherwise, applies only to this software file.  Patent licenses, if
3ff689
+# any, provided herein do not apply to combinations of this program with
3ff689
+# other software, or any other product whatsoever.
3ff689
+#
3ff689
+# You should have received a copy of the GNU General Public License
3ff689
+# along with this program; if not, write the Free Software Foundation,
3ff689
+# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
3ff689
+#
3ff689
+
3ff689
+#
3ff689
+# Checks storage I/O status of all given drives and writes the #health-storage
3ff689
+# status into the CIB
3ff689
+# Implementation is heavily based on ocf:pacemaker:HealtSMART
3ff689
+#
3ff689
+# It sends a single block on IO to a radom location on the device and reports any errors returned.
3ff689
+# If the IO hangs, that will also be returned. (bear in mind tha tmay also hang the C app in some
3ff689
+# instances).
3ff689
+#
3ff689
+# It's worth making a note in the RA description that the smartmon RA is also recommended (this
3ff689
+# does not replace it), and that Pacemaker health checking should be configued.
3ff689
+#
3ff689
+# https://clusterlabs.org/pacemaker/doc/2.1/Pacemaker_Explained/singlehtml/index.html#tracking-node-health
3ff689
+
3ff689
+#######################################################################
3ff689
+
3ff689
+#######################################################################
3ff689
+# Initialization:
3ff689
+
3ff689
+: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
3ff689
+. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
3ff689
+
3ff689
+#
3ff689
+STORAGEMON=$HA_BIN/storage_mon
3ff689
+ATTRDUP=/usr/sbin/attrd_updater
3ff689
+
3ff689
+OCF_RESKEY_CRM_meta_interval_default="0"
3ff689
+OCF_RESKEY_io_timeout_default="10"
3ff689
+OCF_RESKEY_inject_errors_default=""
3ff689
+OCF_RESKEY_state_file_default="${HA_RSCTMP%%/}/storage-mon-${OCF_RESOURCE_INSTANCE}.state"
3ff689
+
3ff689
+# Explicitly list all environment variables used, to make static analysis happy
3ff689
+: ${OCF_RESKEY_CRM_meta_interval:=${OCF_RESKEY_CRM_meta_interval_default}}
3ff689
+: ${OCF_RESKEY_drives:=""}
3ff689
+: ${OCF_RESKEY_io_timeout:=${OCF_RESKEY_io_timeout_default}}
3ff689
+: ${OCF_RESKEY_inject_errors:=${OCF_RESKEY_inject_errors_default}}
3ff689
+: ${OCF_RESKEY_state_file:=${OCF_RESKEY_state_file_default}}
3ff689
+
3ff689
+#######################################################################
3ff689
+
3ff689
+meta_data() {
3ff689
+	cat <
3ff689
+
3ff689
+
3ff689
+<resource-agent name="storage-mon">
3ff689
+<version>1.0</version>
3ff689
+
3ff689
+<longdesc lang="en">
3ff689
+System health agent that checks the storage I/O status of the given drives and
3ff689
+updates the #health-storage attribute. Usage is highly recommended in combination
3ff689
+with storage-mon monitoring agent. The agent currently support a maximum of 25
3ff689
+devices per instance.
3ff689
+</longdesc>
3ff689
+<shortdesc lang="en">storage I/O health status</shortdesc>
3ff689
+
3ff689
+<parameters>
3ff689
+
3ff689
+<parameter name="state_file" unique="1">
3ff689
+<longdesc lang="en">
3ff689
+Location to store the resource state in.
3ff689
+</longdesc>
3ff689
+<shortdesc lang="en">State file</shortdesc>
3ff689
+<content type="string" default="${OCF_RESKEY_state_file_default}" />
3ff689
+</parameter>
3ff689
+
3ff689
+<parameter name="drives" unique="1" required="1">
3ff689
+<longdesc lang="en">
3ff689
+The drive(s) to check as a SPACE separated list. Enter the full path to the device, e.g. "/dev/sda".
3ff689
+</longdesc>
3ff689
+<shortdesc lang="en">Drives to check</shortdesc>
3ff689
+<content type="string" default="" />
3ff689
+</parameter>
3ff689
+
3ff689
+<parameter name="io_timeout" unique="0">
3ff689
+<longdesc lang="en">
3ff689
+Specify disk I/O timeout in seconds. Minimum 1, recommeded 10 (default).
3ff689
+</longdesc>
3ff689
+<shortdesc lang="en">Disk I/O timeout</shortdesc>
3ff689
+<content type="integer" default="${OCF_RESKEY_io_timeout_default}" />
3ff689
+</parameter>
3ff689
+
3ff689
+<parameter name="inject_errors" unique="0">
3ff689
+<longdesc lang="en">
3ff689
+Used only for testing! Specify % of I/O errors to simulate drives failures.
3ff689
+</longdesc>
3ff689
+<shortdesc lang="en">Specify % of I/O errors to simulate drives failures</shortdesc>
3ff689
+<content type="integer" default="${OCF_RESKEY_inject_errors_default}" />
3ff689
+</parameter>
3ff689
+
3ff689
+</parameters>
3ff689
+
3ff689
+<actions>
3ff689
+<action name="start"        timeout="10s" />
3ff689
+<action name="stop"         timeout="120s" />
3ff689
+<action name="monitor"      timeout="120s" interval="30s" start-delay="0s" />
3ff689
+<action name="meta-data"    timeout="5s" />
3ff689
+<action name="validate-all" timeout="10s" />
3ff689
+</actions>
3ff689
+</resource-agent>
3ff689
+END
3ff689
+	return $OCF_SUCCESS
3ff689
+}
3ff689
+
3ff689
+#######################################################################
3ff689
+
3ff689
+storage-mon_usage() {
3ff689
+	cat <
3ff689
+usage: $0 {start|stop|monitor|validate-all|meta-data}
3ff689
+
3ff689
+Expects to have a fully populated OCF RA-compliant environment set.
3ff689
+END
3ff689
+	return $1
3ff689
+}
3ff689
+
3ff689
+storage-mon_init() {
3ff689
+	#Test for presence of storage_mon helper
3ff689
+	if [ ! -x "$STORAGEMON" ] ; then
3ff689
+		ocf_log err "${STORAGEMON} not installed."
3ff689
+		exit $OCF_ERR_INSTALLED
3ff689
+	fi
3ff689
+
3ff689
+	i=0
3ff689
+	for DRIVE in ${OCF_RESKEY_drives}; do
3ff689
+		if [ ! -e "$DRIVE" ] ; then
3ff689
+			ocf_log err "${DRIVE} not found on the system"
3ff689
+			exit $OCF_ERR_INSTALLED
3ff689
+		fi
3ff689
+		i=$((i + 1))
3ff689
+	done
3ff689
+
3ff689
+	if [ "$i" -gt "25" ]; then
3ff689
+		ocf_log err "Too many drives ($i) configured for this agent. Max 25."
3ff689
+		exit $OCF_ERR_CONFIGURED
3ff689
+	fi
3ff689
+
3ff689
+	if [ "${OCF_RESKEY_io_timeout}" -lt "1" ]; then
3ff689
+		ocf_log err "Minimum timeout is 1. Recommended 10 (default)."
3ff689
+		exit $OCF_ERR_CONFIGURED
3ff689
+	fi
3ff689
+
3ff689
+	if [ -n "${OCF_RESKEY_inject_errors}" ]; then
3ff689
+		if [ "${OCF_RESKEY_inject_errors}" -lt "1" ] || [ "${OCF_RESKEY_inject_errors}" -gt "100" ]; then
3ff689
+			ocf_log err "Inject errors % has to be a value between 1 and 100."
3ff689
+			exit $OCF_ERR_CONFIGURED
3ff689
+		fi
3ff689
+	fi
3ff689
+}
3ff689
+
3ff689
+storage-mon_validate() {
3ff689
+	storage-mon_init
3ff689
+
3ff689
+	# Is the state directory writable?
3ff689
+	state_dir=$(dirname "$OCF_RESKEY_state_file")
3ff689
+	touch "$state_dir/$$"
3ff689
+	if [ $? -ne 0 ]; then
3ff689
+		return $OCF_ERR_CONFIGURED
3ff689
+	fi
3ff689
+	rm "$state_dir/$$"
3ff689
+
3ff689
+	return $OCF_SUCCESS
3ff689
+}
3ff689
+
3ff689
+storage-mon_monitor() {
3ff689
+	storage-mon_init
3ff689
+
3ff689
+	# Monitor _MUST!_ differentiate correctly between running
3ff689
+	# (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
3ff689
+	# That is THREE states, not just yes/no.
3ff689
+
3ff689
+	if [ ! -f "${OCF_RESKEY_state_file}" ]; then
3ff689
+		return $OCF_NOT_RUNNING
3ff689
+	fi
3ff689
+
3ff689
+	# generate command line
3ff689
+	cmdline=""
3ff689
+	for DRIVE in ${OCF_RESKEY_drives}; do
3ff689
+		cmdline="$cmdline --device $DRIVE --score 1"
3ff689
+	done
3ff689
+	cmdline="$cmdline --timeout ${OCF_RESKEY_io_timeout}"
3ff689
+	if [ -n "${OCF_RESKEY_inject_errors}" ]; then
3ff689
+		cmdline="$cmdline --inject-errors-percent ${OCF_RESKEY_inject_errors}"
3ff689
+	fi
3ff689
+	$STORAGEMON $cmdline
3ff689
+	if [ $? -ne 0 ]; then
3ff689
+		status="red"
3ff689
+	else
3ff689
+		status="green"
3ff689
+	fi
3ff689
+
3ff689
+	"$ATTRDUP" -n "#health-${OCF_RESOURCE_INSTANCE}" -U "$status" -d "5s"
3ff689
+	return $OCF_SUCCESS
3ff689
+}
3ff689
+
3ff689
+storage-mon_start() {
3ff689
+	storage-mon_monitor
3ff689
+	if [ $? -eq $OCF_SUCCESS ]; then
3ff689
+		return $OCF_SUCCESS
3ff689
+	fi
3ff689
+	touch "${OCF_RESKEY_state_file}"
3ff689
+}
3ff689
+
3ff689
+storage-mon_stop() {
3ff689
+	storage-mon_monitor
3ff689
+	if [ $? -eq $OCF_SUCCESS ]; then
3ff689
+		rm "${OCF_RESKEY_state_file}"
3ff689
+	fi
3ff689
+	return $OCF_SUCCESS
3ff689
+}
3ff689
+
3ff689
+storage-mon_validate() {
3ff689
+	storage-mon_init
3ff689
+
3ff689
+	# Is the state directory writable?
3ff689
+	state_dir=$(dirname "${OCF_RESKEY_state_file}")
3ff689
+	touch "$state_dir/$$"
3ff689
+	if [ $? -ne 0 ]; then
3ff689
+		return $OCF_ERR_CONFIGURED
3ff689
+	fi
3ff689
+	rm "$state_dir/$$"
3ff689
+
3ff689
+	return $OCF_SUCCESS
3ff689
+}
3ff689
+
3ff689
+case "$__OCF_ACTION" in
3ff689
+	start)		storage-mon_start;;
3ff689
+	stop)		storage-mon_stop;;
3ff689
+	monitor)	storage-mon_monitor;;
3ff689
+	validate-all)	storage-mon_validate;;
3ff689
+	meta-data)	meta_data;;
3ff689
+	usage|help)	storage-mon_usage $OCF_SUCCESS;;
3ff689
+	*)		storage-mon_usage $OCF_ERR_UNIMPLEMENTED;;
3ff689
+esac
3ff689
+rc=$?
3ff689
+ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
3ff689
+exit $rc
3ff689
+# vim: set filetype=sh:
3ff689
diff --git a/tools/Makefile.am b/tools/Makefile.am
3ff689
index 1186967cfb..83ff43651d 100644
3ff689
--- a/tools/Makefile.am
3ff689
+++ b/tools/Makefile.am
3ff689
@@ -29,7 +29,8 @@ EXTRA_DIST		= ocf-tester.8 sfex_init.8
3ff689
 
3ff689
 sbin_PROGRAMS		= 
3ff689
 sbin_SCRIPTS		= ocf-tester
3ff689
-halib_PROGRAMS		= findif
3ff689
+halib_PROGRAMS		= findif \
3ff689
+			  storage_mon
3ff689
 
3ff689
 man8_MANS		= ocf-tester.8
3ff689
 
3ff689
@@ -67,6 +68,8 @@ sfex_stat_LDADD		= $(GLIBLIB) -lplumb -lplumbgpl
3ff689
 
3ff689
 findif_SOURCES		= findif.c
3ff689
 
3ff689
+storage_mon_SOURCES	= storage_mon.c
3ff689
+
3ff689
 if BUILD_TICKLE
3ff689
 halib_PROGRAMS		+= tickle_tcp
3ff689
 tickle_tcp_SOURCES	= tickle_tcp.c
3ff689
diff --git a/tools/storage_mon.c b/tools/storage_mon.c
3ff689
new file mode 100644
3ff689
index 0000000000..7b65bb4191
3ff689
--- /dev/null
3ff689
+++ b/tools/storage_mon.c
3ff689
@@ -0,0 +1,263 @@
3ff689
+#include <stdio.h>
3ff689
+#include <getopt.h>
3ff689
+#include <stdlib.h>
3ff689
+#include <stdint.h>
3ff689
+#include <syslog.h>
3ff689
+#include <unistd.h>
3ff689
+#include <errno.h>
3ff689
+#include <string.h>
3ff689
+#include <fcntl.h>
3ff689
+#include <time.h>
3ff689
+#include <sys/types.h>
3ff689
+#include <sys/wait.h>
3ff689
+#include <sys/stat.h>
3ff689
+#include <sys/ioctl.h>
3ff689
+#include <sys/mount.h>
3ff689
+#ifdef __FreeBSD__
3ff689
+#include <sys/disk.h>
3ff689
+#endif
3ff689
+
3ff689
+#define MAX_DEVICES 25
3ff689
+#define DEFAULT_TIMEOUT 10
3ff689
+
3ff689
+static void usage(char *name, FILE *f)
3ff689
+{
3ff689
+	fprintf(f, "usage: %s [-hv] [-d <device>]... [-s <score>]... [-t <secs>]\n", name);
3ff689
+	fprintf(f, "      --device <dev>  device to test, up to %d instances\n", MAX_DEVICES);
3ff689
+	fprintf(f, "      --score  <n>    score if device fails the test. Must match --device count\n");
3ff689
+	fprintf(f, "      --timeout <n>   max time to wait for a device test to come back. in seconds (default %d)\n", DEFAULT_TIMEOUT);
3ff689
+	fprintf(f, "      --inject-errors-percent <n> Generate EIO errors <n>%% of the time (for testing only)\n");
3ff689
+	fprintf(f, "      --verbose        emit extra output to stdout\n");
3ff689
+	fprintf(f, "      --help           print this messages\n");
3ff689
+}
3ff689
+
3ff689
+/* Check one device */
3ff689
+static void *test_device(const char *device, int verbose, int inject_error_percent)
3ff689
+{
3ff689
+	uint64_t devsize;
3ff689
+	int device_fd;
3ff689
+	int res;
3ff689
+	off_t seek_spot;
3ff689
+	char buffer[512];
3ff689
+
3ff689
+	if (verbose) {
3ff689
+		printf("Testing device %s\n", device);
3ff689
+	}
3ff689
+
3ff689
+	device_fd = open(device, O_RDONLY);
3ff689
+	if (device_fd < 0) {
3ff689
+		fprintf(stderr, "Failed to open %s: %s\n", device, strerror(errno));
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+#ifdef __FreeBSD__
3ff689
+	res = ioctl(device_fd, DIOCGMEDIASIZE, &devsize);
3ff689
+#else
3ff689
+	res = ioctl(device_fd, BLKGETSIZE64, &devsize);
3ff689
+#endif
3ff689
+	if (res != 0) {
3ff689
+		fprintf(stderr, "Failed to stat %s: %s\n", device, strerror(errno));
3ff689
+		close(device_fd);
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+	if (verbose) {
3ff689
+		fprintf(stderr, "%s: size=%zu\n", device, devsize);
3ff689
+	}
3ff689
+	/* Don't fret about real randomness */
3ff689
+	srand(time(NULL) + getpid());
3ff689
+	/* Pick a random place on the device - sector aligned */
3ff689
+	seek_spot = (rand() % (devsize-1024)) & 0xFFFFFFFFFFFFFE00;
3ff689
+	res = lseek(device_fd, seek_spot, SEEK_SET);
3ff689
+	if (res < 0) {
3ff689
+		fprintf(stderr, "Failed to seek %s: %s\n", device, strerror(errno));
3ff689
+		close(device_fd);
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+
3ff689
+	if (verbose) {
3ff689
+		printf("%s: reading from pos %ld\n", device, seek_spot);
3ff689
+	}
3ff689
+
3ff689
+	res = read(device_fd, buffer, sizeof(buffer));
3ff689
+	if (res < 0) {
3ff689
+		fprintf(stderr, "Failed to read %s: %s\n", device, strerror(errno));
3ff689
+		close(device_fd);
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+	if (res < (int)sizeof(buffer)) {
3ff689
+		fprintf(stderr, "Failed to read %ld bytes from %s, got %d\n", sizeof(buffer), device, res);
3ff689
+		close(device_fd);
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+
3ff689
+	/* Fake an error */
3ff689
+	if (inject_error_percent && ((rand() % 100) < inject_error_percent)) {
3ff689
+		fprintf(stderr, "People, please fasten your seatbelts, injecting errors!\n");
3ff689
+		close(device_fd);
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+	res = close(device_fd);
3ff689
+	if (res != 0) {
3ff689
+		fprintf(stderr, "Failed to close %s: %s\n", device, strerror(errno));
3ff689
+		close(device_fd);
3ff689
+		exit(-1);
3ff689
+	}
3ff689
+
3ff689
+	if (verbose) {
3ff689
+		printf("%s: done\n", device);
3ff689
+	}
3ff689
+	exit(0);
3ff689
+}
3ff689
+
3ff689
+int main(int argc, char *argv[])
3ff689
+{
3ff689
+	char *devices[MAX_DEVICES];
3ff689
+	int scores[MAX_DEVICES];
3ff689
+	pid_t test_forks[MAX_DEVICES];
3ff689
+	size_t device_count = 0;
3ff689
+	size_t score_count = 0;
3ff689
+	size_t finished_count = 0;
3ff689
+	int timeout = DEFAULT_TIMEOUT;
3ff689
+	struct timespec ts;
3ff689
+	time_t start_time;
3ff689
+	size_t i;
3ff689
+	int final_score = 0;
3ff689
+	int opt, option_index;
3ff689
+	int verbose = 0;
3ff689
+	int inject_error_percent = 0;
3ff689
+	struct option long_options[] = {
3ff689
+		{"timeout", required_argument, 0, 't' },
3ff689
+		{"device",  required_argument, 0, 'd' },
3ff689
+		{"score",   required_argument, 0, 's' },
3ff689
+		{"inject-errors-percent",   required_argument, 0, 0 },
3ff689
+		{"verbose", no_argument, 0, 'v' },
3ff689
+		{"help",    no_argument, 0,       'h' },
3ff689
+		{0,         0,           0,        0  }
3ff689
+	};
3ff689
+	while ( (opt = getopt_long(argc, argv, "hvt:d:s:",
3ff689
+				   long_options, &option_index)) != -1 ) {
3ff689
+		switch (opt) {
3ff689
+			case 0: /* Long-only options */
3ff689
+				if (strcmp(long_options[option_index].name, "inject-errors-percent") == 0) {
3ff689
+					inject_error_percent = atoi(optarg);
3ff689
+					if (inject_error_percent < 1 || inject_error_percent > 100) {
3ff689
+						fprintf(stderr, "inject_error_percent should be between 1 and 100\n");
3ff689
+						return -1;
3ff689
+					}
3ff689
+				}
3ff689
+				break;
3ff689
+			case 'd':
3ff689
+				if (device_count < MAX_DEVICES) {
3ff689
+					devices[device_count++] = strdup(optarg);
3ff689
+				} else {
3ff689
+					fprintf(stderr, "too many devices, max is %d\n", MAX_DEVICES);
3ff689
+					return -1;
3ff689
+				}
3ff689
+				break;
3ff689
+			case 's':
3ff689
+				if (device_count < MAX_DEVICES) {
3ff689
+					int score = atoi(optarg);
3ff689
+					if (score < 1 || score > 10) {
3ff689
+						fprintf(stderr, "Score must be between 1 and 10 inclusive\n");
3ff689
+						return -1;
3ff689
+					}
3ff689
+					scores[score_count++] = score;
3ff689
+				} else {
3ff689
+					fprintf(stderr, "too many scores, max is %d\n", MAX_DEVICES);
3ff689
+					return -1;
3ff689
+				}
3ff689
+				break;
3ff689
+			case 'v':
3ff689
+				verbose++;
3ff689
+				break;
3ff689
+			case 't':
3ff689
+				timeout = atoi(optarg);
3ff689
+				if (timeout < 1) {
3ff689
+					fprintf(stderr, "invalid timeout %d. Min 1, recommended %d (default)\n", timeout, DEFAULT_TIMEOUT);
3ff689
+					return -1;
3ff689
+				}
3ff689
+				break;
3ff689
+			case 'h':
3ff689
+				usage(argv[0], stdout);
3ff689
+				break;
3ff689
+			default:
3ff689
+				usage(argv[0], stderr);
3ff689
+				break;
3ff689
+		}
3ff689
+
3ff689
+	}
3ff689
+	if (device_count == 0) {
3ff689
+		fprintf(stderr, "No devices to test, use the -d  or --device argument\n");
3ff689
+		return -1;
3ff689
+	}
3ff689
+
3ff689
+	if (device_count != score_count) {
3ff689
+		fprintf(stderr, "There must be the same number of devices and scores\n");
3ff689
+		return -1;
3ff689
+	}
3ff689
+
3ff689
+	openlog("storage_mon", 0, LOG_DAEMON);
3ff689
+
3ff689
+	memset(test_forks, 0, sizeof(test_forks));
3ff689
+	for (i=0; i
3ff689
+		test_forks[i] = fork();
3ff689
+		if (test_forks[i] < 0) {
3ff689
+			fprintf(stderr, "Error spawning fork for %s: %s\n", devices[i], strerror(errno));
3ff689
+			syslog(LOG_ERR, "Error spawning fork for %s: %s\n", devices[i], strerror(errno));
3ff689
+			/* Just test the devices we have */
3ff689
+			break;
3ff689
+		}
3ff689
+		/* child */
3ff689
+		if (test_forks[i] == 0) {
3ff689
+			test_device(devices[i], verbose, inject_error_percent);
3ff689
+		}
3ff689
+	}
3ff689
+
3ff689
+	/* See if they have finished */
3ff689
+	clock_gettime(CLOCK_REALTIME, &ts);
3ff689
+	start_time = ts.tv_sec;
3ff689
+
3ff689
+	while ((finished_count < device_count) && ((start_time + timeout) > ts.tv_sec)) {
3ff689
+		for (i=0; i
3ff689
+			int wstatus;
3ff689
+			pid_t w;
3ff689
+
3ff689
+			if (test_forks[i] > 0) {
3ff689
+				w = waitpid(test_forks[i], &wstatus, WUNTRACED | WNOHANG | WCONTINUED);
3ff689
+				if (w < 0) {
3ff689
+					fprintf(stderr, "waitpid on %s failed: %s\n", devices[i], strerror(errno));
3ff689
+					return -1;
3ff689
+				}
3ff689
+
3ff689
+				if (w == test_forks[i]) {
3ff689
+					if (WIFEXITED(wstatus)) {
3ff689
+						if (WEXITSTATUS(wstatus) == 0) {
3ff689
+							finished_count++;
3ff689
+							test_forks[i] = 0;
3ff689
+						} else {
3ff689
+							syslog(LOG_ERR, "Error reading from device %s", devices[i]);
3ff689
+							final_score += scores[i];
3ff689
+						}
3ff689
+					}
3ff689
+				}
3ff689
+			}
3ff689
+		}
3ff689
+
3ff689
+		usleep(100000);
3ff689
+
3ff689
+		clock_gettime(CLOCK_REALTIME, &ts);
3ff689
+	}
3ff689
+
3ff689
+	/* See which threads have not finished */
3ff689
+	for (i=0; i
3ff689
+		if (test_forks[i] != 0) {
3ff689
+			syslog(LOG_ERR, "Reading from device %s did not complete in %d seconds timeout", devices[i], timeout);
3ff689
+			fprintf(stderr, "Thread for device %s did not complete in time\n", devices[i]);
3ff689
+			final_score += scores[i];
3ff689
+		}
3ff689
+	}
3ff689
+
3ff689
+	if (verbose) {
3ff689
+		printf("Final score is %d\n", final_score);
3ff689
+	}
3ff689
+	return final_score;
3ff689
+}