def _GetVsanStub(stub, endpoint=VSAN_API_VC_SERVICE_ENDPOINT, context=None, version='vim.version.version10'): hostname = stub.host.split(':')[0] vsanStub = SoapStubAdapter(host=hostname, path=endpoint, version=version, sslContext=context) vsanStub.cookie = stub.cookie return vsanStub
def ConnectEAM(si, vpxdStub, sslContext): hostname = vpxdStub.host.split(":")[0] eamStub = SoapStubAdapter(host=hostname, version="eam.version.version1", path="/eam/sdk", poolSize=0, sslContext=sslContext) eamStub.cookie = vpxdStub.cookie eamCx = eam.EsxAgentManager("EsxAgentManager", eamStub) return eamCx
def connectToSpbm(stub, context): hostname = stub.host.split(":")[0] sessionCookie = stub.cookie.split('"')[1] VmomiSupport.GetRequestContext()["vcSessionCookie"] = sessionCookie pbmStub = SoapStubAdapter( host=hostname, path="/pbm/sdk", version="pbm.version.version2", sslContext=context, ) pbmStub.cookie = stub.cookie pbmSi = pbm.ServiceInstance("ServiceInstance", pbmStub) return pbmSi
def processRequest(vcIp, vcUser, vcPass, clusterName, goal, startTime, endTime): """ Process the user request with provided credentials """ (si, content) = connectToVimEndpoint(vcIp, vcUser, vcPass) cluster = getClusterInstance(clusterName, si) vhStub = SoapStubAdapter(host=vcIp, version="vsan.version.version7", path="/vsanHealth", poolSize=0) vhStub.cookie = si._stub.cookie vpm = vim.cluster.VsanPerformanceManager('vsan-performance-manager', vhStub) #list of possible diagnostic exceptions exceptionDict = getDiagnosticExceptions(vpm) f = open('result.txt', 'w') if startTime is None: # In this case query for the last hour endTime = datetime.datetime.utcnow() startTime = endTime - datetime.timedelta(0, 3600) diagQueryType = vim.cluster.VsanPerfDiagnosticQueryType(goal) diagQuery = vim.cluster.VsanPerfDiagnoseQuerySpec(startTime=startTime, endTime=endTime, queryType=diagQueryType) print("Starting diagnosis. Will append results in result.txt") beginTime = time.time() try: task = vpm.VsanPerfDiagnoseTask(diagQuery, cluster) vcTask = convertVsanTaskToVcTask(task, si._stub) WaitForTask(vcTask) time.sleep(1) transactionId = vcTask.info.result result = vpm.GetVsanPerfDiagnosisResult(vcTask, cluster) resultList = processResult(exceptionDict, result) for item in resultList: f.write("Issue: %s\nDetails: %s\nURL: %s\nResult: %s\n" % (item[0][0], item[0][1], item[0][2], item[1])) duration = time.time() - beginTime print("Time to run performance diagnostics query is %d seconds. Result" " in result.txt" % duration) except vim.fault.NotFound as ex: logging.error(ex) f.close()
def _GetVsanStub(stub, endpoint=VSAN_API_VC_SERVICE_ENDPOINT, context=None, version='vim.version.version11'): index = stub.host.rfind(':') if valid_ipv6(stub.host[:index][1:-1]): hostname = stub.host[:index][1:-1] else: hostname = stub.host[:index] vsanStub = SoapStubAdapter(host=hostname, path=endpoint, version=version, sslContext=context) vsanStub.cookie = stub.cookie return vsanStub
def connectToServers(hostConnect=False): """ Creates connections to the vCenter/ESXi, vSAN and DPS/DPD server @param hostConnect Host connection. If set to True, connections are made to ESXi and vSAN/Data Protection daemon running in it. If set to False, connections are made to vCenter and vSAN/Data Protection service running in it @return vCenter/ESXi server instance, vSAN stub and DPS/DPD stub """ # For python 2.7.9 and later, the default SSL context has stricter # connection handshaking rule, hence we are turning off the hostname checking # and client side cert verification. sslContext = None if sys.version_info[:3] > (2, 7, 8): sslContext = ssl.create_default_context() sslContext.check_hostname = False sslContext.verify_mode = ssl.CERT_NONE if hostConnect: # Create connection to ESXi host to retrieve clone ticket used for login to Data Protection Daemon (DPD) hostIp = input('Enter ESXi host IP: ') hostUserName = input('Enter ESXi host username: '******'Enter ESXi host password: '******'vsan'), sslContext=sslContext) vsanStub.cookie = vsphereInstance._stub.cookie except Exception as e: msg = "ERROR: Could not connect to vSAN service running on ESXi. Exception thrown is \n {0}".format( e) sys.exit(msg) hostSessionManager = vsphereInstance.content.sessionManager cloneTicket = hostSessionManager.AcquireCloneTicket() # Create stub to DPD and login vsandpStub = SoapStubAdapter(host=hostIp, port=PORT, path=VSAN_DP_PATH, version=VSAN_DP_VERSION, sslContext=sslContext) dpdSessionManager = vim.vsandp.host.SessionManager( 'vsan-dp-session-manager', vsandpStub) dpdSessionManager.UserLoginByCloneTicket(cloneTicket, None) atexit.register(dpdSessionManager.UserLogout) else: vcIp = input("Enter vCenter IP address: ") username = input('Enter vCenter username: '******'Enter vCenter password: '******'@' + domainName # Connect to vCenter try: vsphereInstance = SmartConnect(host=vcIp, user=username, pwd=password, port=PORT, path=VSPHERE_PATH, sslContext=sslContext) except vim.InvalidLogin: sys.exit("ERROR: Incorrect login provided for vCenter") except Exception as e: msg = "ERROR: Could not connect to vCenter. Exception thrown is \n {0}".format( e) sys.exit(msg) atexit.register(Disconnect, vsphereInstance) # Create vSAN stub try: vsanStub = SoapStubAdapter( host=vcIp, port=PORT, path=VSAN_VC_PATH, version=VmomiSupport.newestVersions.Get('vsan'), sslContext=sslContext) vsanStub.cookie = vsphereInstance._stub.cookie except Exception as e: msg = "ERROR: Could not connect to vCenter side vSAN server. Exception thrown is \n {0}".format( e) sys.exit(msg) try: # Create stub to Data Protection Service (DPS) vsandpStub = SoapStubAdapter(host=vcIp, port=PORT, path=VSAN_DP_PATH, version=VSAN_DP_VERSION, sslContext=sslContext) authenticator = sso.SsoAuthenticator( sts_url=STS_URL_TEMPLATE % vcIp + domainName) bearer = authenticator.get_bearer_saml_assertion( username, password, delegatable=True, token_duration=SAML_TOKEN_DURATION, ssl_context=sslContext) vsandpStub.samlToken = bearer sessionManager = vim.vsandp.dps.SessionManager( 'vsan-dp-session-manager', vsandpStub) sessionManager.VsanDpsLoginByToken( 'en') # User can login using non-English locale if needed except Exception as e: msg = "ERROR: Could not connect to DPS server. Exception thrown is \n {0}".format( e) sys.exit(msg) atexit.register(sessionManager.VsanDpsLogout) return (vsphereInstance, vsanStub, vsandpStub)