PyVmomi 使用示例


PyVmomi: VMware vSphere Python SDK


一、OverView

重點知識:
  1、view_type = [vim.VirtualMachine]
  2、content.viewManager.CreateContainerView()
  3、child.summary.config
def print_vm_info(virtual_machine):
    """
    Print information for a particular virtual machine or recurse into a
    folder with depth protection
    """
    summary = virtual_machine.summary
    print("Name       : ", summary.config.name)
    print("Template   : ", summary.config.template)
    print("Path       : ", summary.config.vmPathName)
    print("Guest      : ", summary.config.guestFullName)
    print("Instance UUID : ", summary.config.instanceUuid)
    print("Bios UUID     : ", summary.config.uuid)
    print("State      : ", summary.runtime.powerState)

    # 判斷是否有注釋
    annotation = summary.config.annotation
    if annotation:
        print("Annotation : ", annotation)

    # 打印Guest OS內的信息
    if summary.guest is not None:
        ip_address = summary.guest.ipAddress
        tools_version = summary.guest.toolsStatus
        if tools_version is not None:
            print("VM-tools: ", tools_version)
        else:
            print("V-tools: None")
        if ip_address:
            print("IP         : ", ip_address)
        else:
            print("IP         : None")



content = service_instance.RetrieveContent()  # 拿到vCenter的內容對象
container = content.rootFolder  # starting point to look into
view_type = [vim.VirtualMachine]  # object types to look for
recursive = True  # whether we should look into it recursively
container_view = content.viewManager.CreateContainerView(container, view_type, recursive)

children = container_view.view
for child in children:
    print_vm_info(child)

 

二、虛擬機狀態、配置信息

1、runtime 虛擬機的運行狀態

runtime = (vim.vm.RuntimeInfo) {
      dynamicType = <unset>,
      host = 'vim.HostSystem:host-34',
      connectionState = 'connected',
      powerState = 'poweredOn',  // 虛擬機電源狀態
      faultToleranceState = 'notConfigured',  // 是否配置FT
      dasVmProtection = <unset>,
      toolsInstallerMounted = false,
      suspendTime = <unset>,
      bootTime = 2017-08-26T06:31:27.543474Z,
      suspendInterval = 0,
      question = <unset>,
      memoryOverhead = <unset>,
      maxCpuUsage = 2808,
      maxMemoryUsage = 891,
      numMksConnections = 0,
      recordReplayState = 'inactive',
      cleanPowerOff = <unset>,
      needSecondaryReason = <unset>,
      onlineStandby = false,
      minRequiredEVCModeKey = <unset>,
      consolidationNeeded = false,
}

2、Guest操作系統信息(不建議使用,應為受到VMtools影響)

guest = (vim.vm.Summary.GuestSummary) {
      dynamicType = <unset>,
      dynamicProperty = (vmodl.DynamicProperty) [],
      guestId = 'ubuntu64Guest',
      guestFullName = 'Ubuntu Linux (64-bit)',
      toolsStatus = 'toolsOk',  // VMtools狀態
      toolsVersionStatus = 'guestToolsUnmanaged',
      toolsVersionStatus2 = 'guestToolsUnmanaged',
      toolsRunningStatus = 'guestToolsRunning',
      hostName = 'ubuntu001',  // hostname
      ipAddress = '172.16.65.146'  // ipaddress
}

3、虛擬機配置

config = (vim.vm.Summary.ConfigSummary) {
      dynamicType = <unset>,
      dynamicProperty = (vmodl.DynamicProperty) [],
      name = 'Ubuntu16.04',
      template = false,
      vmPathName = '[datastore1] Ubuntu16.04/Ubuntu16.04.vmx',
      memorySizeMB = 1024,
      cpuReservation = 0,
      memoryReservation = 0,
      numCpu = 1,
      numEthernetCards = 1,
      numVirtualDisks = 1,
      uuid = '4239b0ea-cbb8-c0b2-56a1-0b98bdbf01dd',
      instanceUuid = '5039f07c-47c6-d77d-e793-bf1b7aee17e2',
      guestId = 'ubuntu64Guest',
      guestFullName = 'Ubuntu Linux (64-bit)',
      annotation = 'Ubuntu Server',
      product = <unset>,
      installBootRequired = false,
      ftInfo = <unset>,
      managedBy = <unset>
   },

4、虛擬機磁盤信息

storage = (vim.vm.Summary.StorageSummary) {
      dynamicType = <unset>,
      dynamicProperty = (vmodl.DynamicProperty) [],
      committed = 18424777995,
      uncommitted = 505,
      unshared = 17179869184,
      timestamp = 2017-08-26T08:37:37.764585Z
   },

 

。。。。








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM