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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
#!/bin/sh
#
# ocf:pacemaker:ping resource agent
#
# Copyright 2009-2023 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# or later (GPLv2+) WITHOUT ANY WARRANTY.
#
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS:="${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs"}
. "${OCF_FUNCTIONS}"
: ${__OCF_ACTION:="$1"}
# Explicitly list all environment variables used, to make static analysis happy
: ${OCF_RESKEY_CRM_meta_timeout:="20000"}
: ${OCF_RESKEY_CRM_meta_globally_unique:="false"}
: ${OCF_RESKEY_name:="pingd"}
: ${OCF_RESKEY_dampen:="5s"}
: ${OCF_RESKEY_attempts:="3"}
: ${OCF_RESKEY_multiplier:="1"}
: ${OCF_RESKEY_debug:="0"}
: ${OCF_RESKEY_failure_score:="0"}
: ${OCF_RESKEY_use_fping:="1"}
: ${OCF_RESKEY_host_list:=""}
: ${OCF_RESKEY_options:=""}
: ${OCF_RESKEY_timeout:=""}
#######################################################################
meta_data() {
cat <<END
<?xml version="1.0"?>
<resource-agent name="ping" version="@VERSION@">
<version>1.1</version>
<longdesc lang="en">
Every time the monitor action is run, this resource agent records (in the CIB) the current number of nodes the host can connect to using the system fping (preferred) or ping tool.
</longdesc>
<shortdesc lang="en">node connectivity</shortdesc>
<parameters>
<parameter name="pidfile" unique-group="pidfile">
<longdesc lang="en">PID file</longdesc>
<shortdesc lang="en">PID file</shortdesc>
<content type="string" default="${HA_VARRUN%%/}/ping-${OCF_RESOURCE_INSTANCE}" />
</parameter>
<parameter name="dampen" reloadable="1">
<longdesc lang="en">
The time to wait (dampening) further changes occur
</longdesc>
<shortdesc lang="en">Dampening interval</shortdesc>
<content type="integer" default="5s"/>
</parameter>
<parameter name="name" unique-group="name">
<longdesc lang="en">
The name of the attributes to set. This is the name to be used in the constraints.
</longdesc>
<shortdesc lang="en">Attribute name</shortdesc>
<content type="string" default="pingd"/>
</parameter>
<parameter name="multiplier">
<longdesc lang="en">
The number by which to multiply the number of connected ping nodes by
</longdesc>
<shortdesc lang="en">Value multiplier</shortdesc>
<content type="integer" default="1"/>
</parameter>
<parameter name="host_list" required="1">
<longdesc lang="en">
A space separated list of ping nodes to count.
</longdesc>
<shortdesc lang="en">Host list</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="attempts" reloadable="1">
<longdesc lang="en">
Number of ping attempts, per host, before declaring it dead
</longdesc>
<shortdesc lang="en">no. of ping attempts</shortdesc>
<content type="integer" default="3"/>
</parameter>
<parameter name="timeout" reloadable="1">
<longdesc lang="en">
How long, in seconds, to wait before declaring a ping lost
</longdesc>
<shortdesc lang="en">ping timeout in seconds</shortdesc>
<content type="integer" default="2"/>
</parameter>
<parameter name="options">
<longdesc lang="en">
A catch all for any other options that need to be passed to ping.
</longdesc>
<shortdesc lang="en">Extra Options</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="failure_score">
<longdesc lang="en">
Resource is failed if the score is less than failure_score.
Default never fails.
</longdesc>
<shortdesc lang="en">failure_score</shortdesc>
<content type="integer" default=""/>
</parameter>
<parameter name="use_fping">
<longdesc lang="en">
Use fping rather than ping, if found. If set to 0, fping
will not be used even if present.
</longdesc>
<shortdesc lang="en">Use fping if available</shortdesc>
<content type="boolean" default="1"/>
</parameter>
<parameter name="debug" reloadable="1">
<longdesc lang="en">
Enables to use default attrd_updater verbose logging on every call.
</longdesc>
<shortdesc lang="en">Verbose logging</shortdesc>
<content type="string" default="false"/>
</parameter>
</parameters>
<actions>
<action name="start" timeout="60s" />
<action name="stop" timeout="20s" />
<action name="monitor" depth="0" timeout="60s" interval="10s"/>
<action name="meta-data" timeout="5s" />
<action name="validate-all" timeout="30s" />
<action name="reload-agent" timeout="20s" />
</actions>
</resource-agent>
END
}
#######################################################################
ping_conditional_log() {
level="$1"; shift
if [ $OCF_RESKEY_debug -gt 0 ]; then
ocf_log "$level" "$*"
fi
}
ping_usage() {
cat <<END
usage: $0 {start|stop|monitor|validate-all|reload-agent|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
END
}
ping_start() {
ping_monitor
if [ $? -eq $OCF_SUCCESS ]; then
return $OCF_SUCCESS
fi
touch "${OCF_RESKEY_pidfile}"
ping_update
}
ping_stop() {
rm -f "${OCF_RESKEY_pidfile}"
attrd_updater -D -n "$OCF_RESKEY_name" -d "$OCF_RESKEY_dampen"
return $OCF_SUCCESS
}
ping_monitor() {
if [ -f "${OCF_RESKEY_pidfile}" ]; then
ping_update
if [ $? -eq 0 ]; then
return $OCF_SUCCESS
fi
return $OCF_ERR_GENERIC
fi
return $OCF_NOT_RUNNING
}
ping_validate() {
# Host-specific checks
if [ "$OCF_CHECK_LEVEL" = "10" ]; then
# Is the state directory writable?
state_dir=$(dirname "$OCF_RESKEY_pidfile")
touch "$state_dir/$$"
if [ $? -ne 0 ]; then
ocf_log err "Invalid location for 'state': $state_dir is not writable"
return $OCF_ERR_ARGS
fi
rm "$state_dir/$$"
# Does the ping binary exist?
check_binary ping
fi
# Pidfile better be an absolute path
case "$OCF_RESKEY_pidfile" in
/*) ;;
*) ocf_log warn "You should use an absolute path for pidfile not: $OCF_RESKEY_pidfile" ;;
esac
# Check the host list
if [ -z "$OCF_RESKEY_host_list" ]; then
ocf_log err "Empty host_list. Please specify some nodes to ping"
exit $OCF_ERR_CONFIGURED
fi
# For fping allow only same IP versions or hostnames
if use_fping; then
hosts_family
if [ $? -eq 99 ]; then
ocf_log err "host_list can contain only host with same IP versions for fping"
exit $OCF_ERR_CONFIGURED
fi
fi
return $OCF_SUCCESS
}
ping_reload_agent() {
# No action required
:;
}
fping_check() {
p_exe=fping
hosts_family
case $? in
6) p_exe=fping6 ;;
99) ocf_log err "Ambiguous IP versions in host_list: '$OCF_RESKEY_host_list'"; exit $OCF_ERR_CONFIGURED;;
esac
active=0
timeout=$(expr $OCF_RESKEY_timeout \* 1000 / $OCF_RESKEY_attempts)
cmd="$p_exe -r $OCF_RESKEY_attempts -t $timeout -B 1.0 $OCF_RESKEY_options $OCF_RESKEY_host_list"
fping_output=$($cmd 2>&1); rc=$?
active=$(echo "$fping_output" | grep "is alive" | wc -l)
case $rc in
0)
if [ $OCF_RESKEY_debug -gt 1 ]; then
ping_conditional_log info "$fping_output"
fi
;;
1)
for h in $(echo "$fping_output" | grep "is unreachable" | awk '{print $1}'); do
ping_conditional_log warn "$h is inactive: $fping_output"
done
;;
*)
ocf_log err "Unexpected result for '$cmd' $rc: $(echo "$fping_output" | tr '\n' ';')"
;;
esac
return $active
}
ping_check() {
active=0
for host in $OCF_RESKEY_host_list; do
p_exe=ping
case $(uname) in
Linux) p_args="-n -q -W $OCF_RESKEY_timeout -c $OCF_RESKEY_attempts";;
Darwin) p_args="-n -q -t $OCF_RESKEY_timeout -c $OCF_RESKEY_attempts -o";;
FreeBSD) p_args="-n -q -t $OCF_RESKEY_timeout -c $OCF_RESKEY_attempts -o";;
*) ocf_log err "Unknown host type: $(uname)"; exit $OCF_ERR_INSTALLED;;
esac
case "$host" in
*:*) p_exe=ping6
esac
ping_output=$($p_exe $p_args $OCF_RESKEY_options $host 2>&1); rc=$?
case $rc in
0)
active=$(expr $active + 1)
if [ $OCF_RESKEY_debug -gt 1 ]; then
ping_conditional_log info "$ping_output"
fi
;;
1) ping_conditional_log warn "$host is inactive: $ping_output";;
*) ocf_log err "Unexpected result for '$p_exe $p_args $OCF_RESKEY_options $host' $rc: $ping_output";;
esac
done
return $active
}
ping_update() {
if use_fping; then
fping_check
active=$?
else
ping_check
active=$?
fi
score=$(expr $active \* $OCF_RESKEY_multiplier)
if [ "$__OCF_ACTION" = "start" ] ; then
attrd_updater -n "$OCF_RESKEY_name" -B "$score" -d "$OCF_RESKEY_dampen"
else
attrd_updater -n "$OCF_RESKEY_name" -v "$score" -d "$OCF_RESKEY_dampen"
fi
rc=$?
case $rc in
0) ping_conditional_log debug "Updated $OCF_RESKEY_name = $score" ;;
*) ocf_log warn "Could not update $OCF_RESKEY_name = $score: rc=$rc";;
esac
if [ $rc -ne 0 ]; then
return $rc
fi
if [ -n "$OCF_RESKEY_failure_score" ] && [ "$score" -lt "$OCF_RESKEY_failure_score" ]; then
ocf_log warn "$OCF_RESKEY_name is less than failure_score($OCF_RESKEY_failure_score)"
return 1
fi
return 0
}
use_fping() {
ocf_is_true "$OCF_RESKEY_use_fping" && have_binary fping;
}
# return values:
# 4 IPv4
# 6 IPv6
# 0 indefinite (i.e. hostname)
host_family() {
case $1 in
*[0-9].*[0-9].*[0-9].*[0-9]) return 4 ;;
*:*) return 6 ;;
*) return 0 ;;
esac
}
# return values same as host_family plus
# 99 ambiguous families
hosts_family() {
# For fping allow only same IP versions or hostnames
family=0
for host in $OCF_RESKEY_host_list; do
host_family "$host"
f=$?
if [ $family -ne 0 ] && [ $f -ne 0 ] && [ $f -ne $family ] ; then
family=99
break
fi
[ $f -ne 0 ] && family=$f
done
return $family
}
integer=$(echo ${OCF_RESKEY_timeout} | egrep -o '[0-9]*')
case "${OCF_RESKEY_timeout}" in
*[0-9]ms|*[0-9]msec) OCF_RESKEY_timeout=$(expr $integer / 1000);;
*[0-9]m|*[0-9]min) OCF_RESKEY_timeout=$(expr $integer \* 60);;
*[0-9]h|*[0-9]hr) OCF_RESKEY_timeout=$(expr $integer \* 60 \* 60);;
*) OCF_RESKEY_timeout=$integer;;
esac
if [ -z "${OCF_RESKEY_timeout}" ]; then
if [ -n "$OCF_RESKEY_host_list" ]; then
host_count=$(echo $OCF_RESKEY_host_list | awk '{print NF}')
OCF_RESKEY_timeout=$(expr $OCF_RESKEY_CRM_meta_timeout / $host_count / $OCF_RESKEY_attempts)
OCF_RESKEY_timeout=$(expr $OCF_RESKEY_timeout / 1100) # Convert to seconds and finish 10% early
else
OCF_RESKEY_timeout=5
fi
fi
if [ ${OCF_RESKEY_timeout} -lt 1 ]; then
OCF_RESKEY_timeout=5
elif [ ${OCF_RESKEY_timeout} -gt 1000 ]; then
# ping actually complains if this value is too high, 5 minutes is plenty
OCF_RESKEY_timeout=300
fi
if [ "${OCF_RESKEY_CRM_meta_globally_unique}" = "false" ]; then
: ${OCF_RESKEY_pidfile:="${HA_VARRUN%%/}/ping-${OCF_RESKEY_name}"}
else
: ${OCF_RESKEY_pidfile:="${HA_VARRUN%%/}/ping-${OCF_RESOURCE_INSTANCE}"}
fi
# Check the debug option
case "${OCF_RESKEY_debug}" in
true|True|TRUE|1) OCF_RESKEY_debug=1;;
false|False|FALSE|0) OCF_RESKEY_debug=0;;
verbose|Verbose|VERBOSE|2) OCF_RESKEY_debug=2;;
*)
ocf_log warn "Value for 'debug' is incorrect. Please specify 'true', 'false', or 'verbose', not: ${OCF_RESKEY_debug}"
OCF_RESKEY_debug=false
;;
esac
case "$__OCF_ACTION" in
meta-data) meta_data
exit $OCF_SUCCESS
;;
start) ping_start;;
stop) ping_stop;;
monitor) ping_monitor;;
validate-all) ping_validate;;
reload-agent) ping_reload_agent;;
usage|help) ping_usage
exit $OCF_SUCCESS
;;
*) ping_usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
exit $?
# vim: set filetype=sh expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=80:
|