getWLS_NUP.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # getWLS_NUP.py v19.1
  2. # This script will:
  3. # * connect WLST to the WLS Domain
  4. # * get a list of users who can access the Domain
  5. # * get a count of the number of authorized users for each Group
  6. #
  7. from weblogic.management.security.authentication import UserReaderMBean
  8. from weblogic.management.security.authentication import GroupReaderMBean
  9. from weblogic.management.security.authentication import GroupMemberListerMBean
  10. from weblogic.management.security.authentication import GroupUserListerMBean
  11. from java.io import File
  12. from java.io import FileOutputStream
  13. import os
  14. import sha
  15. NUP_script_version = "19.1"
  16. def getNUP_Info():
  17. NUP_ERRORS = File(os.environ["CT_TMP"] + os.sep + 'logs' + os.sep + 'WLS_errors.log')
  18. fosNUPerr = FileOutputStream(NUP_ERRORS,true)
  19. NUP_WARNINGS = File(os.environ["CT_TMP"] + os.sep + 'logs' + os.sep + 'WLS_warnings.log')
  20. fosNUPwarn = FileOutputStream(NUP_WARNINGS,true)
  21. NUP_COLLECTED = File(os.environ["CT_TMP"] + os.sep + 'logs' + os.sep + 'WLS_collected.log')
  22. fosNUPcol = FileOutputStream(NUP_COLLECTED,true)
  23. print '==================================='
  24. print ' WLS NUP Measurement'
  25. print '==================================='
  26. targetMachine = sys.argv[1]
  27. while 1:
  28. print
  29. print 'Please enter administrative credentials for the WebLogic domain.'
  30. print
  31. try:
  32. connect()
  33. break
  34. except WLSTException, ex:
  35. WLST_return = str(ex)
  36. if WLST_return.find('authenticated') >= 0:
  37. print '\n\nWLS: CT-03000: ERROR: WLSNUP script failed to authenticate user. Invalid userID or password.'
  38. quitWLST = raw_input('Please press enter to try to connect again with new credentials or q to quit:')
  39. if quitWLST.strip() == 'q':
  40. Failed_output = File(os.environ["CT_TMP"] + os.sep + 'WLSNUP' + os.sep + targetMachine + '_OracleWLS_NUP.txt')
  41. fosFailedOut = FileOutputStream(Failed_output)
  42. theInterpreter.setOut(fosNUPerr)
  43. print 'WLS: CT-03000: ERROR: WLSNUP script failed to authenticate user. Invalid userID or password.'
  44. theInterpreter.setOut(fosFailedOut)
  45. print 'WLS: CT-03000: ERROR: WLSNUP script failed to authenticate user. Invalid userID or password.'
  46. print
  47. print 'End of getWLS_NUP'
  48. fosNUPerr.close()
  49. fosNUPwarn.close()
  50. fosNUPcol.close()
  51. fosFailedOut.close()
  52. return
  53. elif WLST_return.find('performing') >= 0:
  54. print '\n\nWLS: CT-03001: ERROR: WLSNUP script failed to connect to URL. Please check the URL and port and try again.'
  55. quitWLST = raw_input('Please press enter to try to connect again with new credentials or q to quit:')
  56. if quitWLST.strip() == 'q':
  57. Failed_output = File(os.environ["CT_TMP"] + os.sep + 'WLSNUP' + os.sep + targetMachine + '_OracleWLS_NUP.txt')
  58. fosFailedOut = FileOutputStream(Failed_output)
  59. theInterpreter.setOut(fosNUPerr)
  60. print 'WLS: CT-03001: ERROR: WLSNUP script failed to connect to URL. Please check the URL and port and try again.'
  61. theInterpreter.setOut(fosFailedOut)
  62. print 'WLS: CT-03001: ERROR: WLSNUP script failed to connect to URL. Please check the URL and port and try again.'
  63. print
  64. print 'End of getWLS_NUP'
  65. fosNUPerr.close()
  66. fosNUPwarn.close()
  67. fosNUPcol.close()
  68. fosFailedOut.close()
  69. return
  70. ## We need to only get NUP from WebLogic 9.X and later
  71. ver=cmo.getConfigurationVersion()
  72. verElements = ver.split('.')
  73. if eval(verElements[0]) < 9 :
  74. print 'Failed to connect. NUP measurement not supported on WebLogic Version' + ver
  75. theInterpreter.setOut(fosNUPerr)
  76. print 'WLS: CT-03002: ERROR: WLSNUP script failed to connect to URL, this version of WebLogic is not supported by the WLST NUP script.'
  77. theInterpreter.setOut(fosFailedOut)
  78. print 'WLS: CT-03002: ERROR: WLSNUP script failed to connect to URL, this version of WebLogic is not supported by the WLST NUP script.'
  79. print
  80. print 'End of getWLS_NUP'
  81. fosNUPerr.close()
  82. fosNUPwarn.close()
  83. fosNUPcol.close()
  84. fosFailedOut.close()
  85. return
  86. else:
  87. print "WebLogic Version " + ver
  88. ##domainName = cmo.getName()
  89. fBaseName = targetMachine + '_' + domainName + '_OracleWLS_NUP.txt'
  90. print 'Reading users for the domain, ' + domainName + ', the time to complete this command will depend on the number of users.'
  91. NUP_output = File(os.environ["CT_TMP"] + os.sep + 'WLSNUP' + os.sep + fBaseName)
  92. print 'Saving output to ' + str(NUP_output)
  93. fosNUPOut = FileOutputStream(NUP_output)
  94. theInterpreter.setOut(fosNUPOut)
  95. print
  96. print 'WLST Input Information'
  97. print 'Target Machine: ' + targetMachine
  98. print 'MW_HOME: ' + os.environ["MW_HOME"]
  99. print 'Domain Name: ' + domainName
  100. ## Loop through each security realm and each authentication provider and get the user names
  101. try:
  102. securityRealms=cmo.getSecurityConfiguration().getRealms()
  103. for r in securityRealms:
  104. authenticationProviders = r.getAuthenticationProviders()
  105. providerCount = 0
  106. for i in authenticationProviders:
  107. if isinstance(i,GroupReaderMBean):
  108. groupReader = i
  109. groupCursor = i.listGroups("*",0)
  110. groupCount = 0
  111. print 'Security Realm:'+r.getName()+' Provider:'+i.getName() + ' groups and users are: '
  112. while groupReader.haveCurrent(groupCursor):
  113. currGrpName = groupReader.getCurrentName(groupCursor)
  114. usergroup = None
  115. usrCount = 0;
  116. print '\tGroup: ' + currGrpName + ' Users are: '
  117. if isinstance(i,GroupUserListerMBean):
  118. usergroup = i.listAllUsersInGroup(currGrpName,"*",0)
  119. for user in usergroup:
  120. encryptedUser = sha.new(user).hexdigest()
  121. if ( user == 'weblogic' or user == 'OracleSystemUser' or user == 'alsb-system-user' ):
  122. print '\t\t\tWLSNUP_SHA1:default_' + encryptedUser
  123. else:
  124. print '\t\t\tWLSNUP_SHA1:' + encryptedUser
  125. ##print '\t\t\tWLSNUP_SHA1:' + encryptedUser
  126. usrCount = usrCount + 1
  127. elif isinstance(i,GroupMemberListerMBean):
  128. members = i.listGroupMembers(currGrpName,"*",0)
  129. while i.haveCurrent(members):
  130. user=i.getCurrentName(members)
  131. if i.userExists(user):
  132. encryptedUser = sha.new(user).hexdigest()
  133. if ( user == 'weblogic' or user == 'OracleSystemUser' or user == 'alsb-system-user' ):
  134. print '\t\t\tWLSNUP_SHA1:default_' + encryptedUser
  135. else:
  136. print '\t\t\tWLSNUP_SHA1:' + encryptedUser
  137. ##print '\t\t\tWLSNUP_SHA1:' + encryptedUser
  138. usrCount = usrCount + 1
  139. i.advance(members)
  140. i.close(members)
  141. print '\tTotal Users in Group ' + currGrpName + ' are: ' + str(usrCount) + '\n\n'
  142. groupReader.advance(groupCursor)
  143. groupCount = groupCount + 1
  144. groupReader.close(groupCursor)
  145. print 'Total Groups in Authentication Provider ' + i.getName()+' are: '+ str(groupCount)
  146. providerCount = providerCount + 1
  147. print 'Total Authentication Providers in Security Realm ' +r.getName()+' are: '+ str(providerCount)
  148. disconnect()
  149. except WLSTException, ex:
  150. print 'WLST Failed. Could not get NUP Information from Domain ' + domainName + ':' + str(ex)
  151. print 'End of getWLS_NUP'
  152. print
  153. theInterpreter.setOut(fosNUPcol)
  154. print 'WLS: CT-03200: COLLECTED: NUP measurement running on ' + targetMachine + ' Domain ' + domainName
  155. theInterpreter.setOut(fosNUPOut)
  156. print
  157. print 'End of getWLS_NUP'
  158. fosNUPOut.close()
  159. fosNUPerr.close()
  160. fosNUPwarn.close()
  161. fosNUPcol.close()
  162. theInterpreter.setOut(prev)
  163. ## getWLS_nup
  164. ##
  165. prev = theInterpreter.getOut()
  166. getNUP_Info()
  167. while 1:
  168. print '\n\nWould you like to connect to another domain?'
  169. quitDomains = raw_input('Please press enter to connect to a new domain or q to quit:')
  170. if quitDomains.strip() == 'q':
  171. break
  172. else :
  173. getNUP_Info()
  174. theInterpreter.setOut(prev)
  175. print
  176. print 'End of getWLS_NUP'
  177. exit()