Thursday, May 31, 2012

Feather duster PowerCLI scripts!

I've been cleaning out script repository and found a file full of PowerCLI scripts I found useful. Hopefully they'll be of use to you too.

What is my consolidation ratio? (number of VMs divided by number of hosts)
(Get-VM | Measure-Object | Measure-Object -Sum Count).sum/(Get-VMHost | Measure-Object | Measure-Object -Sum Count).sum

What is my consolidation ratio rounded to two decimal places?
[Math]::Round((Get-VM | Measure-Object | Measure-Object -Sum Count).sum/(Get-VMHost | Measure-Object | Measure-Object -Sum Count).sum,2)

How many hosts in my cluster aren't ESX 5.0?
Get-Cluster myCluster | Get-VMHost | Where-Object {$_.Version -ne '5.0.0'} | Select-Object Name

What's versions of ESX do I have in my cluster, and how many?
Get-Cluster myCluster | Get-VMHost | Group-Object Version

Count Name     Group
----- ----     -----
    2 5.0.0    {esx01.test.lab, esx02.test.lab....
    1 4.1.0    {esx03.test.lab....

Which VMs in this cluster have SCSI bus sharing set to physical, and what hosts are they on?
Get-Cluster myCluster | Get-VMHost | Get-VM | Get-ScsiController | Where-Object {$_.BusSharingMode -eq 'Physical'} | Select {$_.Parent.Name}, {$_.Parent.Host}, BusSharingMode | Sort {$_.Parent.Host}

$_.Parent.Name  $_.Parent.Host  BusSharingMode
--------------  --------------  --------------
mySQLServer     esx01.test.lab  Physical

How many VMs are on each of my ESX hosts?
Get-VMHost | ForEach-Object {$_.Name; Get-VM -Location $_ | Measure-Object | Select-Object count}

Create a new VMFS datastore on a SAN LUN given a disk's NAA
New-Datastore -Vmfs -VMHost (Get-VMHost esx01.test.lab) -Name myNewDatastore -Path "naa.10000038275027693935893472859354"

Rename a datastore
Get-Datastore BurgerHut | Set-Datastore -Name HungryBurger

Total space consumed by VMs in TB
((Get-VM | Get-HardDisk | Measure-Object -Property CapacityKB -Sum).sum/(1024*1024*1024)).ToString() + " terabytes consumed"
519128.167283058 gigabytes consumed

Total number of datastores that are X KB in size
Get-Datastore | Where-Object {$_.CapacityMB -eq 500} | Measure-Object | Select-Object Count

Total number of VMs
Get-VM | Measure-Object | Select-Object Count

Monday, May 7, 2012

Chess in C (Part 1)

I recently finished writing a chess application in C. I've been trying to spend a greater percentage of my day programming: with all the cool social websites and iPhone apps out there, I've felt a little left out in these past years working in the infrastructure space. Sure, cloud is the coolest thing to happen to infrastructure, but I've always envied the people who write the awesome applications on top of the cloud. zCloud is an amazing piece of infrastructure, but the Zynga games programmed on top of it are much more fun!

The chicken in this picture is happier than your average infrastructure architect.

The best way to write a chess application (or any application) is to split it into several smaller discrete parts. In this blog post, I'll describe how to setup a chess board, put some pieces on the board and print the board. My next blog post will deal with the algorithms used to calculate possible moves.

Setting up the board

I may not be a chess grandmaster, but if I recall correctly, boards have 8 rows and 8 columns. Each square on the board can have a piece. The obvious data structure to represent this would be a two-dimensional array of integers which we can declare as follows.

That was easy. One line of code and we have a chess board! Done! Here's a diagram of what we've created.

A visual representation of int array [8][8]. It ain't pretty, and neither is C.

The first square on the board is 0,0. The last square on the last row is 7,7. Remember, in C, array elements start counting at 0 and not 1, even though the statement we used to create the board was [8][8]. Confusing, I know. Remember this lesson when it comes to writing for loops.

This chess board is neat, but it's not much use without any pieces on it This chess board is an array of integers, which means each array element can hold a number between 0 and 65536. This is more than enough: there are only six types of pieces in chess (assuming a single player for now). Let's define these pieces upfront.

By defining (or should I say, #define'ing) these pieces, we can refer to them freely through our program as BISHOP or KING, instead of having to remember obscure numbers. This will come as a relief to the dyslexics reading this blog post, and the dyslexic writing it.
Now, we'll need a function to print the chess board.

This code might look long, but it's fairly simple. Let's go through it line by line.

We have two for loops that iterate through each element of the board[][] array. Notice how the for loops begin at 0 and stop at 7? This is because in C, we begin counting from 0. Remember this: if you tried to view the contents of array[0][8] (column 9 when the array only 8 columns), you'll get an array out of bounds error. If you're lucky, you'll get garbage, your program will crash, and you'll only have to bang your head against a wall once or twice. If you're unlucky, it'll introduce subtle bad behaviour to your program and you'll die never understanding why your code behaved that way.

If any given element matches a piece, we print the piece. Unfortunately, you can't do a switch() on an array value, which is why I've made a new variable called boardValue and performed a switch() on that. Ignore the BREADCRUMB: I'll explain that later. Let's run the code so far and see what we get.

This is not the chess board I grew up with.

That doesn't look too good. It looks more like a chess board designed by an alcoholic. Where's our chess board?! What happened to our 8x8 one-line wonder?! When an array is instantiated in C, all array elements are filled with garbage. You'll need to write code to "zero out" the board.

This code will iterate through each square in the array and set the value to 0. Let's try that again.
Now THAT'S a chess board!

Fantastic! If you ever get strange array results, make sure you've zeroed out your array. Now let's place two bishops on the board in their starting positions and see what happens.

Two lonely bishops.

Amazing! Two bishops have arrived! Let's go one step further and make an ARMY

Let's look at the chess board now.
An army looking for a fight.

We can even put one hapless opposing pawn on the board with board[6][4] = PAWN;
Advice for pawn in (6,4): RUN

Here's the code if you want to compile it for yourself.

Other posts in this series

Chess in C (Part 1)
Chess in C (Part 2) - Insert Pawn Pun Here
Chess in C (Part 3) - Rook, Rooks, Rookies, Wookies, same thing
Chess in C (Part 4) - I'm asking for input
Chess in C (Part 5) - Potential moves of a bishop: up-left, cardinal, pope