martedì 18 marzo 2014

VMware: VM stats using PowerCLI and Google Charts

Let me start today's post by pointing out that this is more a proof of concept than a real thing, but I hope from this piece of code you get the idea of how many different tasks can be accomplished by using PowerCLI and how versatile it is for reporting capabilities. After producing fancy reports using PowerCLI why not also include some charts in them?

This can be easily accomplished by using Google Charts, JavaScript pieces of code that generates charts/tables just by passing data to them and the resulting charts can be embedded in web pages.

The idea behind this PowerCLI script is to generate an HTML page and retrieve stats from an entity (a virtual machine) by using the Get-Stat cmdlet. These stats will be passed as data upon which charts will be created.

If you have a look at a sample chart code, for example a donut chart, you can slice it up into three different areas: an header in which javascript function is defined, a body, the most important part, in which you insert all the data and some options like title, measures to be used on x-y-axis, etc. Third and final part is a sort of a footer in which javascript and html tags are closed.

The PowerCLI cmdlet Get-Stat retrieve stats from any powered on virtual machine:

 Get-Stat -Entity (Get-VMHost -Name 10.0.1.62 | Get-VM | Where-Object PowerState -match "PoweredOn") -Stat $stat -Start (Get-Date).AddHours(-24) -MaxSamples (10) -IntervalMins 10 | Measure-Object Value -Average  

Where:

-Entity: the object against you want to retrieve stats from. This could be a datacenter, a virtual machine, a host, etc. In the line above it retrieves all powered on VMs running on host 10.0.1.62.
-Stat: the statistic you want to retrieve.

The following values are accepted:

cpu.usage.average
cpu.usagemhz.average
mem.usage.average
disk.usage.average
net.usage.average
sys.uptime.latest

-Start: start time of statistics retrieval
-MaxSamples: how many samples consider for the measurement
-IntervalMins: the amount of time that separates two consecutive measurements
-Measure-Object Value: we need to specify we are going to use this value as a measure.
-Average: how this measure will be considered. Average, Maximum and Minimum are accepted values. Average consider the average resulting value from all collected samples. Please note that for every of the available stats the Average value is a good indicator since the average usage of CPU, Memory, Disk or Network over an amount of time could be considered as a good indicator of how well the measured entity performs. sys.uptime.latest is the only measure for that average is not suited, since we need to retrieve only the last value of uptime in this case Maximum is the correct way to measure it.

The output produced by the PowerCLI script is an HTML file and this means that you can style it applying a proper CSS.

This is an example of a chart report I generated:



The following is the sample PowerCLI code used to generate a donut chart, as usual you can find it also on my GitHub repository: Stats using Google charts.ps1



As you can see this time HTML page is generated by simply concatenating variables $htmlheader, $data and $htmlfooter containing the HTML code. This is another, less elegant way though, to generate the HTML page. Conversely you can use the ConvertTo-Html cmdlet as in fancy reports using PowerCLI.

1 commento:

  1. This is a great idea, thanks. I am new to this, so I have a question: do you put all of this code in a html file and run it or in a ps1 file and run it through powershell/powercli?

    RispondiElimina