Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify  

# it under the terms of the GNU Lesser General Public License as published  

# by the Free Software Foundation; version 2.1 only. 

# 

# This program is distributed in the hope that it will be useful,  

# but WITHOUT ANY WARRANTY; without even the implied warranty of  

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  

# GNU Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# MPP LUNs path status update daemon 

 

import sys 

import os 

import util 

import glob 

import time 

import mpp_mpathutil 

import gc 

import mpp_luncheck 

 

DEBUG_OUT = False 

DAEMONISE = True 

VERSION = "v1.0.1" 

MPP_PATH_STATUS_UPDATE_INTERVAL = 60 

MPP_DEVICES_IN_USE_LOCATION = "/dev/disk/mpInuse" 

MPP_PATH_KEY_PREFIX = "mpath-" 

MPP_PATH_CACHE_LOCATION = "/tmp" 

 

def help(): 

    print "updatempppathd version %s:\n" % VERSION 

    print "Usage: updatempppathd -f : run in foreground" 

    print "                      -d : send debug output to SMlog" 

    sys.exit(-1) 

 

def DEBUG(str): 

    if DEBUG_OUT: 

        util.SMlog(str, ident="updatempppathd", priority=util.LOG_DEBUG) 

 

# Main update paths routine 

def UpdatePaths(): 

 

    while(True): 

        try: 

            session = None 

            # Garbage collect any memory allocated in the last run of the loop 

            DEBUG("The garbage collection routine returned: %d" % gc.collect()) 

 

            # Sleep for some time before checking the status again 

            time.sleep(MPP_PATH_STATUS_UPDATE_INTERVAL); 

 

            # List the contents of the directory /dev/disk/mpInuse 

            fileList = glob.glob(MPP_DEVICES_IN_USE_LOCATION + "/" + "*"); 

            if not len(fileList): 

                continue 

 

            # for each SCSI ID get the cached values of the total paths and active paths  

            # and then compare this with the current path status obtained using mpp_mpathutil.py 

            for filename in fileList: 

                # extract the SCSI ID from the file name.  

                scsiid = filename.rsplit("/")[len(filename.rsplit("/")) - 1].split('-')[0] 

                if not (mpp_luncheck.is_RdacLun(scsiid)): 

                    continue 

 

                # Get the cached value for the total and active paths for this SCSI ID 

                try: 

                    cacheFile = glob.glob(MPP_PATH_CACHE_LOCATION + "/" + scsiid + "*"); 

                    if len(cacheFile) > 1: 

                        DEBUG("More than one cache file found for SCSI ID %s. Please check the cache manually.") 

                        raise Exception 

 

                    # This will return only one file name of the form SCSIID:TOTALPATHS:ACTIVEPATHS, so parse accordingly 

                    cachedTotalPaths = cacheFile[0].split(":")[1]; 

                    cachedActivePaths = cacheFile[0].split(":")[2]; 

                    cacheFileFound = True 

                except: 

                    DEBUG("There was an exception getting the cached path status for SCSI ID %s, assuming 0s." % scsiid) 

                    cachedTotalPaths = 0 

                    cachedActivePaths = 0 

                    cacheFileFound = False 

 

                (totalPaths, activePaths) = mpp_mpathutil.get_pathinfo(scsiid) 

 

                DEBUG("For SCSI ID %s, cached TotalPaths: %s, cached ActivePaths: %s, New Totalpaths: %s New ActivePaths: %s" % (scsiid, cachedTotalPaths, cachedActivePaths, totalPaths, activePaths)) 

 

                if cachedTotalPaths != str(totalPaths) or cachedActivePaths != str(activePaths): 

                    DEBUG("Some path status has changed for SCSI ID %s, updating PBD." % scsiid) 

                    entry = "[" + str(activePaths) + ", " + str(totalPaths) + "]" 

                    DEBUG(entry) 

                    cmd = ['/opt/xensource/sm/mpathcount.py', scsiid, entry] 

                    util.pread2(cmd) 

 

                    # Now update the cache with this updated path status 

                    DEBUG("Checking if cache file was found.") 

                    if cacheFileFound == True: 

                        DEBUG("Cache file was found, delete it.") 

                        os.remove(cacheFile[0]) 

                    cacheFileName = MPP_PATH_CACHE_LOCATION + "/" + scsiid + ":" + str(totalPaths) + ":" + str(activePaths) 

                    DEBUG("Generated new cache file name %s" % cacheFileName) 

                    open(cacheFileName, 'w').close() 

 

        except: 

            pass 

            #DEBUG("There was some exception while updating path status for the SCSI ID %s." % scsiid ) 

            #break 

 

        if session != None: 

            session.xenapi.session.logout() 

 

    return 

 

# Test Cmdline args 

if len(sys.argv) > 1: 

    for i in range(1,len(sys.argv)): 

        if sys.argv[i] == "-f": 

            DAEMONISE = False 

        elif sys.argv[i] == "-d": 

            DEBUG_OUT = True 

            try: 

                DEBUG("UPDATEMPPPATHD - Daemon started (%s)" % VERSION) 

            except: 

                print "Logging failed" 

                help() 

 

# Daemonize 

if DAEMONISE: 

    util.daemon() 

 

try: 

    UpdatePaths(); 

 

except: 

    pass 

 

DEBUG("UPDATEMPPPATHD - Daemon halted")