| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# |
| 4 |
# NodeUtilization OCF Resource Agent |
| 5 |
# |
| 6 |
# Copyright (c) 2011 SUSE LINUX, John Shi |
| 7 |
# Copyright (c) 2016 SUSE LINUX, Kristoffer Gronlund |
| 8 |
# All Rights Reserved. |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of version 2 of the GNU General Public License as |
| 12 |
# published by the Free Software Foundation. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it would be useful, but |
| 15 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
# |
| 18 |
# Further, this software is distributed without any warranty that it is |
| 19 |
# free of the rightful claim of any third person regarding infringement |
| 20 |
# or the like. Any license provided herein, whether implied or |
| 21 |
# otherwise, applies only to this software file. Patent licenses, if |
| 22 |
# any, provided herein do not apply to combinations of this program with |
| 23 |
# other software, or any other product whatsoever. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU General Public License |
| 26 |
# along with this program; if not, write the Free Software Foundation, |
| 27 |
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 28 |
# |
| 29 |
####################################################################### |
| 30 |
# Initialization: |
| 31 |
|
| 32 |
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 33 |
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 34 |
|
| 35 |
# Parameter defaults |
| 36 |
|
| 37 |
OCF_RESKEY_pidfile_default="$HA_VARRUN/NodeUtilization-${OCF_RESOURCE_INSTANCE}" |
| 38 |
OCF_RESKEY_dynamic_default="true" |
| 39 |
OCF_RESKEY_utilization_cpu_default="true" |
| 40 |
OCF_RESKEY_utilization_cpu_reservation_default="1" |
| 41 |
OCF_RESKEY_utilization_host_memory_default="true" |
| 42 |
OCF_RESKEY_utilization_host_memory_reservation_default="512" |
| 43 |
OCF_RESKEY_utilization_hv_memory_default="true" |
| 44 |
OCF_RESKEY_utilization_hv_memory_reservation_default="512" |
| 45 |
|
| 46 |
: ${OCF_RESKEY_pidfile=${OCF_RESKEY_pidfile_default}} |
| 47 |
: ${OCF_RESKEY_dynamic=${OCF_RESKEY_dynamic_default}} |
| 48 |
: ${OCF_RESKEY_utilization_cpu=${OCF_RESKEY_utilization_cpu_default}} |
| 49 |
: ${OCF_RESKEY_utilization_cpu_reservation=${OCF_RESKEY_utilization_cpu_reservation_default}} |
| 50 |
: ${OCF_RESKEY_utilization_host_memory=${OCF_RESKEY_utilization_host_memory_default}} |
| 51 |
: ${OCF_RESKEY_utilization_host_memory_reservation=${OCF_RESKEY_utilization_host_memory_reservation_default}} |
| 52 |
: ${OCF_RESKEY_utilization_hv_memory=${OCF_RESKEY_utilization_hv_memory_default}} |
| 53 |
: ${OCF_RESKEY_utilization_hv_memory_reservation=${OCF_RESKEY_utilization_hv_memory_reservation_default}} |
| 54 |
|
| 55 |
####################################################################### |
| 56 |
|
| 57 |
NodeUtilization_meta_data() { |
| 58 |
cat <<END |
| 59 |
<?xml version="1.0"?> |
| 60 |
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 61 |
<resource-agent name="NodeUtilization" version="1.0"> |
| 62 |
<version>1.0</version> |
| 63 |
|
| 64 |
<longdesc lang="en"> |
| 65 |
The Node Utilization agent detects system parameters like available CPU, host |
| 66 |
memory and hypervisor memory availability, and adds them into the CIB for each |
| 67 |
node using crm_attribute. Run the agent as a clone resource to have it populate |
| 68 |
these parameters on each node. |
| 69 |
Note: Setting hv_memory only works with Xen at the moment, using the xl or xm |
| 70 |
command line tools. |
| 71 |
</longdesc> |
| 72 |
<shortdesc lang="en">Node Utilization</shortdesc> |
| 73 |
|
| 74 |
<parameters> |
| 75 |
<parameter name="dynamic" unique="0" required="0"> |
| 76 |
<longdesc lang="en"> |
| 77 |
If set, parameters will be updated if there are differences between the HA |
| 78 |
parameters and the system values when running the monitor action. |
| 79 |
If not set, the parameters will be set once when the resource instance starts. |
| 80 |
</longdesc> |
| 81 |
<shortdesc lang="en">Dynamically update parameters in monitor</shortdesc> |
| 82 |
<content type="boolean" default="${OCF_RESKEY_dynamic_default}" /> |
| 83 |
</parameter> |
| 84 |
|
| 85 |
<parameter name="utilization_cpu" unique="0" required="0"> |
| 86 |
<longdesc lang="en">Enable setting node CPU utilization limit.</longdesc> |
| 87 |
<shortdesc lang="en">Set node CPU utilization limit.</shortdesc> |
| 88 |
<content type="boolean" default="${OCF_RESKEY_utilization_cpu_default}" /> |
| 89 |
</parameter> |
| 90 |
|
| 91 |
<parameter name="utilization_cpu_reservation" unique="0" required="0"> |
| 92 |
<longdesc lang="en">Subtract this value when setting the CPU utilization parameter.</longdesc> |
| 93 |
<shortdesc lang="en">CPU reservation.</shortdesc> |
| 94 |
<content type="integer" default="${OCF_RESKEY_utilization_cpu_reservation_default}" /> |
| 95 |
</parameter> |
| 96 |
|
| 97 |
<parameter name="utilization_host_memory" unique="0" required="0"> |
| 98 |
<longdesc lang="en">Enable setting available host memory.</longdesc> |
| 99 |
<shortdesc lang="en">Set available host memory.</shortdesc> |
| 100 |
<content type="boolean" default="${OCF_RESKEY_utilization_host_memory_default}" /> |
| 101 |
</parameter> |
| 102 |
|
| 103 |
<parameter name="utilization_host_memory_reservation" unique="0" required="0"> |
| 104 |
<longdesc lang="en">Subtract this value when setting host memory utilization, in MB.</longdesc> |
| 105 |
<shortdesc lang="en">Host memory reservation, in MB.</shortdesc> |
| 106 |
<content type="integer" default="${OCF_RESKEY_utilization_host_memory_reservation_default}" /> |
| 107 |
</parameter> |
| 108 |
|
| 109 |
<parameter name="utilization_hv_memory" unique="0" required="0"> |
| 110 |
<longdesc lang="en">Enable setting available hypervisor memory.</longdesc> |
| 111 |
<shortdesc lang="en">Set available hypervisor memory.</shortdesc> |
| 112 |
<content type="boolean" default="${OCF_RESKEY_utilization_hv_memory_default}" /> |
| 113 |
</parameter> |
| 114 |
|
| 115 |
<parameter name="utilization_hv_memory_reservation" unique="0" required="0"> |
| 116 |
<longdesc lang="en">Subtract this value when setting hypervisor memory utilization, in MB.</longdesc> |
| 117 |
<shortdesc lang="en">Hypervisor memory reservation, in MB.</shortdesc> |
| 118 |
<content type="integer" default="${OCF_RESKEY_utilization_hv_memory_reservation_default}" /> |
| 119 |
</parameter> |
| 120 |
</parameters> |
| 121 |
|
| 122 |
<actions> |
| 123 |
<action name="start" timeout="90s" /> |
| 124 |
<action name="stop" timeout="100s" /> |
| 125 |
<action name="monitor" timeout="20s" interval="60s"/> |
| 126 |
<action name="meta-data" timeout="5s" /> |
| 127 |
<action name="validate-all" timeout="30s" /> |
| 128 |
</actions> |
| 129 |
</resource-agent> |
| 130 |
END |
| 131 |
} |
| 132 |
|
| 133 |
Host_Total_Memory() { |
| 134 |
local xentool |
| 135 |
|
| 136 |
xentool=$(which xl 2> /dev/null || which xm 2> /dev/null) |
| 137 |
|
| 138 |
if [ -x "$xentool" ]; then |
| 139 |
"$xentool" info | awk '/total_memory/{printf("%d\n",$3);exit(0)}' |
| 140 |
else |
| 141 |
ocf_log debug "Can only set hv_memory for Xen hypervisor" |
| 142 |
echo "0" |
| 143 |
fi |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
set_utilization() { |
| 148 |
host_name="$(ocf_local_nodename)" |
| 149 |
|
| 150 |
if ocf_is_true "$OCF_RESKEY_utilization_cpu"; then |
| 151 |
sys_cpu=$(( $(grep -c processor /proc/cpuinfo) - $OCF_RESKEY_utilization_cpu_reservation )) |
| 152 |
uti_cpu=$(crm_attribute --quiet -t nodes --node "$host_name" -z -n cpu 2>/dev/null) |
| 153 |
|
| 154 |
if [ "$sys_cpu" != "$uti_cpu" ]; then |
| 155 |
if ! crm_attribute -t nodes --node "$host_name" -z -n cpu -v $sys_cpu; then |
| 156 |
ocf_log err "Failed to set the cpu utilization attribute for $host_name using crm_attribute." |
| 157 |
return 1 |
| 158 |
fi |
| 159 |
fi |
| 160 |
fi |
| 161 |
|
| 162 |
if ocf_is_true "$OCF_RESKEY_utilization_host_memory"; then |
| 163 |
sys_mem=$(( $(awk '/MemTotal/{printf("%d\n",$2/1024);exit(0)}' /proc/meminfo) - $OCF_RESKEY_utilization_host_memory_reservation )) |
| 164 |
uti_mem=$(crm_attribute --quiet -t nodes --node "$host_name" -z -n host_memory 2>/dev/null) |
| 165 |
|
| 166 |
if [ "$sys_mem" != "$uti_mem" ]; then |
| 167 |
if ! crm_attribute -t nodes --node "$host_name" -z -n host_memory -v $sys_mem; then |
| 168 |
ocf_log err "Failed to set the host_memory utilization attribute for $host_name using crm_attribute." |
| 169 |
return 1 |
| 170 |
fi |
| 171 |
fi |
| 172 |
fi |
| 173 |
|
| 174 |
if ocf_is_true "$OCF_RESKEY_utilization_hv_memory"; then |
| 175 |
hv_mem=$(( $(Host_Total_Memory) - OCF_RESKEY_utilization_hv_memory_reservation )) |
| 176 |
uti_mem=$(crm_attribute --quiet -t nodes --node "$host_name" -z -n hv_memory 2>/dev/null) |
| 177 |
|
| 178 |
[ $hv_mem -lt 0 ] && hv_mem=0 |
| 179 |
|
| 180 |
if [ "$hv_mem" != "$uti_mem" ]; then |
| 181 |
if ! crm_attribute -t nodes --node "$host_name" -z -n hv_memory -v $hv_mem; then |
| 182 |
ocf_log err "Failed to set the hv_memory utilization attribute for $host_name using crm_attribute." |
| 183 |
return 1 |
| 184 |
fi |
| 185 |
fi |
| 186 |
fi |
| 187 |
} |
| 188 |
|
| 189 |
NodeUtilization_usage() { |
| 190 |
cat <<END |
| 191 |
usage: $0 {start|stop|monitor|validate-all|meta-data} |
| 192 |
|
| 193 |
Expects to have a fully populated OCF RA-compliant environment set. |
| 194 |
END |
| 195 |
} |
| 196 |
|
| 197 |
NodeUtilization_start() { |
| 198 |
ha_pseudo_resource $statefile start |
| 199 |
if ! ocf_is_true "$OCF_RESKEY_dynamic"; then |
| 200 |
if ! set_utilization; then |
| 201 |
exit $OCF_ERR_GENERIC |
| 202 |
fi |
| 203 |
fi |
| 204 |
exit $OCF_SUCCESS |
| 205 |
} |
| 206 |
|
| 207 |
NodeUtilization_stop() { |
| 208 |
ha_pseudo_resource $statefile stop |
| 209 |
exit $OCF_SUCCESS |
| 210 |
} |
| 211 |
|
| 212 |
NodeUtilization_monitor() { |
| 213 |
local rc |
| 214 |
ha_pseudo_resource $statefile monitor |
| 215 |
rc=$? |
| 216 |
|
| 217 |
case $rc in |
| 218 |
$OCF_SUCCESS) |
| 219 |
if ocf_is_true "$OCF_RESKEY_dynamic"; then |
| 220 |
if ! set_utilization; then |
| 221 |
exit $OCF_ERR_GENERIC |
| 222 |
fi |
| 223 |
fi |
| 224 |
;; |
| 225 |
*) exit $rc;; |
| 226 |
esac |
| 227 |
} |
| 228 |
|
| 229 |
NodeUtilization_validate() { |
| 230 |
exit $OCF_SUCCESS |
| 231 |
} |
| 232 |
|
| 233 |
statefile=$OCF_RESOURCE_TYPE.$(echo $OCF_RESOURCE_INSTANCE | sed -e 's/^.*://') |
| 234 |
|
| 235 |
OCF_REQUIRED_PARAMS="" |
| 236 |
OCF_REQUIRED_BINARIES="" |
| 237 |
ocf_rarun $* |