Exemplo n.º 1
0
def GetRegistry(hostIn="localhost", userIn="root", pwdIn="ca$hc0w"):
    # Connect
    si = SmartConnect(host=hostIn, user=userIn, pwd=pwdIn)
    atexit.register(Disconnect, si)

    stub = GetStub()
    registry = Hostd.StatsRegistryManager('ha-internalsvc-statsregistrymgr',
                                          stub)
    if registry is None:
        print('failed to get stats registry object')

    return registry
Exemplo n.º 2
0
def GetRegistryStats(perfManager, hostSystem):
    # Query registry stats
    cntrlblCntrs = perfManager.QueryPerfCounterInt()
    if cntrlblCntrs is None:
        Log("ERROR: Failed to get perfCounterInt through vim.PerformanceManager.QueryPerfCounterInt"
            )
        exit(1)

    stub = GetStub()
    registry = Hostd.StatsRegistryManager('ha-internalsvc-statsregistrymgr',
                                          stub)
    if registry is None:
        Log("ERROR: Failed to get stats registry object")
        exit(1)
    statsRegistryStats = registry.QueryStatList()
    cntrlblCntrsIds = []
    metricIds = []

    # prepare a list of all controllable counters
    for cntr in cntrlblCntrs:
        cntrlblCntrsIds.append(cntr.key)
        metricId = vim.PerformanceManager.MetricId()
        metricId.counterId = cntr.key
        metricId.instance = "*"
        metricIds.append(metricId)

    querySpec = vim.PerformanceManager.QuerySpec()
    querySpec.entity = hostSystem
    querySpec.format = vim.PerformanceManager.Format.normal
    querySpec.metricId = metricIds

    perfManager.EnableStat(cntrlblCntrsIds)
    # We should be able to obtain some data for all controllable counters
    # after 20 seconds, which is the default collection interval
    time.sleep(20 + 2)
    result = perfManager.QueryStats([querySpec])
    return GetValidStatsFromEntity(result)
Exemplo n.º 3
0
def get_service_instance(host,
                         username=None,
                         password=None,
                         protocol=None,
                         port=None,
                         mechanism='userpass',
                         principal=None,
                         domain=None):
    '''
    Authenticate with a vCenter server or ESX/ESXi host and return the service instance object.

    host
        The location of the vCenter server or ESX/ESXi host.

    username
        The username used to login to the vCenter server or ESX/ESXi host.
        Required if mechanism is ``userpass``

    password
        The password used to login to the vCenter server or ESX/ESXi host.
        Required if mechanism is ``userpass``

    protocol
        Optionally set to alternate protocol if the vCenter server or ESX/ESXi host is not
        using the default protocol. Default protocol is ``https``.

    port
        Optionally set to alternate port if the vCenter server or ESX/ESXi host is not
        using the default port. Default port is ``443``.

    mechanism
        pyVmomi connection mechanism. Can either be ``userpass`` or ``sspi``.
        Default mechanism is ``userpass``.

    principal
        Kerberos service principal. Required if mechanism is ``sspi``

    domain
        Kerberos user domain. Required if mechanism is ``sspi``
    '''

    if protocol is None:
        protocol = 'https'
    if port is None:
        port = 443

    service_instance = GetSi()
    if service_instance:
        stub = GetStub()
        if salt.utils.is_proxy() or (hasattr(
                stub, 'host') and stub.host != ':'.join([host, str(port)])):
            # Proxies will fork and mess up the cached service instance.
            # If this is a proxy or we are connecting to a different host
            # invalidate the service instance to avoid a potential memory leak
            # and reconnect
            Disconnect(service_instance)
            service_instance = None
        else:
            return service_instance

    if not service_instance:
        service_instance = _get_service_instance(host, username, password,
                                                 protocol, port, mechanism,
                                                 principal, domain)

    # Test if data can actually be retrieved or connection has gone stale
    log.trace('Checking connection is still authenticated')
    try:
        service_instance.CurrentTime()
    except vim.fault.NotAuthenticated:
        log.trace('Session no longer authenticating. Reconnecting')
        Disconnect(service_instance)
        service_instance = _get_service_instance(host, username, password,
                                                 protocol, port, mechanism,
                                                 principal, domain)

    return service_instance
Exemplo n.º 4
0
def Main():
    # Process command line
    options = GetOptions()

    si = SmartConnect(host=options.host,
                      user=options.user,
                      pwd=options.password)
    atexit.register(Disconnect, si)

    content = si.RetrieveContent()
    rootFolder = content.GetRootFolder()
    dataCenter = rootFolder.GetChildEntity()[0]
    hostFolder = dataCenter.GetHostFolder()
    compResrce = hostFolder.GetChildEntity()[0]
    localHost = compResrce.GetHost()[0]
    prfrmncMngr = content.perfManager

    cntrlblCntrs = prfrmncMngr.GetPerfCounterInt()

    if cntrlblCntrs is None:
        print(
            "ERROR: Failed to get perfCounterInt through vim.PerformanceManager.GetCounterInfoInt"
        )
        exit(1)

    namedCntrlblCntrs = {}
    for cntr in cntrlblCntrs:
        name = cntr.groupInfo.key + "." + cntr.nameInfo.key
        namedCntrlblCntrs[name] = cntr

    stub = GetStub()
    registry = Hostd.StatsRegistryManager('ha-internalsvc-statsregistrymgr',
                                          stub)
    if registry is None:
        print("ERROR: Failed to get stats registry object")
        exit(1)

    statsRegistryStats = registry.QueryStatList()

    #
    # Check whether all valid StatsRegistry stats are in perfCounterInt property
    #
    checked = {}
    for srStat in statsRegistryStats:
        attribs = srStat.statDef.attribute
        if len(attribs) <= 1 or (len(attribs) == 2 and
                                 (attribs[0].type == "vm"
                                  or attribs[0].type == "resPool")):
            name = srStat.statDef.name
            found = namedCntrlblCntrs[name]
            if found is None:
                print(
                    "ERROR: StatsRegistry stat %s is not present in vim.PerformanceManager"
                    % name)
                exit(1)
            if name in checked:
                continue
            checked[name] = name
            if srStat.statDef.unit != found.unitInfo.key:
                print(
                    "ERROR: StatsRegistry stat %s is different in vim.PerformanceManager"
                    % name)
                print(" INFO: Wrong unit - expected '%s', but got '%s'" %
                      (srStat.statDef.unit, found.unitInfo.key))
                exit(1)
            # -2147483648 == 0x80000000 in int32 type
            expectedCounterId = srStat.id - 2147483648
            if expectedCounterId != found.key:
                print(
                    "ERROR: StatsRegistry stat %s is different in vim.PerformanceManager"
                    % name)
                print(" INFO: Wrong id - expected '%s', but got '%s'" %
                      (expectedCounterId, found.key))
                exit(1)

    cntrlblCntrsIds = []
    metricIds = []

    # prepare a list of our controllable counters
    for cntr in cntrlblCntrs:
        cntrlblCntrsIds.append(cntr.key)
        metricId = Vim.PerformanceManager.MetricId()
        metricId.counterId = cntr.key
        metricId.instance = "*"
        metricIds.append(metricId)

    querySpec = Vim.PerformanceManager.QuerySpec()
    querySpec.entity = localHost
    querySpec.maxSample = 1
    querySpec.intervalId = 20
    querySpec.format = Vim.PerformanceManager.Format.normal
    querySpec.metricId = metricIds

    #
    # Enable all possible counters
    #
    prfrmncMngr.EnableStat(cntrlblCntrsIds)

    #
    # We should be able to obtain some data for all controllable counters
    # after 20 seconds, which is the default collection interval
    #
    time.sleep(20 + 2)
    result = prfrmncMngr.QueryStats([querySpec])

    for metric in result:
        for value in metric.value:
            idx = len(value.value) - 1
            val = value.value[idx]
            if val == -1:
                print("ERROR: Stat with counterId:%d and instance:'%s' "
                      "for entity '%s' is still disabled" %
                      (value.id.counterId, value.id.instance, metric.entity))
                print(
                    " INFO: Wrong last value - expected value different from -1 "
                    "for time-stamp:'%s'" % (metric.sampleInfo[idx].timestamp))
                exit(1)

    #
    # Disable all possible counters
    #
    prfrmncMngr.DisableStat(cntrlblCntrsIds)

    #
    # The controllable counters should be disabled now and after 20 more
    # seconds we should see empty samples (-1s)
    #
    time.sleep(20 + 2)
    result = prfrmncMngr.QueryStats([querySpec])

    for metric in result:
        for value in metric.value:
            idx = len(value.value) - 1
            val = value.value[idx]
            if val != -1:
                print("ERROR: Stat with counterId:%d and instance:'%s' "
                      "for entity '%s' is still enabled" %
                      (value.id.counterId, value.id.instance, metric.entity))
                print("INFO: Wrong last value - expected -1, but got %d "
                      "for time-stamp:'%s'" %
                      (val, metric.sampleInfo[idx].timestamp))
                exit(1)

    print("SUCCESS!")