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

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