summaryrefslogtreecommitdiffstats
path: root/heartbeat/NodeUtilization
blob: f98ab13e9ca25b1a3129fb883589e5b83f254654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh
#
#
#	NodeUtilization OCF Resource Agent
#
# Copyright (c) 2011 SUSE LINUX, John Shi
# Copyright (c) 2016 SUSE LINUX, Kristoffer Gronlund
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Parameter defaults

OCF_RESKEY_pidfile_default="$HA_VARRUN/NodeUtilization-${OCF_RESOURCE_INSTANCE}"
OCF_RESKEY_dynamic_default="true"
OCF_RESKEY_utilization_cpu_default="true"
OCF_RESKEY_utilization_cpu_reservation_default="1"
OCF_RESKEY_utilization_host_memory_default="true"
OCF_RESKEY_utilization_host_memory_reservation_default="512"
OCF_RESKEY_utilization_hv_memory_default="true"
OCF_RESKEY_utilization_hv_memory_reservation_default="512"

: ${OCF_RESKEY_pidfile=${OCF_RESKEY_pidfile_default}}
: ${OCF_RESKEY_dynamic=${OCF_RESKEY_dynamic_default}}
: ${OCF_RESKEY_utilization_cpu=${OCF_RESKEY_utilization_cpu_default}}
: ${OCF_RESKEY_utilization_cpu_reservation=${OCF_RESKEY_utilization_cpu_reservation_default}}
: ${OCF_RESKEY_utilization_host_memory=${OCF_RESKEY_utilization_host_memory_default}}
: ${OCF_RESKEY_utilization_host_memory_reservation=${OCF_RESKEY_utilization_host_memory_reservation_default}}
: ${OCF_RESKEY_utilization_hv_memory=${OCF_RESKEY_utilization_hv_memory_default}}
: ${OCF_RESKEY_utilization_hv_memory_reservation=${OCF_RESKEY_utilization_hv_memory_reservation_default}}

#######################################################################

NodeUtilization_meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="NodeUtilization" version="1.0">
<version>1.0</version>

<longdesc lang="en">
The Node Utilization agent detects system parameters like available CPU, host
memory and hypervisor memory availability, and adds them into the CIB for each
node using crm_attribute. Run the agent as a clone resource to have it populate
these parameters on each node.
Note: Setting hv_memory only works with Xen at the moment, using the xl or xm
command line tools.
</longdesc>
<shortdesc lang="en">Node Utilization</shortdesc>

<parameters>
<parameter name="dynamic" unique="0" required="0">
<longdesc lang="en">
If set, parameters will be updated if there are differences between the HA
parameters and the system values when running the monitor action.
If not set, the parameters will be set once when the resource instance starts.
</longdesc>
<shortdesc lang="en">Dynamically update parameters in monitor</shortdesc>
<content type="boolean" default="${OCF_RESKEY_dynamic_default}" />
</parameter>

<parameter name="utilization_cpu" unique="0" required="0">
<longdesc lang="en">Enable setting node CPU utilization limit.</longdesc>
<shortdesc lang="en">Set node CPU utilization limit.</shortdesc>
<content type="boolean" default="${OCF_RESKEY_utilization_cpu_default}" />
</parameter>

<parameter name="utilization_cpu_reservation" unique="0" required="0">
<longdesc lang="en">Subtract this value when setting the CPU utilization parameter.</longdesc>
<shortdesc lang="en">CPU reservation.</shortdesc>
<content type="integer" default="${OCF_RESKEY_utilization_cpu_reservation_default}" />
</parameter>

<parameter name="utilization_host_memory" unique="0" required="0">
<longdesc lang="en">Enable setting available host memory.</longdesc>
<shortdesc lang="en">Set available host memory.</shortdesc>
<content type="boolean" default="${OCF_RESKEY_utilization_host_memory_default}" />
</parameter>

<parameter name="utilization_host_memory_reservation" unique="0" required="0">
<longdesc lang="en">Subtract this value when setting host memory utilization, in MB.</longdesc>
<shortdesc lang="en">Host memory reservation, in MB.</shortdesc>
<content type="integer" default="${OCF_RESKEY_utilization_host_memory_reservation_default}" />
</parameter>

<parameter name="utilization_hv_memory" unique="0" required="0">
<longdesc lang="en">Enable setting available hypervisor memory.</longdesc>
<shortdesc lang="en">Set available hypervisor memory.</shortdesc>
<content type="boolean" default="${OCF_RESKEY_utilization_hv_memory_default}" />
</parameter>

<parameter name="utilization_hv_memory_reservation" unique="0" required="0">
<longdesc lang="en">Subtract this value when setting hypervisor memory utilization, in MB.</longdesc>
<shortdesc lang="en">Hypervisor memory reservation, in MB.</shortdesc>
<content type="integer" default="${OCF_RESKEY_utilization_hv_memory_reservation_default}" />
</parameter>
</parameters>

<actions>
<action name="start"   timeout="90s" />
<action name="stop"    timeout="100s" />
<action name="monitor" timeout="20s" interval="60s"/>
<action name="meta-data"  timeout="5s" />
<action name="validate-all"  timeout="30s" />
</actions>
</resource-agent>
END
}

Host_Total_Memory() {
    local xentool

    xentool=$(which xl 2> /dev/null || which xm 2> /dev/null)

    if [ -x "$xentool" ]; then
        "$xentool" info | awk '/total_memory/{printf("%d\n",$3);exit(0)}'
    else
        ocf_log debug "Can only set hv_memory for Xen hypervisor"
        echo "0"
    fi
}


set_utilization() {
    host_name="$(ocf_local_nodename)"

    if ocf_is_true "$OCF_RESKEY_utilization_cpu"; then
        sys_cpu=$(( $(grep -c processor /proc/cpuinfo) - $OCF_RESKEY_utilization_cpu_reservation ))
        uti_cpu=$(crm_attribute --quiet -t nodes --node "$host_name" -z -n cpu 2>/dev/null)

        if [ "$sys_cpu" != "$uti_cpu" ]; then
            if ! crm_attribute -t nodes --node "$host_name" -z -n cpu -v $sys_cpu; then
                ocf_log err "Failed to set the cpu utilization attribute for $host_name using crm_attribute."
                return 1
            fi
        fi
    fi

    if ocf_is_true "$OCF_RESKEY_utilization_host_memory"; then
        sys_mem=$(( $(awk '/MemTotal/{printf("%d\n",$2/1024);exit(0)}' /proc/meminfo) - $OCF_RESKEY_utilization_host_memory_reservation ))
        uti_mem=$(crm_attribute --quiet -t nodes --node "$host_name" -z -n host_memory 2>/dev/null)

        if [ "$sys_mem" != "$uti_mem" ]; then
            if ! crm_attribute -t nodes --node "$host_name" -z -n host_memory -v $sys_mem; then
                ocf_log err "Failed to set the host_memory utilization attribute for $host_name using crm_attribute."
                return 1
            fi
        fi
    fi

    if ocf_is_true "$OCF_RESKEY_utilization_hv_memory"; then
        hv_mem=$(( $(Host_Total_Memory) - OCF_RESKEY_utilization_hv_memory_reservation ))
        uti_mem=$(crm_attribute --quiet -t nodes --node "$host_name" -z -n hv_memory 2>/dev/null)

        [ $hv_mem -lt 0 ] && hv_mem=0

        if [ "$hv_mem" != "$uti_mem" ]; then
            if ! crm_attribute -t nodes --node "$host_name" -z -n hv_memory -v $hv_mem; then
                ocf_log err "Failed to set the hv_memory utilization attribute for $host_name using crm_attribute."
                return 1
            fi
        fi
    fi
}

NodeUtilization_usage() {
    cat <<END
usage: $0 {start|stop|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

NodeUtilization_start() {
    ha_pseudo_resource $statefile start
    if ! ocf_is_true "$OCF_RESKEY_dynamic"; then
        if ! set_utilization; then
            exit $OCF_ERR_GENERIC
        fi
    fi
    exit $OCF_SUCCESS
}

NodeUtilization_stop() {
    ha_pseudo_resource $statefile stop
    exit $OCF_SUCCESS
}

NodeUtilization_monitor() {
    local rc
    ha_pseudo_resource $statefile monitor
    rc=$?

    case $rc in
        $OCF_SUCCESS)
            if ocf_is_true "$OCF_RESKEY_dynamic"; then
                if ! set_utilization; then
                    exit $OCF_ERR_GENERIC
                fi
            fi
            ;;
        *) exit $rc;;
    esac
}

NodeUtilization_validate() {
    exit $OCF_SUCCESS
}

statefile=$OCF_RESOURCE_TYPE.$(echo $OCF_RESOURCE_INSTANCE | sed -e 's/^.*://')

OCF_REQUIRED_PARAMS=""
OCF_REQUIRED_BINARIES=""
ocf_rarun $*