示例#1
0
    def get_vm(self):
        data = {}
        vm_properties = [
            "name", "summary.runtime.host", "config.hardware",
            "runtime.powerState"
        ]
        view = pchelper.get_container_view(self.SI,
                                           obj_type=[vim.VirtualMachine])
        vm_data = pchelper.collect_properties(self.SI,
                                              view_ref=view,
                                              obj_type=vim.VirtualMachine,
                                              path_set=vm_properties,
                                              include_mors=True)
        for vm in vm_data:
            if vm['runtime.powerState'] == vim.VirtualMachinePowerState.poweredOn:
                data.update({
                    '{}.{}.{}.vm.{}.fatstats.numIOAvg'.format(
                        self.server.replace('.', '_'), vm['summary.runtime.host'].parent.parent.parent.name.replace(
                            '.', '_'), vm['summary.runtime.host'].parent.name.replace(
                            '.', '_').lower(), vm['name'].replace('.', '_')):
                    self.get_vm_vd_iops(vm, vm['obj'])
                })

                vm_ds_iops = self.get_vm_ds_iops(vm, vm['obj'])
                for key, value in vm_ds_iops.items():
                    data.update({
                        '{}.{}.{}.datastore.{}.{}.numIOAvg'.format(
                            self.server.replace('.', '_'), vm['summary.runtime.host'].parent.parent.parent.name.replace(
                                '.', '_'), vm['summary.runtime.host'].parent.name.replace(
                                '.', '_').lower(),
                            key.replace('.', '_').lower(), vm['name'].replace(
                                '.', '_')):
                        value
                    })

                get_vm_ds_latency = self.get_vm_ds_latency(vm, vm['obj'])
                for key, value in get_vm_ds_latency.items():
                    data.update({
                        '{}.{}.{}.datastore.{}.{}.LatencyAvg'.format(
                            self.server.replace('.', '_'), vm['summary.runtime.host'].parent.parent.parent.name.replace(
                                '.', '_'), vm['summary.runtime.host'].parent.name.replace(
                                '.', '_').lower(),
                            key.replace('.', '_').lower(), vm['name'].replace(
                                '.', '_')):
                        value
                    })
        return data
def update_host_data(server):
    """This function is called to update the HOST data for the specified vcenter server"""
    logger = logging.getLogger(__name__)
    # API:  HostSystem -
    # https://pubs.vmware.com/vsphere-51/index.jsp#com.vmware.wssdk.apiref.doc/vim.HostSystem.html
    # summary.overallStatus: general "health" value: gray, green, red, yellow
    # summary.quickStats.overallCpuUsage: Aggregated CPU usage across all cores on the host in MHz.
    # summary.quickStats.overallMemoryUsage: Physical memory usage on the host in MB.
    # hardware.memorySize:  Total available RAM in bytes.

    try:
        # Fast way of getting Host properties using PropertyCollector
        # https://github.com/vmware/pyvmomi/blob/master/docs/vmodl/query/PropertyCollector.rst
        logger.debug("Getting PropertyCollector for " + server["name"])
        container_view = get_container_view(server["conn"], obj_type=[pyVmomi.vim.HostSystem])
        query = [
            "name",
            "summary.overallStatus",
            "summary.quickStats.overallCpuUsage",
            "summary.quickStats.overallMemoryUsage",
            "hardware.memorySize"
        ]
        props = collect_properties(server["conn"], container_view,
                                   pyVmomi.vim.HostSystem, query, include_mors=True)

    except Exception as error:
        logger.error("Error collecting VMware Host data from " + server["name"] + str(error))
        raise

    # Loop through all of the ESX servers in props
    for prop_set in props:
        host_mor = prop_set["obj"]                      # Managed object reference
        host_name = prop_set["name"]
        host_name = hostname_from_fqdn(host_name)       # trim out the domain name
        host_status = prop_set["summary.overallStatus"]
        host_cpu = prop_set["summary.quickStats.overallCpuUsage"]
        host_ram = prop_set["summary.quickStats.overallMemoryUsage"]
        host_ram_max = prop_set["hardware.memorySize"]
        host_ram_max = int(host_ram_max / 1024 / 1024)      # Convert to Megabytes to match overallMemoryUsage

        host_ram_percent = int((host_ram / host_ram_max) * 100)  # Calculate RAM percentage

        logger.debug(host_name + " RAM: " + str(host_ram_percent) + "% CPU: " + str(host_cpu))

        # Convert ram into Gigabytes and round to 1 decimal place
        host_ram = int(host_ram / 1024)

        # Find/Create this host in our list of hosts and update the object's data
        host = ESXHost.find_by_name(host_mor, host_name)

        if host_status == "green":
            host.status = 1
        elif host_status == "yellow":
            host.status = 2
        elif host_status == "red":
            host.status = 3
        else:
            host.status = 0

        # Add the raw data to the ESXHost object
        # For RAM datapoints, we want to do a bar graph, so only include the current value
        host.ram = host_ram
        host.ram_percent = host_ram_percent

        # For CPU datapoints, we want to do a line graph, so we need a history
        if len(host.cpu_datapoints) == 0:                # first time through, initialize the list
            host.cpu_datapoints = [host_cpu]
        else:
            host.cpu_datapoints.append(host_cpu)
        # If we already have the max number of datapoints in our list, delete the oldest item
        if len(host.cpu_datapoints) >= MAX_DATAPOINTS:
            del(host.cpu_datapoints[0])

        # Update ranking value of this Host to determine if we should show it
        host.update_relative_weight()
示例#3
0
def update_host_data(server):
    """This function is called to update the HOST data for the specified vcenter server"""
    logger = logging.getLogger(__name__)
    # API:  HostSystem -
    # https://pubs.vmware.com/vsphere-51/index.jsp#com.vmware.wssdk.apiref.doc/vim.HostSystem.html
    # summary.overallStatus: general "health" value: gray, green, red, yellow
    # summary.quickStats.overallCpuUsage: Aggregated CPU usage across all cores on the host in MHz.
    # summary.quickStats.overallMemoryUsage: Physical memory usage on the host in MB.
    # hardware.memorySize:  Total available RAM in bytes.

    try:
        # Fast way of getting Host properties using PropertyCollector
        # https://github.com/vmware/pyvmomi/blob/master/docs/vmodl/query/PropertyCollector.rst
        logger.debug("Getting PropertyCollector for " + server["name"])
        container_view = get_container_view(server["conn"],
                                            obj_type=[pyVmomi.vim.HostSystem])
        query = [
            "name", "summary.overallStatus",
            "summary.quickStats.overallCpuUsage",
            "summary.quickStats.overallMemoryUsage", "hardware.memorySize"
        ]
        props = collect_properties(server["conn"],
                                   container_view,
                                   pyVmomi.vim.HostSystem,
                                   query,
                                   include_mors=True)

    except Exception as error:
        logger.error("Error collecting VMware Host data from " +
                     server["name"] + str(error))
        raise

    # Loop through all of the ESX servers in props
    for prop_set in props:
        host_mor = prop_set["obj"]  # Managed object reference
        host_name = prop_set["name"]
        host_name = hostname_from_fqdn(host_name)  # trim out the domain name
        host_status = prop_set["summary.overallStatus"]
        host_cpu = prop_set["summary.quickStats.overallCpuUsage"]
        host_ram = prop_set["summary.quickStats.overallMemoryUsage"]
        host_ram_max = prop_set["hardware.memorySize"]
        host_ram_max = int(
            host_ram_max / 1024 /
            1024)  # Convert to Megabytes to match overallMemoryUsage

        host_ram_percent = int(
            (host_ram / host_ram_max) * 100)  # Calculate RAM percentage

        logger.debug(host_name + " RAM: " + str(host_ram_percent) + "% CPU: " +
                     str(host_cpu))

        # Convert ram into Gigabytes and round to 1 decimal place
        host_ram = int(host_ram / 1024)

        # Find/Create this host in our list of hosts and update the object's data
        host = ESXHost.find_by_name(host_mor, host_name)

        if host_status == "green":
            host.status = 1
        elif host_status == "yellow":
            host.status = 2
        elif host_status == "red":
            host.status = 3
        else:
            host.status = 0

        # Add the raw data to the ESXHost object
        # For RAM datapoints, we want to do a bar graph, so only include the current value
        host.ram = host_ram
        host.ram_percent = host_ram_percent

        # For CPU datapoints, we want to do a line graph, so we need a history
        if len(host.cpu_datapoints
               ) == 0:  # first time through, initialize the list
            host.cpu_datapoints = [host_cpu]
        else:
            host.cpu_datapoints.append(host_cpu)
        # If we already have the max number of datapoints in our list, delete the oldest item
        if len(host.cpu_datapoints) >= MAX_DATAPOINTS:
            del (host.cpu_datapoints[0])

        # Update ranking value of this Host to determine if we should show it
        host.update_relative_weight()
示例#4
0
def generate_json(vmware_monitor):
    """This is the main function. It will connect to the vCenter server, obtain perf data and output json"""
    logger = logging.getLogger("vmware_view_vm")

    if vmware_monitor.conn is None:
        vmware_monitor.conn = connect_vcenter()     # Connect to vCenter server
        if vmware_monitor.conn is None:
            logger.warning("Unable to connect to " + VCENTER_SERVER + " will retry in " +
                        str(SAMPLE_INTERVAL) + " seconds.")
            vmware_monitor.json = json.dumps({"vms": [{"error": "Unable to connect to " + VCENTER_SERVER +
                                                       " will retry in " + str(SAMPLE_INTERVAL) +
                                                       " seconds."}]}, indent=4)
            return vmware_monitor   # Could not connect so return

    # Final test if we're connected
    try:
        if vmware_monitor.conn.content.sessionManager.currentSession:
            logger.debug("Connected to " + VCENTER_SERVER + " at " +
                        str(vmware_monitor.conn.content.sessionManager.currentSession.loginTime))
    except Exception as error:
        logger.error("Final test: Error connecting to " + VCENTER_SERVER + str(error))
        vmware_monitor.json = json.dumps({"vms": [{"error": "Unable to connect to " + VCENTER_SERVER +
                                                            " will retry in " + str(SAMPLE_INTERVAL) +
                                                            " seconds."}]}, indent=4)
        return vmware_monitor  # Could not connect so return

    #
    # ---------------------------------------------------------------------------------------------------------------
    #
    # First we want to grab a list of all the hosts so we can determine the clock speed of the CPUs
    # API:  HostSystem - https://pubs.vmware.com/vsphere-51/index.jsp#com.vmware.wssdk.apiref.doc/vim.HostSystem.html
    #
    # ---------------------------------------------------------------------------------------------------------------
    try:
        # Fast way of getting Host properties using PropertyCollector
        # https://github.com/vmware/pyvmomi/blob/master/docs/vmodl/query/PropertyCollector.rst

        logger.debug("Collecting properties for HostSystem")
        container_view = get_container_view(vmware_monitor.conn, obj_type=[pyVmomi.vim.HostSystem])
        query = ["name", "hardware.cpuInfo.hz"]
        host_props = collect_properties(vmware_monitor.conn, container_view,
                                        pyVmomi.vim.HostSystem, query, include_mors=True)

    except Exception as error:
        logger.error("Error collecting VMware Host data." + str(error))
        vmware_monitor.json = json.dumps({"vms": [{"error": "Error retrieving VMware Host data" +
                                                            str(error)}]}, indent=4)
        return vmware_monitor

    for prop_set in host_props:
        # The properties aren't always returned in the order you expect, so we have to match them up
        host_mor = prop_set["obj"]
        host_name = prop_set["name"]
        host_hz = prop_set["hardware.cpuInfo.hz"]
        logger.debug("Name: " + host_name + " cpuInfo.hz: " + str(host_hz))
        # Create host object so we can find Mhz later (object is stored in class array all_hosts)
        VMwareHost(host_mor, host_name, host_hz)

    # ---------------------------------------------------------------------------------------------------------------
    #
    #  Next we query for the VM stats we are looking for.
    #
    # ---------------------------------------------------------------------------------------------------------------
    # API:  VirtualMachine
    # https://pubs.vmware.com/vsphere-50/index.jsp#com.vmware.wssdk.apiref.doc_50/vim.VirtualMachine.html
    #
    # summary.overallStatus: general "health" value: gray, green, red, yellow
    # summary.quickStats.overallCpuUsage: Amount of CPU actually granted to the VM in Mhz
    # summary.quickStats.staticCpuEntitlement: Max CPU possible for the VM in Mhz
    # summary.quickStats.guestMemoryUsage: Active memory usage of the VM in MB.
    # summary.quickStats.staticMemoryEntitlement: Max CPU possible for the VM in MB
    # config.hardware.numCPU:  Number of virtual CPUs present in this virtual machine.
    # runtime.host:  The host that is responsible for running a virtual machine.

    try:
        # Fast way of getting VM properties using PropertyCollector
        # https://github.com/vmware/pyvmomi/blob/master/docs/vmodl/query/PropertyCollector.rst

        logger.debug("Collecting properties for VirtualMachine")
        container_view = get_container_view(vmware_monitor.conn, obj_type=[pyVmomi.vim.VirtualMachine])
        query = [
            "name",
            "summary.overallStatus",
            "summary.quickStats.overallCpuUsage",
            "config.hardware.numCPU",  # This number is vCPU
            "runtime.host"
        ]
        props = collect_properties(vmware_monitor.conn, container_view,
                                        pyVmomi.vim.VirtualMachine, query, include_mors=True)

    except Exception as error:
        logger.error("Error collecting VMware VirtualMachine data." + str(error))
        vmware_monitor.json = json.dumps({"vms": [{"error": "Error retrieving VMware VirtualMachine data" +
                                                            str(error)}]}, indent=4)
        return vmware_monitor

    # Loop through all of the VMs
    for prop_set in props:
        mor = prop_set["obj"]  # managed_object_reference
        vm_name = prop_set["name"]
        vm_status = prop_set["summary.overallStatus"]
        vm_cpu = prop_set["summary.quickStats.overallCpuUsage"]
        vm_cpu_count = prop_set["config.hardware.numCPU"]
        vm_host_mor = prop_set["runtime.host"]      # managed_object_reference for its host

        # Exclude any items by our list of search terms EXCLUDE_VM
        if any(substring in vm_name for substring in EXCLUDE_VM):
            logger.debug("Excluding: " + vm_name)
            continue
        else:
            logger.debug("VM: " + vm_name + " Status: " + str(vm_status) + " CPU: " + str(vm_cpu))

        # Check to see if this VM is in our list or create one if not found
        vm = VMwareVM.find_by_name(mor, vm_name)
        if vm_status == "green":
            vm.heartbeat_status = 1
        elif vm_status == "yellow":
            vm.heartbeat_status = 2
        elif vm_status == "red":
            vm.heartbeat_status = 3
        else:
            vm.heartbeat_status = 0

        # Store the cpu data in the object
        if len(vm.cpu_datapoints) == 0:
            vm.cpu_datapoints = [vm_cpu]
        else:
            vm.cpu_datapoints.append(vm_cpu)
        # If we already have the max number of datapoints in our list, delete the oldest item
        if len(vm.cpu_datapoints) >= MAX_DATAPOINTS:
            del (vm.cpu_datapoints[0])

        vm.host_cpu_mhz = VMwareHost.get_mhz_by_host(vm_host_mor)  # Get the host hz per CPU
        vm.cpu_count = vm_cpu_count
        # Update ranking value of this VM to determine if we should show it
        vm.update_relative_weight()

    # Once we have finished updating our VM data, grab the top MAX_VM_RESULTS and output the JSON
    # Sort by relative weight
    VMwareVM.all_vms.sort(key=operator.attrgetter('relative_weight'), reverse=True)

    # If there are fewer VMs than we've asked it to display, fix the MAX_VM_RESULTS
    global MAX_VM_RESULTS
    if len(VMwareVM.all_vms) < MAX_VM_RESULTS:
        MAX_VM_RESULTS = len(VMwareVM.all_vms)

    vms = []
    for i in range(MAX_VM_RESULTS):
        vms.append({
            "name": VMwareVM.all_vms[i].name,
            "status": VMwareVM.all_vms[i].heartbeat_status,
            "cpu": VMwareVM.all_vms[i].cpu_datapoints,
            "cpu_count": VMwareVM.all_vms[i].cpu_count,
            "host_cpu_mhz": VMwareVM.all_vms[i].host_cpu_mhz,
        })

    vmware_monitor.json = json.dumps({"vms": vms})

    logger.debug(vmware_monitor.json)

    return vmware_monitor
示例#5
0
def main():

    # Parse path to configuration file
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', help='Configuration ini file.', required=True)
    args = parser.parse_args()

    # Parse the config file
    config = ConfigParser.ConfigParser()
    config.read(args.c)
    if not config.sections():
        print("Unable to read configuration file.")
        sys.exit()

    service_instance = None
    try:
        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        context.verify_mode = ssl.CERT_NONE

        service_instance = connect.SmartConnect(
            host=config.get('main', 'Server'),
            user=config.get('main', 'Username'),
            pwd=config.get('main', 'Password'),
            port=443,sslContext=context)

        atexit.register(connect.Disconnect, service_instance)
    except IOError as e:
        pass

    if not service_instance:
        raise SystemExit("Unable to connect to host with supplied info.")

    # Get current time
    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    # VM properties to fetch
    vm_properties = ["name",
                     "snapshot",
                     "layoutEx.file",
                     "guest.hostName",
                     "config.guestFullName",
                     "summary.quickStats.overallCpuUsage",
                     "summary.quickStats.uptimeSeconds",
                     "summary.customValue",
                     "summary.quickStats",
                     "config.hardware.memoryMB",
                     "config.hardware.numCPU",
                     "config.hardware.device",
                     "config.annotation",
                     "summary.runtime.powerState",
                     "runtime.host",
                     "network",
                     "guest.toolsRunningStatus",
                     "guest.toolsVersionStatus",
                     "guest.net",
                     "guest.guestState",
                     "config.guestId",
                     "config.version",
                     "runtime",
                     "config"]

    # Host properties to fetch
    host_properties = ["config.product.fullName",
                      "hardware.cpuInfo.numCpuCores",
                      "hardware.cpuInfo.hz",
                      "hardware.cpuInfo.numCpuThreads",
                      "hardware.cpuInfo.numCpuPackages",
                      "hardware.memorySize",
                      "hardware.systemInfo.model",
                      "hardware.systemInfo.vendor",
                      "hardware.biosInfo.biosVersion",
                      "name",
                      "summary.hardware.cpuModel",
                      "summary.hardware.numCpuPkgs",
                      "summary.hardware.numNics",
                      "summary.overallStatus"]

    root_folder = service_instance.content.rootFolder

    vm_view = pchelper.get_container_view(service_instance,
                                       obj_type=[vim.VirtualMachine])
    vm_data = pchelper.collect_properties(service_instance, view_ref=vm_view,
                                          obj_type=vim.VirtualMachine,
                                          path_set=vm_properties,
                                          include_mors=True)

    #print(vm_data)

    host_view = pchelper.get_container_view(service_instance,
                                       obj_type=[vim.HostSystem])
    host_data = pchelper.collect_properties(service_instance, view_ref=host_view,
                                          obj_type=vim.HostSystem,
                                          path_set=host_properties,
                                          include_mors=True)

    # Return dictionary of network info
    def get_network_info(guest_net):
        networking = {}
        for nic in guest_net:
            #print(nic)
            networking['vlan'] = []
            networking['mac']  = []
            networking['ip']   = []
            networking['deviceid'] = []

            if nic.ipAddress:
                for i in nic.ipAddress:
                    if not ':' in i:
                        networking['ip'].append(i)

            if nic.network:
                networking['vlan'].append(nic.network)

            if nic.macAddress:
                networking['mac'].append(nic.macAddress)

            if nic.deviceConfigId:
                networking['deviceid'].append(nic.deviceConfigId)

        return(networking)


    # Return human readable uptime
    def return_uptime(seconds):
		days = seconds / 86400
		seconds -= 86400 * days
		hrs = seconds / 3600
		seconds -= 3600 * hrs
		mins = seconds / 60
		seconds -= 60 * mins
		if days >= 7:
			return '{} days'.format(days)
		elif days >= 1 and days < 7:
			return '{}d {}h'.format(days, hrs)
		else:
			return '{}h {}m'.format(hrs, mins)

    # Return human readable mem size
    def memGB(MB):
		if MB > 1000:
			return MB/1024
		elif MB < 1001:
			return '.{}'.format(str(MB)[:1])
		else:
			return '-'

	# Return snapshot info
    def get_snapshot_info(vm):
        snapshot_dict = {}
        disk_list = vm["layoutEx.file"]
        size = 0
        for disk in disk_list:
			if disk.type == 'snapshotData':
				size += disk.size
			ss_disk = re.search('0000\d\d', disk.name)
			if ss_disk:
				size += disk.size

        size_gb = (float(size) / 1024 / 1024 / 1024)
        create_time = vm['snapshot'].rootSnapshotList[0].createTime

        snapshot_dict['size']        = round(size_gb, 2)
        snapshot_dict['description'] = vm['snapshot'].rootSnapshotList[0].description
        snapshot_dict['created']     = create_time.strftime('%Y-%m-%d %H:%M:%S')
        snapshot_dict['name']        = vm['snapshot'].rootSnapshotList[0].name

        return snapshot_dict

    # Host ID to hostname translation dictionary
    host_to_hostname = {}
    for host in host_data:
        host_to_hostname[host['obj']] = host['name']

    # Init dictionary of VMs
    vms = {}

    # Populate vms dictionary
    for vm in vm_data:
        vms[vm["name"]] = {}
        vms[vm["name"]]['numCPU']        = vm["config.hardware.numCPU"]
        vms[vm["name"]]['memoryMB']      = vm["config.hardware.memoryMB"]
        vms[vm["name"]]['host']          = host_to_hostname[vm["runtime.host"]]
        vms[vm["name"]]['powerState']    = vm["summary.runtime.powerState"]
        vms[vm["name"]]['guestState']    = vm["guest.guestState"]
        vms[vm["name"]]['ballooning']    = vm["summary.quickStats"].balloonedMemory
        vms[vm["name"]]['swapping']      = vm["summary.quickStats"].swappedMemory
        vms[vm["name"]]['guestName']     = vm["config.guestFullName"]
        vms[vm["name"]]['hwVersion']     = vm["config.version"]
        try:
            vms[vm["name"]]['annotation']    = vm[u"config.annotation"]
        except KeyError:
            vms[vm["name"]]['annotation']  = "" 
        vms[vm["name"]]['uptime']        = return_uptime(vm["summary.quickStats"].uptimeSeconds)
        try:
            vms[vm["name"]]['toolsVersion']  = vm["guest.toolsVersionStatus"]
            vms[vm["name"]]['toolsRunning']  = vm["guest.toolsRunningStatus"]
        except KeyError:
            vms[vm["name"]]['toolsVersion']  = "Information not available"
            vms[vm["name"]]['toolsRunning']  = "Information not available"
        vms[vm["name"]]['cpuUsage']      = vm["summary.quickStats"].overallCpuUsage
        vms[vm["name"]]['networking']    = get_network_info(vm['guest.net'])
        vms[vm["name"]]['disks']         = {}
        vms[vm["name"]]['snapshot']     = {}

        try:
            vms[vm["name"]]["snapshot"] = get_snapshot_info(vm)
        except KeyError:
            pass

        for device in vm['config.hardware.device']:
            if type(device).__name__ == 'vim.vm.device.VirtualDisk':
                vms[vm["name"]]['disks'][device.deviceInfo.label] = {
                       'size' : device.capacityInKB / 1024 / 1024,
                       'file' : device.backing.fileName}
                # Condition needed for thin provisioning due to RDM's missing the parameter
                try:
                    vms[vm["name"]]['disks'][device.deviceInfo.label]['thin'] = device.backing.thinProvisioned
                except AttributeError:
                    vms[vm["name"]]['disks'][device.deviceInfo.label]['thin'] = False

            if type(device).__name__ == 'vim.vm.device.VirtualE1000':
                vms[vm["name"]]['networking']['type'] = 'E1000'

            if type(device).__name__ == 'vim.vm.device.VirtualE1000e':
                vms[vm["name"]]['networking']['type'] = 'E1000e'

            if type(device).__name__ == 'vim.vm.device.VirtualVmxnet3':
                vms[vm["name"]]['networking']['type'] = 'Vmxnet3'

    # Init dictionary of hosts
    hosts = {}

    # Populate host dictionary
    for host in host_data:
        hosts[host["name"]] = {}
        hosts[host["name"]]["fullName"]         = host["config.product.fullName"]
        hosts[host["name"]]["numCores"]         = host["hardware.cpuInfo.numCpuCores"]
        hosts[host["name"]]["cpuHz"]            = host["hardware.cpuInfo.hz"]
        hosts[host["name"]]["cpuMHz"]           = int(host["hardware.cpuInfo.hz"]) / 1000000 + 1
        hosts[host["name"]]["cpuThreads"]       = host["hardware.cpuInfo.numCpuThreads"]
        hosts[host["name"]]["cpuCount"]         = host["hardware.cpuInfo.numCpuPackages"]
        hosts[host["name"]]["cpuModel"]         = host["summary.hardware.cpuModel"]
        hosts[host["name"]]["memSize"]          = host["hardware.memorySize"] / 1024 / 1024 / 1023
        hosts[host["name"]]["model"]            = host["hardware.systemInfo.model"]
        hosts[host["name"]]["vendor"]           = host["hardware.systemInfo.vendor"]
        hosts[host["name"]]["biosVersion"]      = host["hardware.biosInfo.biosVersion"]
        hosts[host["name"]]["numCpuPkgs"]       = host["summary.hardware.numCpuPkgs"]
        hosts[host["name"]]["numNics"]          = host["summary.hardware.numNics"]
        hosts[host["name"]]["health"]           = host["summary.overallStatus"]

    # Calculate extra parameters to avoid doing too many calculations in template
    for host in hosts:
        memoryUsage = 0
        vcpuUsage = 0
        runningVMs = 0
        for vm in vms:
            if vms[vm]['host'] == host and vms[vm]['powerState'] == 'poweredOn':
                memoryUsage += vms[vm]['memoryMB']
                vcpuUsage += vms[vm]['numCPU']
                vms[vm]['memoryGB'] = memGB(vms[vm]['memoryMB'])
            if vms[vm]['host'] == host and vms[vm]['powerState'] == 'poweredOn':
                runningVMs += 1

        hosts[host]['memoryUsage']              = memoryUsage / 1024
        hosts[host]['cpuUsage']                 = vcpuUsage
        hosts[host]['runningVMs']               = runningVMs
        hosts[host]['memoryUsagePercentage']    = int(float(hosts[host]['memoryUsage']) /
                                                    float(hosts[host]['memSize']) * 100)
        hosts[host]['cpuUsagePercentage']       = int(float(hosts[host]['cpuUsage']) /
                                                    float(hosts[host]['numCores']) * 100)

    # Shift historic files, keep number of versions defined in ini file
    for i in range(int(config.get('main', 'History')), 1, -1):
        try:
            os.rename('{}/{}.html'.format(config.get('main', 'OutputPath'), i-1),
                      '{}/{}.html'.format(config.get('main', 'OutputPath'), i))
        except OSError:
            pass

    try:
        os.rename('{}/{}'.format(config.get('main', 'OutputPath'), config.get('main', 'OutputFile')),
                  '{}/1.html'.format(config.get('main', 'OutputPath')))
    except OSError:
        pass

    ### Start generating HTML

    # Capture our current directory
    THIS_DIR = os.path.dirname(os.path.abspath(__file__))

    j2_env = Environment(loader=FileSystemLoader(THIS_DIR),
                         trim_blocks=True,
                         lstrip_blocks=True )

    file = open(config.get('main', 'OutputPath')+
                '/'+
                config.get('main', 'OutputFile'),'w')

    file.write(j2_env.get_template(config.get('main', 'Template')).render(
        title=config.get('main', 'Site') + ' ESXi overview',
        last_update=current_time,
        vms=collections.OrderedDict(sorted(vms.items())),
        hosts=collections.OrderedDict(sorted(hosts.items())),
        filename=config.get('main', 'OutputFile'),
        instances=int(config.get('main', 'History')))
    )

    file.close()
示例#6
0
                                                 pwd=args.password,
                                                 port=int(args.port))
    atexit.register(connect.Disconnect, service_instance)
    atexit.register(endit)
except IOError as e:
    pass

if not service_instance:
    raise SystemExit("Unable to connect to host with supplied info.")

root_folder = service_instance.content.rootFolder
view = pchelper.get_container_view(service_instance,
                                   obj_type=[vim.VirtualMachine])
vm_data = pchelper.collect_properties(service_instance,
                                      view_ref=view,
                                      obj_type=vim.VirtualMachine,
                                      path_set=vm_properties,
                                      include_mors=True)

for vm in vm_data:
    try:
        vm_details.append(
            format(vm["name"]).lower() + "," + format(vm["config.uuid"]) +
            "," + format(vm["config.hardware.numCPU"]) + "," +
            format(vm["config.hardware.memoryMB"]) + "," +
            format(vm["guest.guestState"]) + "," +
            format(vm["config.guestFullName"]) + "," +
            format(vm["config.guestId"]) + "," + format(vm["config.version"]) +
            "," + format(vm["config.firmware"]) + "," +
            format(vm["config.hardware.numCoresPerSocket"]) + "," +
            format(vm["summary.guest.ipAddress"]))
示例#7
0
    def collect(self, si, cloud_entry_ids, target):
        metric_list = constants.metric_list
        metrics = {}
        for key in metric_list.keys():
            metrics.update(metric_list[key])
        self.metrics = metrics

        logger.info(
            "[{0}] [PID-{1}] Start collecting vcenter metrics for {2}".format(
                datetime.utcnow().replace(tzinfo=pytz.utc), os.getpid(),
                target))
        print(
            "[{0}] [PID-{1}] Start collecting vcenter metrics for {2}".format(
                datetime.utcnow().replace(tzinfo=pytz.utc), os.getpid(),
                target))

        obj_types = {
            vim.VirtualMachine: constants.vm_properties,
            vim.HostSystem: constants.host_properties,
            vim.Datastore: constants.data_properties
        }

        for obj_type, properties in obj_types.items():
            view = self.get_container_view(si, obj_type=[obj_type])

            data = []
            if obj_type is vim.VirtualMachine:
                consul_vms = self.get_vms(cloud_entry_ids)
                if consul_vms:
                    data = pchelper.collect_properties(si,
                                                       view_ref=view,
                                                       obj_type=obj_type,
                                                       path_set=properties,
                                                       include_mors=True,
                                                       vms=consul_vms)
            else:
                data = pchelper.collect_properties(si,
                                                   view_ref=view,
                                                   obj_type=obj_type,
                                                   path_set=properties,
                                                   include_mors=True)

            if obj_type is vim.HostSystem:
                self._vmware_get_hosts(cloud_entry_ids, data)
            elif obj_type is vim.VirtualMachine:
                vm_counts, vm_ages = self._vmware_get_snapshots(data)
                self._vmware_get_vms(consul_vms, data)
            elif obj_type is vim.Datastore:
                self._vmware_get_datastores(cloud_entry_ids, data)

        vm_labels = constants.vm_labels
        sscount_metric_name = 'vmware_vm_snapshots'
        sstime_metric_name = 'vmware_vm_snapshot_timestamp_seconds'
        for v in vm_counts:
            label_values = []
            v_id = v['vm_id']
            consul_vm = consul_vms.get(v_id)
            for label in vm_labels:
                label_value = ''
                if label == 'server_type':
                    vm_value = consul_vm.get('resource_type', '')
                else:
                    vm_value = consul_vm.get(label, '')
                if vm_value:
                    label_value = vm_value
                if isinstance(label_value, list):
                    label_value = ','.join(str(e) for e in label_value)
                label_values.append(label_value)
            snapshot_count = v['snapshot_count']
            self.metrics[sscount_metric_name].add_metric(
                label_values, snapshot_count)

        for k in vm_ages:
            label_values = []
            consul_vm = consul_vms.get(k)
            for label in vm_labels:
                label_value = ''
                if label == 'server_type':
                    vm_value = consul_vm.get('resource_type', '')
                else:
                    vm_value = consul_vm.get(label, '')
                if vm_value:
                    label_value = vm_value
                if isinstance(label_value, list):
                    label_value = ','.join(str(e) for e in label_value)
                label_values.append(label_value)
            for v in vm_ages[k]:
                ss_name = v['vm_snapshot_name']
                vm_snapshot_name = ss_name if ss_name else ''
                vm_snapshot_time = v['vm_snapshot_timestamp_seconds']
                self.metrics[sstime_metric_name].add_metric(
                    label_values + [vm_snapshot_name], vm_snapshot_time)

        logger.info(
            "[{0}] [PID-{1}] Stop collecting vcenter metrics for {2}".format(
                datetime.utcnow().replace(tzinfo=pytz.utc), os.getpid(),
                target))
        print("[{0}] [PID-{1}] Stop collecting vcenter metrics for {2}".format(
            datetime.utcnow().replace(tzinfo=pytz.utc), os.getpid(), target))

        self._vmware_disconnect(si)

        for _, metric in self.metrics.items():
            yield metric