[Linux-HA] INFO: A script to use for finding resources

Doug Knight dknight at wsi.com
Tue Sep 18 07:29:40 MDT 2007


All,
I recently had the need to be able to intelligently run crontab tasks in
a heartbeat cluster, so that they would only execute the task required
on the node where the heartbeat-managed resource was located. I wanted
to script to key off of a known nvpair that would always be (or most
likely be) within a particular resource's primitive tag, retrieve the
resource name, and then determine if it is running locally. Finally it
would return an exit status I could use from within the crontab script.
The below is written in Python. Feel free to use and modify, thought it
might be useful. I put extra print statements in to illustrate other
info that can be obtained from the xml.

#!/usr/bin/python
import xml.dom.minidom
from xml import xpath
import os, sys, string

argc = len(sys.argv)
if argc != 2:
    print "Need cib.xml nvpair name to check"
    sys.exit(-1)

nvpair = sys.argv[1]

os.system("/usr/sbin/cibadmin -Q > cibadmin.xml")

doc=xml.dom.minidom.parse("cibadmin.xml")
os.system("rm -f cibadmin.xml")

nv_list = xpath.Evaluate('//nvpair[@name="%s"]' % nvpair,doc)

if nv_list == []:
    print "Invalid nvpair name given, not found"
    sys.exit(-1)

nv_resource_node = nv_list[0]
print nv_resource_node.getAttribute("name")
print nv_resource_node.getAttribute("id")
print nv_resource_node.getAttribute("value")
print nv_resource_node.nodeName

tmpParent = nv_resource_node.parentNode
while tmpParent != None and tmpParent.nodeName != "primitive":
    tmpParent = tmpParent.parentNode

if tmpParent != None:
    print tmpParent.getAttribute("id")
    resource_name = tmpParent.getAttribute("id")
else:
    print "Did not find primitive parent"
    doc.unlink()
    sys.exit(-1)

doc.unlink()

resource_location=os.popen("/usr/sbin/crm_resource -W -r %s" %
resource_name).readline().strip()
print "resource location is :%s:" % resource_location
localhost_name=os.popen("uname -n").readline().strip()
print "local hostname is :%s:" % localhost_name
is_running_locally=resource_location.find(localhost_name)
if is_running_locally == -1:
    print "Not running CRONTAB here, resource is elsewhere"
    sys.exit(1)
else:
    print "Running CRONTAB here, resource is local"
    sys.exit(0)


An example of using it is:

#!/bin/sh
/u2/cfdb/bin/parse_cib.py pgdata
resource_status=$?
if [ $resource_status != 0 ]; then
    echo "Resource not local, exiting"
    exit 1
else
    echo "Resource is local, continuing"
fi




More information about the Linux-HA mailing list