Home > Blogs > VMware PowerCLI Blog > Monthly Archives: April 2008

Monthly Archives: April 2008

More Fun With PowerShell Mashups

A few weeks ago I blogged about using PowerGadgets to create custom dashboards to monitor Virtual Infrastructure performance. It was a great example of how easy it is to combine different domains within PowerShell.

Today I want to talk about a slightly different type of monitoring: alarms. VirtualCenter provides a very sophisticated alarm generation system. The problem is, what if you’re not in VirtualCenter watching the alarm tab? Wouldn’t it be better if alarms would reach out and find you wherever you are?

Again, PowerShell provides a very easy solution, due to the ease in which it combines different domains. The VI Toolkit (for Windows) makes it easy to get alarms from VirtualCenter, and /n software’s NetCmdlets makes it really easy to send instant messages. Combine the two and you have a monitoring solution that just about anyone can write and customize.

First, a bit of background: NetCmdlets provides a lot of really useful functionality to PowerShell, including sending email, logging into servers via SSH, downloading files via FTP and a lot more. As usual, the cmdlets come with a very easy syntax, built in documentation and are very easy to learn and use.

Taking that, and combining it with the VI Toolkit (for Windows), I wrote a script that monitors VirtualCenter for alarms and, when it sees new alarms, sends an IM using NetCmdlets. (Note that when you run the script, you are prompted for a login. This login is the login to your Jabber server, not to VirtualCenter.) NetCmdlets currently only supports Jabber as an IM protocol, but more protocols may be added in the future. If IM is not your thing, it’s also really easy to get NetCmdlets to send email instead.

Here’s an example of the results:

Imalarm

The best thing about this is it only took me a couple of hours to put together, and that was mostly due to alarms not being easy enough to access from the current version of the Toolkit (we’ll fix this in a later release, but the script above also contains a script cmdlet that returns all alarms.) Developing a solution like this using other technologies would have taken substantially longer, and wouldn’t have been as easy to customize when they were complete. This, to me, is the real power of PowerShell.

So, if you haven’t tried the VI Toolkit (for Windows) there’s no reason you shouldn’t download it today.

Changing CPU and Memory Allocations For All VMs

We here at VMware often get requests by people who are trying to tweak the default resource allocations that VMs have. This post shows you how easy it is to automate configuring and changing resource allocations is when using PowerShell.

First, a bit of background information. Each VM has a certain level of CPU and memory shares. In addition these levels may be limited or unlimited. Even further, a VM can have CPU or memory reservations, which means the VM will always have at least this minimal level of resources available to it. When shares are unlimited, a VM may receive more resources than its share allocation, provided these resources are not being used by some other VM.

By default, a VM gets normal CPU and memory shares with no reservation and no limit. This is a sensible default, but many times you want to tune this to improve performance for some applications.

This first example shows you how to set all VMs in your entire Virtual Infrastructure to high CPU shares and no limit.

Get-VM | % {Get-View $_.ID} |
    % {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;
        $spec.cpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo;
        $spec.cpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;
        $spec.cpuAllocation.Shares.Level = "high";
        $spec.cpuAllocation.Limit = -1;
        Get-View($_.ReconfigVM_Task($spec))}

This script works whether you have 1 VM or 1,000 VMs. The next example shows how to set a VM’s memory allocation to normal, with no limit, and ensure that each VM has at least 1 GB of memory.

Get-VM | % {Get-View $_.ID} |
    % {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;
        $spec.memoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo;
        $spec.memoryAllocation.Shares = New-Object VMware.Vim.SharesInfo;
        $spec.memoryAllocation.Shares.Level = "normal";
        $spec.memoryAllocation.Limit = -1;
        $spec.memoryAllocation.Reservation = 1024;
        Get-View($_.ReconfigVM_Task($spec))}

Chances are these are not quite the policies you’re looking for, but you easily determine what values you want by referring to the VMware API Reference Guide.

This is extremely powerful, but it’s even more so when you consider that you can combine it with cmdlets like

Get-ResourcePool

and

Get-Folder

to select exactly the VMs you want to modify. For example, using

Get-ResourcePool

you could very easily set all VMs in one resource pool to have CPU resources set to high and all VMs in another resource pool to have CPU resources set to low.

This is just one of the great things you can do when you manage VMware with PowerShell, so if you haven’t looked at it yet you should definately download the VI Toolkit (for Windows) Beta.

You can manage VMware Server with PowerShell too.

Blogger John Tuffin has made an entry about his experiments managing VMware Server 2.0 Beta with PowerShell. To summarize, you can manage VMware Server with PowerShell, in much the same way you manage VMware ESX.

Thngs that will work include:

  • Listing, creating and deleting VMs.
  • Starting, stopping and resetting VMs.
  • Creating snapshot of VMs.

Things that won’t work include things that VMware ESX supports but VMware Server doesn’t. This is a pretty long list, but some highlight include:

  • You can’t create resource pools in VMware Server.
  • You can’t utilize advanced networking options such as virtual switches in VMware Server.
  • You can’t VMotion VMs between VMware Server instances.

Still, if you don’t have access to ESX this is a great way to find out how easy it is to manage VMware using PowerShell. Not only that, but if you do decide to upgrade to ESX to unlock some of these advanced features, the scripts you write against VMware Server will work against VMware ESX with little or no modification.

Manage VMware with PowerGUI

If you haven’t seen it yet, PowerGUI is a great tool for running Windows PowerShell, and we find it to be one of the most popular PowerShell tools out there. Many of our users have anticipated a VMware PowerGUI plugin, and I’m happy to say that the wait is over.

The PowerGUI VMware PowerPack has lots of great features, including the ability to manage your ESX Hosts and VMs, as well as doing bulk operations. In my opinion, PowerGUI’s best feature is that as you interact with PowerGUI, it generates PowerShell code as you go. You can take this code and re-run it later or you can parameterize it and turn it into a script. This is especially great if you’re not a hard-core scripter, or if you aren’t very familiar with PowerShell.

Either way, whether you’re a novice or an expert, this is not a tool to be missed. Congratulations to Dmitry and team from Quest Software!

Monitoring With PowerGadgets

While I wouldn’t go so far as to say that PowerGadgets is the greatest thing in the history of history, it is easily my favorite tool for Windows PowerShell, because of how easily it can be integrated with the VI Toolkit (for Windows) to create some great ways to monitor Virtual Infrastructure.

Getting insight into your entire Virtual Infrastructure

Currently it is not very easy to get an overall picture of your VMware Virtual Infrastructure using the VI Client. The VI Client comes with a number of nice charting tools, but for the most part they focus on one object at a time — a single VM, a single resource pool or a single host. How can we get a picture of the overall status of our Virtual Infrastructure?

The VI Toolkit (for Windows) includes a cmdlet called Get-Stat, which gives you raw access to the performance statistics of VMware objects. When we combine that with the Toolkit’s ability to easily retreive all of a certain type of object, it becomes easy to start making reports that cover the entire datacenter.

Here are two examples that illustrate the point. The first example, cpu.ps1 , creates a graph that shows us the average CPU utilization of all ESX hosts under management. (Note that if you want to run the script against your Virtual Infrastructure you will need to log in using the Get-VC cmdlet first.) Here’s a sample that I ran against my Infrastructure:

Cpu_3

In the same vein, memory.ps1 shows us the average memory utilization across all our hosts. Here’s the output I got when I ran against my systems:

Memory

Monitor All The Time

Another nice feature of PowerGadgets is the ability to publish a gadget as a Vista Sidebar. With this feature you could easily take the examples above and have a pervasive and continuously-updated view of the health of your Virtual Infrastructure, whether you are logged in through VI Client or not.

PowerGadgets: Worth the price?

PowerGadgets is not a free tool, so you’ll have to decide for yourself whether its benefits cover the costs. If monitoring your Virtual Infrastructure is important to you, you should at least download the trial and try it out, I think you’ll find that with the VI Toolkit (for Windows) and PowerGadgets it’s pretty easy to develop a gadget that monitors the things that are most important to you. If you’ve got feedback, or need a bit of help getting started, please share your feedback with us in the VI Toolkit (for Windows) Community.