Blame SOURCES/bz1430304-NodeUtilization.patch

15862b
diff -uNr a/doc/man/Makefile.am b/doc/man/Makefile.am
15862b
--- a/doc/man/Makefile.am	2017-03-15 14:11:58.136058131 +0100
15862b
+++ b/doc/man/Makefile.am	2017-03-15 14:31:58.181539045 +0100
15862b
@@ -73,6 +73,7 @@
15862b
                           ocf_heartbeat_MailTo.7 \
15862b
                           ocf_heartbeat_ManageRAID.7 \
15862b
                           ocf_heartbeat_ManageVE.7 \
15862b
+                          ocf_heartbeat_NodeUtilization.7 \
15862b
                           ocf_heartbeat_nova-compute-wait.7 \
15862b
                           ocf_heartbeat_NovaEvacuate.7 \
15862b
                           ocf_heartbeat_Pure-FTPd.7 \
15862b
diff -uNr a/heartbeat/Makefile.am b/heartbeat/Makefile.am
15862b
--- a/heartbeat/Makefile.am	2017-03-15 14:11:58.136058131 +0100
15862b
+++ b/heartbeat/Makefile.am	2017-03-15 14:32:45.554873187 +0100
15862b
@@ -95,6 +95,7 @@
15862b
 			MailTo			\
15862b
 			ManageRAID		\
15862b
 			ManageVE		\
15862b
+			NodeUtilization		\
15862b
 			mysql			\
15862b
 			mysql-proxy		\
15862b
 			nagios			\
15862b
diff -uNr a/heartbeat/NodeUtilization b/heartbeat/NodeUtilization
15862b
--- a/heartbeat/NodeUtilization	1970-01-01 01:00:00.000000000 +0100
15862b
+++ b/heartbeat/NodeUtilization	2017-03-15 14:29:18.141788491 +0100
15862b
@@ -0,0 +1,226 @@
15862b
+#!/bin/sh
15862b
+#
15862b
+#
15862b
+#	NodeUtilization OCF Resource Agent
15862b
+#
15862b
+# Copyright (c) 2011 SUSE LINUX, John Shi
15862b
+# Copyright (c) 2016 SUSE LINUX, Kristoffer Gronlund
15862b
+#                    All Rights Reserved.
15862b
+#
15862b
+# This program is free software; you can redistribute it and/or modify
15862b
+# it under the terms of version 2 of the GNU General Public License as
15862b
+# published by the Free Software Foundation.
15862b
+#
15862b
+# This program is distributed in the hope that it would be useful, but
15862b
+# WITHOUT ANY WARRANTY; without even the implied warranty of
15862b
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15862b
+#
15862b
+# Further, this software is distributed without any warranty that it is
15862b
+# free of the rightful claim of any third person regarding infringement
15862b
+# or the like.  Any license provided herein, whether implied or
15862b
+# otherwise, applies only to this software file.  Patent licenses, if
15862b
+# any, provided herein do not apply to combinations of this program with
15862b
+# other software, or any other product whatsoever.
15862b
+#
15862b
+# You should have received a copy of the GNU General Public License
15862b
+# along with this program; if not, write the Free Software Foundation,
15862b
+# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
15862b
+#
15862b
+#######################################################################
15862b
+# Initialization:
15862b
+
15862b
+: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
15862b
+. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
15862b
+
15862b
+#######################################################################
15862b
+
15862b
+NodeUtilization_meta_data() {
15862b
+	cat <
15862b
+
15862b
+
15862b
+<resource-agent name="NodeUtilization">
15862b
+<version>1.0</version>
15862b
+
15862b
+<longdesc lang="en">
15862b
+The Node Utilization agent detects system parameters like available CPU, host
15862b
+memory and hypervisor memory availability, and adds them into the CIB for each
15862b
+node using crm_attribute. Run the agent as a clone resource to have it populate
15862b
+these parameters on each node.
15862b
+Note: Setting hv_memory only works with Xen at the moment, using the xl or xm
15862b
+command line tools.
15862b
+</longdesc>
15862b
+<shortdesc lang="en">Node Utilization</shortdesc>
15862b
+
15862b
+<parameters>
15862b
+<parameter name="dynamic" unique="0" required="0">
15862b
+<longdesc lang="en">
15862b
+If set, parameters will be updated if there are differences between the HA
15862b
+parameters and the system values when running the monitor action.
15862b
+If not set, the parameters will be set once when the resource instance starts.
15862b
+</longdesc>
15862b
+<shortdesc lang="en">Dynamically update parameters in monitor</shortdesc>
15862b
+<content type="boolean" default="true" />
15862b
+</parameter>
15862b
+
15862b
+<parameter name="utilization_cpu" unique="0" required="0">
15862b
+<longdesc lang="en">Enable setting node CPU utilization limit.</longdesc>
15862b
+<shortdesc lang="en">Set node CPU utilization limit.</shortdesc>
15862b
+<content type="boolean" default="true" />
15862b
+</parameter>
15862b
+
15862b
+<parameter name="utilization_cpu_reservation" unique="0" required="0">
15862b
+<longdesc lang="en">Subtract this value when setting the CPU utilization parameter.</longdesc>
15862b
+<shortdesc lang="en">CPU reservation.</shortdesc>
15862b
+<content type="integer" default="1" />
15862b
+</parameter>
15862b
+
15862b
+<parameter name="utilization_host_memory" unique="0" required="0">
15862b
+<longdesc lang="en">Enable setting available host memory.</longdesc>
15862b
+<shortdesc lang="en">Set available host memory.</shortdesc>
15862b
+<content type="boolean" default="true" />
15862b
+</parameter>
15862b
+
15862b
+<parameter name="utilization_host_memory_reservation" unique="0" required="0">
15862b
+<longdesc lang="en">Subtract this value when setting host memory utilization, in MB.</longdesc>
15862b
+<shortdesc lang="en">Host memory reservation, in MB.</shortdesc>
15862b
+<content type="integer" default="512" />
15862b
+</parameter>
15862b
+
15862b
+<parameter name="utilization_hv_memory" unique="0" required="0">
15862b
+<longdesc lang="en">Enable setting available hypervisor memory.</longdesc>
15862b
+<shortdesc lang="en">Set available hypervisor memory.</shortdesc>
15862b
+<content type="boolean" default="true" />
15862b
+</parameter>
15862b
+
15862b
+<parameter name="utilization_hv_memory_reservation" unique="0" required="0">
15862b
+<longdesc lang="en">Subtract this value when setting hypervisor memory utilization, in MB.</longdesc>
15862b
+<shortdesc lang="en">Hypervisor memory reservation, in MB.</shortdesc>
15862b
+<content type="integer" default="512" />
15862b
+</parameter>
15862b
+</parameters>
15862b
+
15862b
+<actions>
15862b
+<action name="start"   timeout="90" />
15862b
+<action name="stop"    timeout="100" />
15862b
+<action name="monitor" timeout="20s" interval="60s"/>
15862b
+<action name="meta-data"  timeout="5" />
15862b
+<action name="validate-all"  timeout="30" />
15862b
+</actions>
15862b
+</resource-agent>
15862b
+END
15862b
+}
15862b
+
15862b
+Host_Total_Memory() {
15862b
+    local xentool
15862b
+
15862b
+    xentool=$(which xl 2> /dev/null || which xm 2> /dev/null)
15862b
+
15862b
+    if [ -x $xentool ]; then
15862b
+        $xentool info | awk '/total_memory/{printf("%d\n",$3);exit(0)}'
15862b
+    else
15862b
+        ocf_log warn "Can only set hv_memory for Xen hypervisor"
15862b
+        echo "0"
15862b
+    fi
15862b
+}
15862b
+
15862b
+
15862b
+set_utilization() {
15862b
+    host_name="$(ocf_local_nodename)"
15862b
+
15862b
+    if ocf_is_true "$OCF_RESKEY_utilization_cpu"; then
15862b
+        sys_cpu=$(( $(grep -c processor /proc/cpuinfo) - $OCF_RESKEY_utilization_cpu_reservation ))
15862b
+        uti_cpu=$(crm_attribute -Q -t nodes -U "$host_name" -z -n cpu 2>/dev/null)
15862b
+
15862b
+        if [ "$sys_cpu" != "$uti_cpu" ]; then
15862b
+            if ! crm_attribute -t nodes -U "$host_name" -z -n cpu -v $sys_cpu; then
15862b
+                ocf_log err "Failed to set the cpu utilization attribute for $host_name using crm_attribute."
15862b
+                return 1
15862b
+            fi
15862b
+        fi
15862b
+    fi
15862b
+
15862b
+    if ocf_is_true "$OCF_RESKEY_utilization_host_memory"; then
15862b
+        sys_mem=$(( $(awk '/MemTotal/{printf("%d\n",$2/1024);exit(0)}' /proc/meminfo) - $OCF_RESKEY_utilization_host_memory_reservation ))
15862b
+        uti_mem=$(crm_attribute -Q -t nodes -U "$host_name" -z -n host_memory 2>/dev/null)
15862b
+
15862b
+        if [ "$sys_mem" != "$uti_mem" ]; then
15862b
+            if ! crm_attribute -t nodes -U "$host_name" -z -n host_memory -v $sys_mem; then
15862b
+                ocf_log err "Failed to set the host_memory utilization attribute for $host_name using crm_attribute."
15862b
+                return 1
15862b
+            fi
15862b
+        fi
15862b
+    fi
15862b
+
15862b
+    if ocf_is_true "$OCF_RESKEY_utilization_hv_memory"; then
15862b
+        hv_mem=$(( $(Host_Total_Memory) - OCF_RESKEY_utilization_hv_memory_reservation ))
15862b
+        uti_mem=$(crm_attribute -Q -t nodes -U "$host_name" -z -n hv_memory 2>/dev/null)
15862b
+
15862b
+        [ $hv_mem -lt 0 ] && hv_mem=0
15862b
+
15862b
+        if [ "$hv_mem" != "$uti_mem" ]; then
15862b
+            if ! crm_attribute -t nodes -U "$host_name" -z -n hv_memory -v $hv_mem; then
15862b
+                ocf_log err "Failed to set the hv_memory utilization attribute for $host_name using crm_attribute."
15862b
+                return 1
15862b
+            fi
15862b
+        fi
15862b
+    fi
15862b
+}
15862b
+
15862b
+NodeUtilization_usage() {
15862b
+    cat <
15862b
+usage: $0 {start|stop|monitor|validate-all|meta-data}
15862b
+
15862b
+Expects to have a fully populated OCF RA-compliant environment set.
15862b
+END
15862b
+}
15862b
+
15862b
+NodeUtilization_start() {
15862b
+    ha_pseudo_resource $statefile start
15862b
+    if ! ocf_is_true "$OCF_RESKEY_dynamic"; then
15862b
+        if ! set_utilization; then
15862b
+            exit $OCF_ERR_GENERIC
15862b
+        fi
15862b
+    fi
15862b
+    exit $OCF_SUCCESS
15862b
+}
15862b
+
15862b
+NodeUtilization_stop() {
15862b
+    ha_pseudo_resource $statefile stop
15862b
+    exit $OCF_SUCCESS
15862b
+}
15862b
+
15862b
+NodeUtilization_monitor() {
15862b
+    local rc
15862b
+    ha_pseudo_resource $statefile monitor
15862b
+    rc=$?
15862b
+
15862b
+    case $rc in
15862b
+        $OCF_SUCCESS)
15862b
+            if ocf_is_true "$OCF_RESKEY_dynamic"; then
15862b
+                if ! set_utilization; then
15862b
+                    exit $OCF_ERR_GENERIC
15862b
+                fi
15862b
+            fi
15862b
+            ;;
15862b
+        *) exit $rc;;
15862b
+    esac
15862b
+}
15862b
+
15862b
+NodeUtilization_validate() {
15862b
+    exit $OCF_SUCCESS
15862b
+}
15862b
+
15862b
+statefile=$OCF_RESOURCE_TYPE.$(echo $OCF_RESOURCE_INSTANCE | sed -e 's/^.*://')
15862b
+
15862b
+: ${OCF_RESKEY_pidfile:="$HA_VARRUN/NodeUtilization-${OCF_RESOURCE_INSTANCE}"}
15862b
+: ${OCF_RESKEY_dynamic:="true"}
15862b
+: ${OCF_RESKEY_utilization_cpu:="true"}
15862b
+: ${OCF_RESKEY_utilization_cpu_reservation="1"}
15862b
+: ${OCF_RESKEY_utilization_hv_memory:="true"}
15862b
+: ${OCF_RESKEY_utilization_hv_memory_reservation="512"}
15862b
+: ${OCF_RESKEY_utilization_host_memory:="true"}
15862b
+: ${OCF_RESKEY_utilization_host_memory_reservation="512"}
15862b
+
15862b
+OCF_REQUIRED_PARAMS=""
15862b
+OCF_REQUIRED_BINARIES=""
15862b
+ocf_rarun $*