Monitoring Hyper-V utilization and performance

This report gathers information about a Hyper-V server and the VMs running on that server.

Getting ready

You run this recipe on the HV1 Hyper-V host.

How to do it...

  1. Create a basic report hash table:
    $ReportHT = [Ordered] @{}
  2. Get the host details and add them to the report hash table:
    $HostDetails = Get-CimInstance -ClassName Win32_ComputerSystem
    $ReportHT.HostName = $HostDetails.Name
    $ReportHT.Maker = $HostDetails.Manufacturer
    $ReportHT.Model = $HostDetails.Model
  3. Add the PowerShell version information to the report hash table:
    $ReportHT.PSVersion = $PSVersionTable.PSVersion.ToString()
  4. Add OS information to the report hash table:
    $OS = Get-CimInstance -Class Win32_OperatingSystem
    $ReportHT.OSEdition    = $OS.Caption
    $ReportHT.OSArch       = $OS.OSArchitecture
    $ReportHT.OSLang       = $OS.OSLanguage
    $ReportHT.LastBootTime = $os.LastBootUpTime
    $Now = Get-Date
    $UTD = [float] ("{0:n3}" -f (($Now –
                                 $OS.LastBootUpTime).Totaldays))
    $ReportHT.UpTimeDays = $UTD
  5. Add a count of processors in the host to the report hash table:
    $PHT = @{
      ClassName  = 'MSvm_Processor'
      Namespace  = 'Root/Virtualization/v2'
    }
    $Proc = Get-CimInstance @PHT
    $ReportHT.CPUCount = ($Proc |
      Where-Object ElementName -Match 'Logical Processor').Count
  6. Add the current host CPU usage to the report hash table:
    $Cname = '\.processor(_total)\% processor time'
    $CPU = Get-Counter -Counter $Cname
    $ReportHT.HostCPUUsage = $CPU.CounterSamples.CookedValue
  7. Add the total host physical memory to the report hash table:
    $Memory = Get-Ciminstance -Class Win32_ComputerSystem
    $HostMemory = [float]("{0:n2}" -f
                                   ($Memory.TotalPhysicalMemory/1GB))
    $ReportHT.HostMemoryGB = $HostMemory
  8. Add the memory allocated to VMs to the report hash table:
    $Sum = 0
    Get-VM | Foreach-Object {$sum += $_.MemoryAssigned + $Total}
    $Sum = [float] ( "{0:N2}" -f ($Sum/1gb) )
    $ReportHT.AllocatedMemoryGB = $Sum
  9. Create the host report object from the hash table:
    $Reportobj = New-Object -TypeName PSObject -Property $ReportHT
  10. Create the report header:
    $Report =  "Hyper-V Report for: $(hostname)`n"
    $Report += "At: [$(Get-Date)]"
  11. Add the report object to the report:
    $Report += $Reportobj | Out-String
  12. Get the VM details on the local VM host and create a container array for individual VM-related objects:
    $VMs = Get-VM -Name *
    $VMHT = @()   # to be an array of hash tables
  13. Get VM details for each VM into an object added to the hash table container:
    Foreach ($VM in $VMs) {
      # Create VM Report hash table for this VM
       $VMReport = [ordered] @{}
      # Add VM's Name
       $VMReport.VMName = $VM.VMName
      # Add Status
       $VMReport.Status = $VM.Status
      # Add current VM uptime
       $VMReport.Uptime = $VM.Uptime
      # Add VM CPU
       $VMReport.VMCPU = $VM.CPUUsage
      # Replication mode and status
       $VMReport.ReplMode = $VM.ReplicationMode
       $VMReport.ReplState = $Vm.ReplicationState
      # Create an object from Hash table and add to array
       $VMR = New-Object -TypeName PSObject -Property $VMReport
       $VMHT += $VMR
    }
  14. Convert the array of hash tables to a nice string, finishing the report creation:
    $Report += $VMHT | Format-Table | Out-String
  15. Display the report:
    $Report

How it works...

This recipe creates a report that describes both the Hyper-V host and the VM on the host. In the first part, you build a hash table containing information about the host itself which you add to the report. Then, you report on the VMs running on this server (HV1).

In step 1, you create an ordered hash table ($ReportHT). You create an ordered hash table so that the order of rows is maintained. With step 2, you add basic host details to the hash table.

In step 3, you add details of the PowerShell version running on the host. With step 4, you add operating system details to the hash table along with (in step 5) a count of processors available in the host. Then you add current host CPU usage in step 6, and total host physical memory in step 7. In step 8, you calculate the total amount of memory assigned to VMs and add it to the hash table.

In step 9, you create a summary report object with properties coming from the hash table. In step 10, you create a basic report header to which, in step 11, you convert the report object to a string and add it the report.

With step 12 and step 13, you retrieve details of each of the VMs assigned to HV1, and with step 14, you add those details to the report.

Finally, in step 15, you view the report, which looks like this:

How it works...

There's more...

In this recipe, you create and use two hash tables. The first hash table holds details about the VM host overall. If you were reporting on multiple Hyper-V servers, you could create hash tables relating to each server and then convert those two hash tables into objects to report on. If you are going to expand this recipe to cover multiple servers, using objects to report from is easier.

This recipe chose some basic performance and usage information to report. As ever with performance analysis, you could add more. For example, you could add network throughput, storage usage information to the report header. And you could expand the information reported on each VM to include more details. An important thing to consider is when to stop adding details to your reports.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.139.172