Wednesday, October 21, 2015

Get-SCSILun Woes

When running any type of PowerCLI script that needs to query storage devices on an ESXi host, you end up using the GET-SCSILun command.  Unfortunately, if you have a large environment with a lot of storage devices connected to your hosts, you will quickly find out that this single command can be very expensive to run.  In my environment it literally takes us almost 1-2 minutes to execute.  Now you may think that is nothing, but if you are iterating through 50 hosts and for each one you are running this command, now your talking about 50-100 minutes for the script to complete.  I was working on a script recently that would pull disk information for each virtual machine in our environment.  One of the fields that was asked for was the vendor and model info for each storage device that housed the VMDK files for the VM.  Of course to get this info it required using the GET-SCSILun command.  Well you guesed it that meant running that command for each of the 200+ VM's.  The script worked great, except that it took nearly 5 1/2 hours to complete :(.  Unacceptable right! 

Thinking outside the box, I decided to determine what my script was doing each time I called that command.  Essentially what I was doing was using GET-Datastore cmdlet to pull the canonical name (unique number identifying the storage device) and then running the GET-SCSILun using the known canonical name to pull the vendor and model properties off of the storage device.  This got me thinking, why not pull all the storage devices information for a host and store it in a hash table using the canonical name as the key.  This works for me as all the VMs are in the same cluster of Hosts, hence all the hosts should have the same storage devices.  If you intend to work with multiple clusters or hosts not in clusters I would recommend using separate hashtables and then referencing each based on the VM you are querying and what host its hosted on.

Anywho...To build this hash table is pretty simple.  First just declare the hash table array variable using @{} syntax.  Then run the GET-SCSILun command targeting a host while using the hashtable ".add" function to pipe the information to the hash table.  In the add function you declare what properties to add to the table (the first one is the key so make sure its unique).  Here is an example of how I built the hash table of the information.

#Dump scsi lun info to hash table
$scsiLunInfo = @{}
GET-SCSILun -VMHost $vmHost | %{
     $scsiLunInfo.Add($_.CanonicalName,$_.ExtensionData.Vendor,$_.ExtensionData.Model)
}


Alrighty.  Now that we have a hash table of the scsi lun information we can search that table instead of running the GET-SCSILun command.  The difference is literally night and day in terms of time to execute.  Remember how I said the GET-SCSILun command could take 1-2 minutes?  Searching the hash table is nearly instant.  Ok, now we got a hash table of the storage device info, but how do we use it?

Well, in my scenario I am trying to pull the vendor and model of a storage device that a VM's VMDK file is residing on.  So first I use the GET-Datastore cmdlet to target a datastore with a VM's VMDK files on it and pull the canonical name of the storage device that the datastore is hosted on (remember the canonical name is the key in the hash table).  Once I have this name, I can then search the hash table and return the additional properties that are stored with the canonical name key.  This is accomplished through the ".Get_Item" function on a hash table.  Here is an example of how I utilize the hash table to return the required information.

#Get canonical name of storage device
$id = (GET-Datastore $datastore | Get-View).Info.vmfs.extent.diskname
#Determine backend storage system
$storageSystem = $scsiLunInfo.Get_Item($id)

Using this approach, I first build the hash table in the begining of my script.  Then any time I need to pull the storage device info, I query the hash table instead of running the expensive GET-SCSILun command.  Now that I only run that command once at the begining of the script, the time to execute was reduced nearly 80%.  Remember I stated it took nearly 5 1/2 hours to run my script against 200 VM's?   With this approach the script now takes roughly 15 minutes.

I am sure this concept can be used to other expensive VMWare PowerCLI cmdlets, but have yet to experiment with any others.  


Monday, October 14, 2013

A Healthy Start


So to begin with, I started getting healthy about 75 days ago or so.  I was about 30 plus pounds overweight, had a slipped disc in my back, high blood pressure, and took a pill a day for acid reflux.  Maybe not that bad compared to others, but definately not where I wanted to be.  My diet consisted of about everything unhealthy you could think of and washed down with Mountain Dew or Beer. 

I would normally do my "annual" workout during my hunting trips which amounted to about 2-3 weeks a year.  Although I recently got a four wheeler so I seem to do alot less hiking.  With the upcoming hunting season I wanted to get back in some sort of decent shape.  I looked around alot and finally determined that I needed to figure out what exactly my goals were.  In the past I always just did some running and maybe go to the gym and fumble around on some machines until I got a sweat.  Not really ever had a plan.  Come to think of it, all my life I have always had my fitness planned out for me and I would just show up and follow the directions (High school sports followed by the military).  I didn't have to worry about what exercises to do and how often to do them.  Looking around alot online, I was able to put together a plan for both diet and exercise.  I followed these plans and even progressed in what they consisted of over the last 75 days and to date have lost nearly 40 pounds.  I am now pain free in my back (and more mobile than I can remember), great blood pressure, and for the first time in 7 years was able to stop taking my acid reflux pills. 

I get alot of people asking me how I lost the weight and I just say diet and exercise.  I would like to explain to them the details but it can be alot to explain and I am sure some just ask to be nice and don't really want the full version.  That is why I decided to put this in a blog.  To help others undestand my process if they are interested.

The Diet...

So after tons of reading and researching (BTW I do have a job).  I finally came to one conclusion, DIET IS MORE IMPORTANT THAN THE EXERCISING.  This was an interesting revelation because in all my attempts to get healthy I rarely considered diet, but just how much I was sweating.  This may be common sense to some, but not to someone that "once" had a very high metabolism.  So what kind of diet you ask, well to start off with I found that you didn't have to make it that complex. 

I break this diet down to three areas; liquids, portions, and content.  Remember, by no means am I a dietician or anything like that, this is just the formula that I used and had good results.

First, I focused on the liquids (which I beleive are the biggest culprits to obseity).  I stopped drinking soda and really anything carbonated.  I made it a point to drink more water, juices, and gatorades.  I did have the occaisonal beer, but other than that I pretty much stuck to it.  People are human and cravings and circumstances can always be a factor, just REMEMBER if you do deviate, keep it as minimum as possible and make up for it in your next meal (IMO this mentality is what kept my progress going even when I made bad decisions in my diet).

Second, it was obvious to me that portions were a big issue in my diet.  Thinking hard on how I ate gave me some great insight into my relationship with food.  I found that my eating habits were just that, "habits."  I did not eat with a purpose other than to finish what was on my plate (something I learned growing up).  I found that I ate because of what I was doing or what time it was.  What was really interesting to me, was that the most important reason never really came up.  I didn't eat because I was HUNGRY.  And most importantly, I did not stop when I was FULL.  I decided to throttle my portions and meals to just three meals a day with two snacks.  Of course this was hard at first, (habits are called that for a reason), but after a couple weeks of really being strict my body actually adopted the scheduled and let me know when I deviated.  I did not get into strict calorie counting or weighing portions on a scale, I just used common sense as to what was "enough."  Using the approach of asking myself before my meals if I am hungry and during my meals if I am full really helped out and I find this technique useful to this day.

Lastly, you have to look at what it is that you are eating.  Obviously I got more scrupulous as I progressed in my journey,  but to start out with just make healthy choices.  I tried to stay away from fattening things such as sweets and sugars and especially anything fried.  I tried to introduce as many veggies as I could and if all else failed, go with a sandwich.  I made a consicous effort to only eat pastas or potatoes 1-2 times a week.  Now I always had times where I couldn't exactly follow the plan, but on those days I tried to eat "even smaller" portions of the bad food or compensated with "smaller" or "healthier" portions on the next meal.  DO NOT GET THE MENTALITY THAT YOU WILL JUST EXERCISE HARDER OR LONGER TO MAKE IT UP. As you will see with my future blogs, this portion of my diet changes drastically of time, but this is the foundation that got me on the right track.

The Exercise...

Well now that I have a diet plan, I just needed to make an exercise plan.  Sounds easy right?  Well I had been a very active person for most of my life with the exception being the last 7-8 years.  I have done tons of exercise and hit lots of gyms, hell I even organized and ran workouts in the Army.  So I'm thinking, I will just do tons of running and maybe some situps and pushups.  Problem is at the time I had a bad back and lots of impact would aggrevate my slipped disc.  Really, my goal was to lose weight not to run a marathon.  I remember back in High School when wrestlers wanted to wrestle at smaller weights they would train in trash bags.  Why?  Because sweat equals weight loss!  I needed to sweat as much as possible to lose as much weight as possible.  That was my mentality anyway.  I had heard alot of family and friends talking about this video workout called Insanity.  Everybody claimed it kicked your butt and made you sweat.  So I got myself a copy and decided to give it a try.

The workouts were really hard and produced a ton of sweat.  And I used the mentality that I did not have to follow the workouts to a T to get my results.  I just had to keep moving and doing something even if it was not what they were doing.   The thing with the workouts is that you have to be consistent and you have to push yourself.  You know your body the best and you know if you got a good workout or not.  DO THE WORKOUTS, even if that means doing them right before bed at 10PM.  I have a wife, kids, and a job.  It's hard to make time for these workouts, but it's also hard to make time for your favorite TV series.  Make it important to you and you will complete it.  It's always good to have someone help you in accountability, every night my wife would ask me if I did my workout and if the answer was no, she would suggest I do it then.  Sometimes I grumbled but I appreciated it later.

The Tricks...

Throughout the journey I found a few insights into the process that I was able to exploit to my advantage.  These are not magic pills nor are they easy to do, but if you can they will give you some great results. 

As much as I despise counting calories, I did find that if I could keep my caloric intake to around 1800-2000 calories and keep my workouts I would see significant drops on the scale.  Mainly I did this when I would get stuck on a weight for a week or so.  Its hard to do, let alone track, but it is possible.  Towards the end of the weight loss journey I found myself doing this so regulary that I have a hard time eating the recommended 2500 calories a day now.  Positive side effect is once you know what minimum calories looks like, you can easily eyeball what's good and bad portions when preparing your meals.

Everyone knows CARBS are bad for weight loss.  They translate to energy, but you want your body to burn the fat it has stored for energy (hence lose weight).  One trick I picked up was to make sure and ONLY eat carbs during the meal after a workout.  So if I worked out in the morning I would eat my carbs for breakfast, or if I worked out after work, I would eat my carbs for dinner.  The other two meals should have VERY minimal carbs.   This ensures that you are not beefed up on carbs prior to your workout causing your body to dip into your stored fat for energy.  After your workout your body is still burning fuel at a good rate for quite some time and can burn the carbs you just put in, instead of storing them in fat.

Eat the same thing ALOT.  I found a few foods and snacks that were healthy and I enjoyed and stuck with them.  Eating the same thing at the same time each day, for some reason, seems to make food turn into a critical function (much like walking) and less like a pleasure.  This means that your stomach stops driving your actions and starts behaving again like an organ in your body.  We almost never eat things that are bad or alot of things because we are hungry.  It is normally because of a smell or a craving of a taste of something that we have in our memory. I cannot fully explain it, but keeping the same diet and schedule really does help combat this.  Maybe because you stop thinking so much about food.

Like I said before, I am not a doctor nor do I hold any type of medical degree.   I am just a guy who took a healthy journey and had some success.  I am sharing this info with others to hopefully help someone else, but remember that we are all different and things that worked for me may not work for you.