~ampelbein/ubuntu/oneiric/heartbeat/lp-770743

« back to all changes in this revision

Viewing changes to tools/utillib.sh

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-10 19:29:25 UTC
  • mfrom: (5.2.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20090810192925-9zy2llcbgavbskf7
Tags: 2.99.2+sles11r9-5ubuntu1
* New upstream snapshot
* Adjusted heartbeat.install and rules for documentation path

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# NB: This is not going to work unless you source /etc/ha.d/shellfuncs!
19
19
 
20
20
#
21
 
# ha.cf/logd.cf parsing
22
 
#
23
 
getcfvar() {
24
 
        [ -f "$HA_CF" ] || return
25
 
        sed 's/#.*//' < $HA_CF |
 
21
# figure out the cluster type, depending on the process list
 
22
# and existence of configuration files
 
23
#
 
24
get_cluster_type() {
 
25
        if ps -ef | grep -qs '[a]isexec' ||
 
26
                        [ -f /etc/ais/openais.conf -a ! -f "$HA_CF" ]
 
27
        then
 
28
                echo "openais"
 
29
        else
 
30
                echo "heartbeat"
 
31
        fi
 
32
}
 
33
#
 
34
# find out which membership tool is installed
 
35
#
 
36
echo_membership_tool() {
 
37
        membership_tools="ccm_tool crm_node"
 
38
        for f in $membership_tools; do
 
39
                which $f 2>/dev/null && break
 
40
        done
 
41
}
 
42
#
 
43
# find nodes for this cluster
 
44
#
 
45
getnodes() {
 
46
        # 1. set by user?
 
47
        if [ "$USER_NODES" ]; then
 
48
                echo $USER_NODES
 
49
        # 2. running crm
 
50
        elif iscrmrunning; then
 
51
                get_crm_nodes
 
52
        # 3. hostcache
 
53
        elif [ -f $HA_VARLIB/hostcache ]; then
 
54
                awk '{print $1}' $HA_VARLIB/hostcache
 
55
        # 4. ha.cf
 
56
        elif [ "$CLUSTER_TYPE" = heartbeat ]; then
 
57
                getcfvar node
 
58
        fi
 
59
}
 
60
 
 
61
logd_getcfvar() {
 
62
        sed 's/#.*//' < $LOGD_CF |
26
63
                grep -w "^$1" |
27
64
                sed 's/^[^[:space:]]*[[:space:]]*//'
28
65
}
29
 
iscfvarset() {
30
 
        test "`getcfvar \"$1\"`"
31
 
}
32
 
iscfvartrue() {
33
 
        getcfvar "$1" |
34
 
                egrep -qsi "^(true|y|yes|on|1)"
35
 
}
36
 
getnodes() {
37
 
        getcfvar node
38
 
}
39
 
 
 
66
get_logd_logvars() {
 
67
        # unless logfacility is set to none, heartbeat/ha_logd are
 
68
        # going to log through syslog
 
69
        HA_LOGFACILITY=`logd_getcfvar logfacility`
 
70
        [ "" = "$HA_LOGFACILITY" ] && HA_LOGFACILITY=$DEFAULT_HA_LOGFACILITY
 
71
        [ none = "$HA_LOGFACILITY" ] && HA_LOGFACILITY=""
 
72
        HA_LOGFILE=`logd_getcfvar logfile`
 
73
        HA_DEBUGFILE=`logd_getcfvar debugfile`
 
74
}
 
75
findlogdcf() {
 
76
        for f in \
 
77
                `which strings > /dev/null 2>&1 &&
 
78
                        strings $HA_BIN/ha_logd | grep 'logd\.cf'` \
 
79
                `for d; do echo $d/logd.cf $d/ha_logd.cf; done`
 
80
        do
 
81
                if [ -f "$f" ]; then
 
82
                        echo $f
 
83
                        return 0
 
84
                fi
 
85
        done
 
86
        return 1
 
87
}
40
88
#
41
89
# logging
42
90
#
51
99
#
52
100
# find log destination
53
101
#
54
 
uselogd() {
55
 
        iscfvartrue use_logd &&
56
 
                return 0  # if use_logd true
57
 
        iscfvarset logfacility ||
58
 
        iscfvarset logfile ||
59
 
        iscfvarset debugfile ||
60
 
                return 0  # or none of the log options set
61
 
        false
62
 
}
63
 
findlogdcf() {
64
 
        for f in \
65
 
                `which strings > /dev/null 2>&1 &&
66
 
                        strings $HA_BIN/ha_logd | grep 'logd\.cf'` \
67
 
                `for d; do echo $d/logd.cf $d/ha_logd.cf; done`
68
 
        do
69
 
                if [ -f "$f" ]; then
70
 
                        echo $f
71
 
                        return 0
72
 
                fi
73
 
        done
74
 
        return 1
75
 
}
76
 
getlogvars() {
77
 
        HA_LOGFACILITY=${HA_LOGFACILITY:-$DEFAULT_HA_LOGFACILITY}
78
 
        if uselogd; then
79
 
                [ -f "$LOGD_CF" ] ||
80
 
                        return  # no configuration: use defaults
81
 
        fi
82
 
        savecf=$HA_CF
83
 
        HA_CF=$LOGD_CF
84
 
        # unless logfacility is set to none, heartbeat/ha_logd are
85
 
        # going to log through syslog
86
 
        HA_LOGFACILITY=`getcfvar logfacility`
87
 
        [ "" = "$HA_LOGFACILITY" ] && HA_LOGFACILITY=$DEFAULT_HA_LOGFACILITY
88
 
        [ none = "$HA_LOGFACILITY" ] && HA_LOGFACILITY=""
89
 
        HA_LOGFILE=`getcfvar logfile`
90
 
        HA_DEBUGFILE=`getcfvar debugfile`
91
 
        HA_CF=$savecf
92
 
}
93
102
findmsg() {
94
103
        # this is tricky, we try a few directories
95
104
        syslogdir="/var/log /var/logs /var/syslog /var/adm /var/log/ha /var/log/cluster"
161
170
                        [ "$tmid" ] && break
162
171
                        warning "cannot extract time: $logf:$mid; will try the next one"
163
172
                        trycnt=$(($trycnt-1))
164
 
                        mid=$(($mid+1))
 
173
                        # shift the whole first-last segment
 
174
                        first=$(($first-1))
 
175
                        last=$(($last-1))
 
176
                        mid=$((($last+$first)/2))
165
177
                done
166
178
                if [ -z "$tmid" ]; then
167
179
                        warning "giving up on log..."
210
222
        from_stamp=""
211
223
}
212
224
find_files() {
213
 
        dir=$1
 
225
        dirs=$1
214
226
        from_time=$2
215
227
        to_time=$3
216
228
        isnumber "$from_time" && [ "$from_time" -gt 0 ] || {
219
231
        }
220
232
        trap find_files_clean 0
221
233
        if ! from_stamp=`touchfile $from_time`; then
222
 
                warning "sorry, can't create temoary file for find_files"
 
234
                warning "sorry, can't create temporary file for find_files"
223
235
                return
224
236
        fi
225
237
        findexp="-newer $from_stamp"
226
238
        if isnumber "$to_time" && [ "$to_time" -gt 0 ]; then
227
239
                if ! to_stamp=`touchfile $to_time`; then
228
 
                        warning "sorry, can't create temoary file for" \
 
240
                        warning "sorry, can't create temporary file for" \
229
241
                                "find_files"
230
242
                        find_files_clean
231
243
                        return
232
244
                fi
233
245
                findexp="$findexp ! -newer $to_stamp"
234
246
        fi
235
 
        find $dir -type f $findexp
 
247
        find $dirs -type f $findexp
236
248
        find_files_clean
237
249
        trap "" 0
238
250
}
239
251
 
240
252
#
 
253
# check permissions of files/dirs
 
254
#
 
255
pl_checkperms() {
 
256
perl -e '
 
257
# check permissions and ownership
 
258
# uid and gid are numeric
 
259
# everything must match exactly
 
260
# no error checking! (file should exist, etc)
 
261
($filename, $perms, $in_uid, $in_gid) = @ARGV;
 
262
($mode,$uid,$gid) = (stat($filename))[2,4,5];
 
263
$p=sprintf("%04o", $mode & 07777);
 
264
$p ne $perms and exit(1);
 
265
$uid ne $in_uid and exit(1);
 
266
$gid ne $in_gid and exit(1);
 
267
' $*
 
268
}
 
269
num_id() {
 
270
        getent $1 $2 | awk -F: '{print $3}'
 
271
}
 
272
chk_id() {
 
273
        [ "$2" ] && return 0
 
274
        echo "$1: id not found"
 
275
        return 1
 
276
}
 
277
check_perms() {
 
278
        essential_files |
 
279
        while read type f p uid gid; do
 
280
                [ -$type $f ] || {
 
281
                        echo "$f wrong type or doesn't exist"
 
282
                        continue
 
283
                }
 
284
                n_uid=`num_id passwd $uid`
 
285
                chk_id "$uid" "$n_uid" || continue
 
286
                n_gid=`num_id group $gid`
 
287
                chk_id "$gid" "$n_gid" || continue
 
288
                pl_checkperms $f $p $n_uid $n_gid || {
 
289
                        echo "wrong permissions or ownership for $f:"
 
290
                        ls -ld $f
 
291
                }
 
292
        done
 
293
}
 
294
 
 
295
#
241
296
# coredumps
242
297
#
243
298
findbinary() {
245
300
        binary=`gdb $random_binary $1 < /dev/null 2>/dev/null |
246
301
                grep 'Core was generated' | awk '{print $5}' |
247
302
                sed "s/^.//;s/[.':]*$//"`
 
303
        if [ x = x"$binary" ]; then
 
304
                binary=$(file $1 | awk '/from/{
 
305
                        for( i=1; i<=NF; i++ )
 
306
                                if( $i == "from" ) {
 
307
                                        print $(i+1)
 
308
                                        break
 
309
                                }
 
310
                        }')
 
311
                binary=`echo $binary | tr -d "'"`
 
312
                binary=$(echo $binary | tr -d '`')
 
313
                if [ "$binary" ]; then
 
314
                        binary=`which $binary 2>/dev/null`
 
315
                fi
 
316
        fi
248
317
        [ x = x"$binary" ] && return
249
318
        fullpath=`which $binary 2>/dev/null`
250
319
        if [ x = x"$fullpath" ]; then
273
342
# heartbeat configuration/status
274
343
#
275
344
iscrmrunning() {
276
 
        crmadmin -D >/dev/null 2>&1 &
 
345
        ps -ef | grep -qs [c]rmd || return 1
 
346
        #crmadmin -D >/dev/null 2>&1 &
 
347
        crm_mon -1 >/dev/null 2>&1 &
277
348
        pid=$!
278
349
        maxwait=10
279
350
        while kill -0 $pid 2>/dev/null && [ $maxwait -gt 0 ]; do
290
361
dumpstate() {
291
362
        crm_mon -1 | grep -v '^Last upd' > $1/$CRM_MON_F
292
363
        cibadmin -Ql > $1/$CIB_F
293
 
        ccm_tool -p > $1/$CCMTOOL_F 2>&1
 
364
        `echo_membership_tool` $MEMBERSHIP_TOOL_OPTS -p > $1/$MEMBERSHIP_F 2>&1
294
365
}
295
366
getconfig() {
296
 
        [ -f "$HA_CF" ] &&
297
 
                cp -p $HA_CF $1/
 
367
        [ -f "$CONF" ] &&
 
368
                cp -p $CONF $1/
298
369
        [ -f "$LOGD_CF" ] &&
299
370
                cp -p $LOGD_CF $1/
300
371
        if iscrmrunning; then
304
375
                cp -p $HA_VARLIB/crm/$CIB_F $1/ 2>/dev/null
305
376
                touch $1/STOPPED
306
377
        fi
307
 
        cp -p $HA_VARLIB/hostcache $1/ 2>/dev/null
 
378
        [ "$HOSTCACHE" ] &&
 
379
                cp -p $HA_VARLIB/hostcache $1/$HOSTCACHE 2>/dev/null
 
380
        [ "$HB_UUID_F" ] &&
 
381
                crm_uuid -r > $1/$HB_UUID_F 2>&1
308
382
        [ -f "$1/$CIB_F" ] &&
309
383
                crm_verify -V -x $1/$CIB_F >$1/$CRM_VERIFY_F 2>&1
 
384
        [ -f "$1/$CIB_F" ] && which crm >/dev/null 2>&1 &&
 
385
                CIB_file=$1/$CIB_F crm configure show >$1/$CIB_TXT_F 2>&1
 
386
 
 
387
}
 
388
get_crm_nodes() {
 
389
        cibadmin -Ql -o nodes |
 
390
        awk '
 
391
        /type="normal"/ {
 
392
                for( i=1; i<=NF; i++ )
 
393
                        if( $i~/^uname=/ ) {
 
394
                                sub("uname=.","",$i);
 
395
                                sub(".$","",$i);
 
396
                                print $i;
 
397
                                next;
 
398
                        }
 
399
        }
 
400
        '
310
401
}
311
402
 
312
403
#
407
498
                        }
408
499
                done
409
500
        }
410
 
        warning "no lsb_release no /etc/*-release no /etc/debian_version"
 
501
        warning "no lsb_release, no /etc/*-release, no /etc/debian_version: no distro information"
411
502
}
412
 
hb_ver() {
 
503
pkg_ver() {
413
504
        # for Linux .deb based systems
414
 
        which dpkg > /dev/null 2>&1 && {
415
 
                for pkg in heartbeat heartbeat-2; do
416
 
                        dpkg-query -f '${Version}' -W $pkg 2>/dev/null && break
417
 
                done
418
 
                [ $? -eq 0 ] &&
419
 
                        debsums -s $pkg 2>/dev/null
420
 
                return
421
 
        }
422
 
        # for Linux .rpm based systems
423
 
        which rpm > /dev/null 2>&1 && {
424
 
                rpm -q --qf '%{version}-%{release}' heartbeat &&
425
 
                echo &&
426
 
                rpm --verify heartbeat
427
 
                return
428
 
        }
429
 
        # for OpenBSD
430
 
        which pkg_info > /dev/null 2>&1 && {
431
 
                pkg_info | grep heartbeat | cut -d "-" -f 2- | cut -d " " -f 1
432
 
                return
433
 
        }
434
 
        # for Solaris
435
 
        which pkginfo > /dev/null 2>&1 && {
436
 
                pkginfo | awk '{print $3}'
437
 
        }
438
 
        # more packagers?
 
505
        for pkg; do
 
506
                which dpkg > /dev/null 2>&1 && {
 
507
                        dpkg-query -f '${Name} ${Version}' -W $pkg 2>/dev/null && break
 
508
                        [ $? -eq 0 ] &&
 
509
                                debsums -s $pkg 2>/dev/null
 
510
                        break
 
511
                }
 
512
                # for Linux .rpm based systems
 
513
                which rpm > /dev/null 2>&1 && {
 
514
                        rpm -q --qf '%{name} %{version}-%{release}' $pkg &&
 
515
                        echo &&
 
516
                        rpm --verify $pkg
 
517
                        break
 
518
                }
 
519
                # for OpenBSD
 
520
                which pkg_info > /dev/null 2>&1 && {
 
521
                        pkg_info | grep $pkg
 
522
                        break
 
523
                }
 
524
                # for Solaris
 
525
                which pkginfo > /dev/null 2>&1 && {
 
526
                        pkginfo | awk '{print $3}'  # format?
 
527
                }
 
528
                # more packagers?
 
529
        done
439
530
}
440
531
crm_info() {
441
532
        $HA_BIN/crmd version 2>&1