Friday, March 6, 2015

Python script for multipath configuration creation

import os
A = []
B = []
C = 0
LUNPATH = set()
M_LUNPATH = set()
HOST_DEVICE = set()
HOST_DEVICE_LIST = []
HOST_STRING = "Host Device:"
HOST_LUN = {}
p = os.popen('sanlun lun show all', "r")
while 1:
        line = p.readline()
        if not line: break
        #print line
        A = line.split()
        #print len(A)
        if len(A) > 2 :
                controller = A[0]
                lunpathname = A[1]
                print lunpathname, controller
                if lunpathname == "device": continue
                if lunpathname == "lun-pathname": continue
                LUNPATH.add(lunpathname)

                p2 = os.popen('sanlun lun show -p '+controller+":"+lunpathname, "r")
                while 1:
                        line2 = p2.readline()
                        if not line2: break
                        if line2.find(HOST_STRING) != -1 :
                                print line2
                                B = line2.split()
                                print len(B)
                                C = len(B)-1
                                print B[C]
                                HOST_DEVICE.add(B[C])
                                #HOST_DEVICE_LIST.append(B[C])
                                HOST_LUN.update( {B[C]:lunpathname} )



print LUNPATH
for i in LUNPATH:
        # print i
        i = i.replace("/vol/","") # get rid of /vol/
        i = i.replace("/","_") # replace / with _
        M_LUNPATH.add(i)

for i in M_LUNPATH:
        print i

#HOST_DEVICE_LIST.sort()

#for i in HOST_DEVICE_LIST:
#       print i
#       HOST_DEVICE.add(i)

for i in HOST_DEVICE:
        print i

print HOST_LUN

print "mpathconf --enable --with_multipathd y"
print "multipaths {"
for i in HOST_DEVICE:
        #print i
        #print HOST_LUN.get(i)
        j = HOST_LUN.get(i)
        j = j.replace("/vol/","") # get rid of /vol/
        j = j.replace("/","_") # replace / with _
        print "         multipath {"
        print "                 wwid    " + i
        print "                 alias   " + j
        print "         }"

print "}"

print "service multipathd reload"

No comments:

Post a Comment