Blame SOURCES/bz1476401-4-add-fence_heuristics_ping.patch

035a21
diff --git a/configure.ac b/configure.ac
035a21
index 8acfef9..f9f29cf 100644
035a21
--- a/configure.ac
035a21
+++ b/configure.ac
035a21
@@ -214,6 +214,27 @@ AC_PATH_PROG([SNMPGET_PATH], [snmpget], [/usr/bin/snmpget])
035a21
 AC_PATH_PROG([MPATH_PATH], [mpathpersist], [/usr/sbin/mpathpersist])
035a21
 AC_PATH_PROG([SUDO_PATH], [mpathpersist], [/usr/bin/sudo])
035a21
 
035a21
+AC_PATH_PROG([PING_CMD], [ping])
035a21
+AC_PATH_PROG([PING6_CMD], [ping6])
035a21
+AC_PATH_PROG([PING4_CMD], [ping4])
035a21
+
035a21
+if test "x${ac_cv_path_PING_CMD}" = x; then
035a21
+	# assume multicall-ping just not available in build-environment
035a21
+	PING_CMD="/bin/ping"
035a21
+	PING4_CMD="/bin/ping -4"
035a21
+	PING6_CMD="/bin/ping -6"
035a21
+elif test "x${ac_cv_path_PING6_CMD}" = x; then
035a21
+	# just IPv4
035a21
+	PING4_CMD="${ac_cv_path_PING_CMD}"
035a21
+elif test -L ${ac_cv_path_PING6_CMD}; then
035a21
+	# assume multicall-ping
035a21
+	PING4_CMD="${ac_cv_path_PING_CMD} -4"
035a21
+else
035a21
+	# ping is just IPv4
035a21
+	PING4_CMD="${ac_cv_path_PING_CMD}"
035a21
+fi
035a21
+
035a21
+
035a21
 ## do subst
035a21
 
035a21
 AC_SUBST([DEFAULT_CONFIG_DIR])
035a21
@@ -278,6 +278,7 @@
035a21
 		 fence/agents/eaton_snmp/Makefile
035a21
 		 fence/agents/emerson/Makefile
035a21
 		 fence/agents/eps/Makefile
035a21
+		 fence/agents/heuristics_ping/Makefile
035a21
 		 fence/agents/hpblade/Makefile
035a21
 		 fence/agents/ibmblade/Makefile
035a21
 		 fence/agents/ipdu/Makefile
035a21
--- /dev/null	2017-09-27 08:35:37.286500265 +0200
035a21
+++ b/fence/agents/heuristics_ping/Makefile.am	2017-09-28 15:27:42.605317632 +0200
035a21
@@ -0,0 +1,20 @@
035a21
+MAINTAINERCLEANFILES	= Makefile.in
035a21
+
035a21
+TARGET			= fence_heuristics_ping
035a21
+
035a21
+SRC			= $(TARGET).py
035a21
+
035a21
+EXTRA_DIST		= $(SRC)
035a21
+
035a21
+sbin_SCRIPTS		= $(TARGET)
035a21
+
035a21
+man_MANS		= $(TARGET).8
035a21
+
035a21
+FENCE_TEST_ARGS         = --ping-targets=localhost
035a21
+
035a21
+include $(top_srcdir)/make/fencebuild.mk
035a21
+include $(top_srcdir)/make/fenceman.mk
035a21
+include $(top_srcdir)/make/agentpycheck.mk
035a21
+
035a21
+clean-local: clean-man
035a21
+	rm -f $(TARGET)
035a21
diff --git a/doc/COPYRIGHT b/doc/COPYRIGHT
035a21
index 8124c53..49f88c6 100644
035a21
--- a/doc/COPYRIGHT
035a21
+++ b/doc/COPYRIGHT
035a21
@@ -58,6 +58,7 @@ Joel Becker <joel.becker at oracle.com>
035a21
 Jonathan Brassow <jbrassow at redhat.com>
035a21
 jparsons <jparsons at redhat.com>
035a21
 Ken Preslan <kpreslan at redhat.com>
035a21
+Klaus Wenninger <kwenning at redhat.com>
035a21
 Lon Hohberger <lhh at redhat.com>
035a21
 Marc - A. Dahlhaus <mad at wol.de>
035a21
 Marek 'marx' Grac <mgrac at redhat.com>
035a21
diff --git a/fence/agents/heuristics_ping/fence_heuristics_ping.py b/fence/agents/heuristics_ping/fence_heuristics_ping.py
035a21
new file mode 100644
035a21
index 0000000..b21d6a4
035a21
--- /dev/null
035a21
+++ b/fence/agents/heuristics_ping/fence_heuristics_ping.py
035a21
@@ -0,0 +1,200 @@
035a21
+#!/usr/bin/python -tt
035a21
+
035a21
+# The Following Agent Has Been Tested On:
035a21
+#
035a21
+# RHEL 7.4
035a21
+#
035a21
+
035a21
+import io
035a21
+import re
035a21
+import subprocess
035a21
+import shlex
035a21
+import sys, stat
035a21
+import logging
035a21
+import os
035a21
+import atexit
035a21
+sys.path.append("@FENCEAGENTSLIBDIR@")
035a21
+from fencing import fail_usage, run_command, fence_action, all_opt
035a21
+from fencing import atexit_handler, check_input, process_input, show_docs
035a21
+from fencing import run_delay
035a21
+
035a21
+def ping_test(con, options):
035a21
+	# Send pings to the targets
035a21
+
035a21
+	if options["--action"] == "on":
035a21
+		# we want unfencing to always succeed
035a21
+		return True
035a21
+
035a21
+	if not "--ping-targets" in options or options["--ping-targets"] == "":
035a21
+		# "off" was requested so fake "on" to provoke failure
035a21
+		logging.error("ping target required")
035a21
+		return False
035a21
+
035a21
+	timeout = int(options["--ping-timeout"])
035a21
+	count = int(options["--ping-count"])
035a21
+	interval = int(options["--ping-interval"])
035a21
+	good_required = int(options["--ping-good-count"])
035a21
+	maxfail = int(options["--ping-maxfail"])
035a21
+	targets = options["--ping-targets"].split(",")
035a21
+	exitcode = True
035a21
+	p = {}
035a21
+	failcount = 0
035a21
+	# search string for parsing the results of the ping-executable
035a21
+	packet_count = re.compile(r".*transmitted, ([0-9]*) received.*")
035a21
+
035a21
+	# start a ping-process per target
035a21
+	for target in targets:
035a21
+		ping_path = '@PING_CMD@'
035a21
+		target_mangled = target
035a21
+		if target.startswith('inet6:'):
035a21
+			if '@PING6_CMD@' == '':
035a21
+				p[target] = None
035a21
+				continue
035a21
+			ping_path = '@PING6_CMD@'
035a21
+			target_mangled = target.lstrip('inet6:')
035a21
+		elif target.startswith('inet:'):
035a21
+			ping_path = '@PING4_CMD@'
035a21
+			target_mangled = target.lstrip('inet:')
035a21
+
035a21
+		ping_cmd = "%s -n -q -W %d -c %d -i %d %s" % (
035a21
+			ping_path, timeout, count, interval, target_mangled)
035a21
+		logging.info("Running command: %s", ping_cmd)
035a21
+		try:
035a21
+			p[target] = subprocess.Popen(shlex.split(ping_cmd),
035a21
+				stdout=subprocess.PIPE);
035a21
+		except OSError:
035a21
+			p[target] = None
035a21
+
035a21
+	# collect the results of the ping-processes
035a21
+	for target in targets:
035a21
+		good = 0
035a21
+		if p[target] != None:
035a21
+			p[target].wait()
035a21
+			if p[target].returncode == 0:
035a21
+				for line in p[target].stdout:
035a21
+					searchres = packet_count.search(line)
035a21
+					if searchres:
035a21
+						good = int(searchres.group(1))
035a21
+						break
035a21
+				if good >= good_required:
035a21
+					logging.info("ping target %s received %d of %d" \
035a21
+						% (target, good, count))
035a21
+					continue
035a21
+			failcount += 1
035a21
+			logging.info("ping target %s received %d of %d and thus failed"
035a21
+				% (target, good, count))
035a21
+		else:
035a21
+			failcount += 1
035a21
+			logging.error("ping target %s failed on OS level" % target)
035a21
+
035a21
+	if failcount > maxfail:
035a21
+		exitcode = False
035a21
+
035a21
+	return exitcode
035a21
+
035a21
+
035a21
+def define_new_opts():
035a21
+	all_opt["ping_count"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "ping-count",
035a21
+		"required" : "0",
035a21
+		"help" : "--ping-count=[number]          Number of ping-probes to send",
035a21
+		"shortdesc" : "The number of ping-probes that is being sent per target",
035a21
+		"default" : "10",
035a21
+		"order" : 1
035a21
+		}
035a21
+
035a21
+	all_opt["ping_good_count"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "ping-good-count",
035a21
+		"required" : "0",
035a21
+		"help" : "--ping-good-count=[number]     Number of positive ping-probes required",
035a21
+		"shortdesc" : "The number of positive ping-probes required to account a target as available",
035a21
+		"default" : "8",
035a21
+		"order" : 1
035a21
+		}
035a21
+
035a21
+	all_opt["ping_interval"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "ping-interval",
035a21
+		"required" : "0",
035a21
+		"help" : "--ping-interval=[seconds]      Seconds between ping-probes",
035a21
+		"shortdesc" : "The interval in seconds between ping-probes",
035a21
+		"default" : "1",
035a21
+		"order" : 1
035a21
+		}
035a21
+
035a21
+	all_opt["ping_timeout"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "ping-timeout",
035a21
+		"required" : "0",
035a21
+		"help" : "--ping-timeout=[seconds]       Timeout for individual ping-probes",
035a21
+		"shortdesc" : "The timeout in seconds till an individual ping-probe is accounted as lost",
035a21
+		"default" : "2",
035a21
+		"order" : 1
035a21
+		}
035a21
+
035a21
+	all_opt["ping_maxfail"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "ping-maxfail",
035a21
+		"required" : "0",
035a21
+		"help" : "--ping-maxfail=[number]        Number of failed ping-targets allowed",
035a21
+		"shortdesc" : "The number of failed ping-targets to still account as overall success",
035a21
+		"default" : "0",
035a21
+		"order" : 1
035a21
+		}
035a21
+
035a21
+	all_opt["ping_targets"] = {
035a21
+		"getopt" : ":",
035a21
+		"longopt" : "ping-targets",
035a21
+		"required" : "1",
035a21
+		"help" : "--ping-targets=tgt1,[inet6:]tgt2  Comma separated list of ping-targets",
035a21
+		"shortdesc" : "A comma separated list of ping-targets (optionally prepended by 'inet:' or 'inet6:') to be probed",
035a21
+		"default" : "",
035a21
+		"order" : 1
035a21
+		}
035a21
+
035a21
+
035a21
+def main():
035a21
+	device_opt = ["no_status", "no_password", "ping_count", "ping_good_count",
035a21
+		"ping_interval", "ping_timeout", "ping_maxfail", "ping_targets", "method"]
035a21
+	define_new_opts()
035a21
+	atexit.register(atexit_handler)
035a21
+
035a21
+	all_opt["method"]["default"] = "cycle"
035a21
+	all_opt["method"]["help"] = "-m, --method=[method]          Method to fence (cycle|onoff) (Default: cycle)"
035a21
+
035a21
+	options = check_input(device_opt, process_input(device_opt))
035a21
+
035a21
+	docs = {}
035a21
+	docs["shortdesc"] = "Fence agent for ping-heuristic based fencing"
035a21
+	docs["longdesc"] = "fence_heuristics_ping uses ping-heuristics to control execution of another fence agent on the same fencing level.\
035a21
+\n.P\n\
035a21
+This is not a fence agent by itself! \
035a21
+Its only purpose is to enable/disable another fence agent that lives on the same fencing level but after fence_heuristics_ping.\
035a21
+\n.P\n\
035a21
+fence_heuristics_ping is currently provided as tech preview in RHEL-7.5."
035a21
+	docs["vendorurl"] = ""
035a21
+	show_docs(options, docs)
035a21
+
035a21
+	# move ping-test to the end of the time-window set via --delay
035a21
+	# as to give the network time to settle after the incident that has
035a21
+	# caused fencing and have the results as current as possible
035a21
+	max_pingcheck = (int(options["--ping-count"]) - 1) * \
035a21
+		int(options["--ping-interval"]) + int(options["--ping-timeout"])
035a21
+	run_delay(options, reserve=max_pingcheck)
035a21
+
035a21
+	result = fence_action(\
035a21
+				None, \
035a21
+				options, \
035a21
+				None, \
035a21
+				None, \
035a21
+				reboot_cycle_fn = ping_test,
035a21
+				sync_set_power_fn = ping_test)
035a21
+
035a21
+	# execute the remaining delay
035a21
+	run_delay(options, result=result)
035a21
+	sys.exit(result)
035a21
+
035a21
+if __name__ == "__main__":
035a21
+	main()
035a21
diff --git a/make/fencebuild.mk b/make/fencebuild.mk
035a21
index 0a1f2bc..25bb0f1 100644
035a21
--- a/make/fencebuild.mk
035a21
+++ b/make/fencebuild.mk
035a21
@@ -28,5 +28,8 @@ define gen_agent_from_py
035a21
 		-e 's#@''STORE_PATH@#${CLUSTERVARRUN}#g' \
035a21
 		-e 's#@''SUDO_PATH@#${SUDO_PATH}#g' \
035a21
+		-e 's#@''PING_CMD@#${PING_CMD}#g' \
035a21
+		-e 's#@''PING6_CMD@#${PING6_CMD}#g' \
035a21
+		-e 's#@''PING4_CMD@#${PING4_CMD}#g' \
035a21
 	> $@
035a21
 
035a21
 	if [ 0 -eq `echo "$(SRC)" | grep fence_ &> /dev/null; echo $$?` ]; then \
035a21
--- /dev/null	2017-10-08 13:42:59.634387493 +0200
035a21
+++ fence-agents-4.0.11/tests/data/metadata/fence_heuristics_ping.xml	2017-10-18 20:55:23.978815450 +0200
035a21
@@ -0,0 +1,117 @@
035a21
+
035a21
+<resource-agent name="fence_heuristics_ping" shortdesc="Fence agent for ping-heuristic based fencing" >
035a21
+<longdesc>fence_heuristics_ping uses ping-heuristics to control execution of another fence agent on the same fencing level.
035a21
+.P
035a21
+This is not a fence agent by itself! Its only purpose is to enable/disable another fence agent that lives on the same fencing level but after fence_heuristics_ping.
035a21
+.P
035a21
+fence_heuristics_ping is currently provided as tech preview in RHEL-7.5.</longdesc>
035a21
+<vendor-url></vendor-url>
035a21
+<parameters>
035a21
+	<parameter name="ping_interval" unique="0" required="0">
035a21
+		<getopt mixed="--ping-interval=[seconds]" />
035a21
+		<content type="string" default="1"  />
035a21
+		<shortdesc lang="en">The interval in seconds between ping-probes</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ping_maxfail" unique="0" required="0">
035a21
+		<getopt mixed="--ping-maxfail=[number]" />
035a21
+		<content type="string" default="0"  />
035a21
+		<shortdesc lang="en">The number of failed ping-targets to still account as overall success</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ping_targets" unique="0" required="1">
035a21
+		<getopt mixed="--ping-targets=tgt1,[inet6:]tgt2" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">A comma separated list of ping-targets (optionally prepended by 'inet:' or 'inet6:') to be probed</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="action" unique="0" required="1">
035a21
+		<getopt mixed="-o, --action=[action]" />
035a21
+		<content type="string" default="reboot"  />
035a21
+		<shortdesc lang="en">Fencing Action</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ping_good_count" unique="0" required="0">
035a21
+		<getopt mixed="--ping-good-count=[number]" />
035a21
+		<content type="string" default="8"  />
035a21
+		<shortdesc lang="en">The number of positive ping-probes required to account a target as available</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ping_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--ping-timeout=[seconds]" />
035a21
+		<content type="string" default="2"  />
035a21
+		<shortdesc lang="en">The timeout in seconds till an individual ping-probe is accounted as lost</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="ping_count" unique="0" required="0">
035a21
+		<getopt mixed="--ping-count=[number]" />
035a21
+		<content type="string" default="10"  />
035a21
+		<shortdesc lang="en">The number of ping-probes that is being sent per target</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="method" unique="0" required="0">
035a21
+		<getopt mixed="-m, --method=[method]" />
035a21
+		<content type="select" default="cycle"  >
035a21
+			<option value="onoff" />
035a21
+			<option value="cycle" />
035a21
+		</content>
035a21
+		<shortdesc lang="en">Method to fence (onoff|cycle)</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="verbose" unique="0" required="0">
035a21
+		<getopt mixed="-v, --verbose" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Verbose mode</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="debug" unique="0" required="0" deprecated="1">
035a21
+		<getopt mixed="-D, --debug-file=[debugfile]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="debug_file" unique="0" required="0" obsoletes="debug">
035a21
+		<getopt mixed="-D, --debug-file=[debugfile]" />
035a21
+		<content type="string"  />
035a21
+		<shortdesc lang="en">Write debug information to given file</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="version" unique="0" required="0">
035a21
+		<getopt mixed="-V, --version" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Display version information and exit</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="help" unique="0" required="0">
035a21
+		<getopt mixed="-h, --help" />
035a21
+		<content type="boolean"  />
035a21
+		<shortdesc lang="en">Display help and exit</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="shell_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--shell-timeout=[seconds]" />
035a21
+		<content type="second" default="3"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after issuing command</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="delay" unique="0" required="0">
035a21
+		<getopt mixed="--delay=[seconds]" />
035a21
+		<content type="second" default="0"  />
035a21
+		<shortdesc lang="en">Wait X seconds before fencing is started</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--power-timeout=[seconds]" />
035a21
+		<content type="second" default="20"  />
035a21
+		<shortdesc lang="en">Test X seconds for status change after ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="power_wait" unique="0" required="0">
035a21
+		<getopt mixed="--power-wait=[seconds]" />
035a21
+		<content type="second" default="0"  />
035a21
+		<shortdesc lang="en">Wait X seconds after issuing ON/OFF</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="login_timeout" unique="0" required="0">
035a21
+		<getopt mixed="--login-timeout=[seconds]" />
035a21
+		<content type="second" default="5"  />
035a21
+		<shortdesc lang="en">Wait X seconds for cmd prompt after login</shortdesc>
035a21
+	</parameter>
035a21
+	<parameter name="retry_on" unique="0" required="0">
035a21
+		<getopt mixed="--retry-on=[attempts]" />
035a21
+		<content type="integer" default="1"  />
035a21
+		<shortdesc lang="en">Count of attempts to retry power on</shortdesc>
035a21
+	</parameter>
035a21
+</parameters>
035a21
+<actions>
035a21
+	<action name="on" automatic="0"/>
035a21
+	<action name="off" />
035a21
+	<action name="reboot" />
035a21
+	<action name="monitor" />
035a21
+	<action name="metadata" />
035a21
+	<action name="validate-all" />
035a21
+</actions>
035a21
+</resource-agent>