Showing posts with label ESX. Show all posts
Showing posts with label ESX. Show all posts

Thursday, September 10, 2009

Powershell ESX Lun Balance script

Back again from a long break. I found out that many of the unix based scripts are going to be obsolete within a couple of month. so here is a powershell version of my earlier published script. Thanks to Justin Emerson, http://vmjunkie.wordpress.com/ for the basics of this script. a little copy paste and some mods.

# Cluster/host lun balance script
# Allan Christiansen http://doitsmarter.blogspot.com/
Write-Host "This script will modify the policy of all your shared LUNs on all ESX Servers" -ForegroundColor Cyan
Write-Host "in a Cluster to Fixed and select a preferred path in a round-robin fashion." -ForegroundColor Cyan
#
if ($args.Length -lt 2) {
Write-Host "usage: -c or -s "
exit 1
} else {
if ($args[0] -eq "-c") { $VMHosts = Get-Cluster $args[1] | Get-VMHost }
if ($args[0] -eq "-s") { $VMHosts = Get-VMHost $args[1] }
}
Write-Host "Modifiying ESX hosts:" $VMHosts
foreach ($VMHost in $VMHosts)
{
$luns = $VMHost|get-scsilun -luntype disk| where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"}| Where-object {$_.CanonicalName -notlike "vmhba32*"} |Sort-Object CanonicalName
$firstLUNPaths = Get-ScsiLunPath $luns[0]
$numPaths = $firstLUNPaths.Length
$count = 0
foreach ($lun in $luns)
{
"count   : $count"
"numpath : $numPaths"
$lun.CanonicalName
if ($count -ge $numPaths) { $count = 0 }
$paths = Get-ScsiLunPath -ScsiLun $lun
$paths
$lun|Set-ScsiLun -MultipathPolicy Fixed -PreferredPath $paths[$count]
$count += 1
# Sleep for some seconds as some arrays dont like doing this too fast. and with running vm's its .....
Start-Sleep -Seconds 3
}
}
# Thanks to Justin Emerson, http://vmjunkie.wordpress.com for the basics of this.

Tuesday, February 10, 2009

ESX iscsi enable script using vi-toolkit.

Below is a script for enabling and rescanning Iscsi target for valid vmfs volumes. I'm using Vi-Toolkit for this.
   
Param ($VMhost = "")
$iscsiHba = "vmhba32"
$iscsiServer = "Iscsi-SAN-IP"
$iscsiPort = 3260
$target = New-Object VMware.Vim.HostInternetScsiHBASendTarget
$target.address = $iscsiServer
$target.port = $iscsiPort
$iscsiauthprop = New-Object VMware.Vim.HostInternetScsiHbaAuthenticationProperties
#$iscsiauthprop.ChapAuthEnabled = "true"
#$iscsiauthprop.ChapName = "Chapuser"
#$iscsiauthprop.ChapSecret = "ChapSecret"
$h = Get-VMHost $VMhost
Foreach ($hostView in ( Get-View -VIObject $h)) {
    $storageSystem = Get-View $hostView.configManager.storageSystem
    # Enable software iSCSI controller
    $storageSystem.UpdateSoftwareInternetScsiEnabled($true)
    # Add iSCSI Server for dynamic discovery
    $storageSystem.AddInternetScsiSendTargets($iscsiHba, $target)
    $storageSystem.UpdateInternetScsiAuthenticationProperties($iscsiHba,$iscsiauthprop)
    # Scan for iSCSI devices
    $storageSystem.RescanHba($iscsiHba)
}

I used this for setting the same settings for all my hosts and ensuring that they all are 100% alike.

Friday, October 17, 2008

List vm usage and assosiated datastore

Hi all,
 
in a fairly large environment the tracking of a VM's Disk usage and the datastore on who i resides i a trivial task.
 
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
 This small PowerShell script pulls a list of all VMs and the disk ussage pr. vm and on witch datastore it is connected to/placed. Be aware if an iso is mounted from a datastore this will generate an aditional entry for the VM in question.

Thursday, September 18, 2008

Updating tools on VM's in Vmware

Hi
 
I'm back, after a long break, with other important stuff on the horizon. I realize i have a couple of hundred vm's that needed the tools to be update and i didn't want to do it manually one by one. So i again looked into the VI-toolkit for Vmware and found it pretty easy to update all vm's with a simple script.
 
$VmList = Get-VM | Where-Object {$_.Powerstate -eq "PoweredON"} | % { Get-View $_.ID } | where {$_.guest.toolsstatus -notmatch "Ok"} | Select Name
foreach ($vm in $VmList) {
  "Updating :" $vm
  Update-Tools $vm
}
 
I hope this can help others.

Tuesday, July 22, 2008

Sending mail from Esx 3.x servers

So now i'm back from holiday and was wondering what has happened in my production ESX environment. Well thats not so easy to find out unless you have set up some logging and reporting.
 
To do that easy you need to be able to send emails from the ESX servers. By default that is disabled. But it is very easy to overcome. download the MIME-Lite-3-0-1 module from http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm install it by:
 
tar zxfv MIME-Lite-3.01.tar.gz
cd MIME-Lite-3.01
perl Makefile.PL
make test
make install
 
Then you should be ready to send mails by using the smtp_send.pl script from http://www.jeremypries.com/wp-content/plugins/wp-downloadMonitor/download.php?id=5
 
Then it should just be creating the reports you want in the various cron libraries.
 
Btw the esx-diag tool from Veem tells a great deal on the configuration status of your ESX servers.

Saturday, June 21, 2008

ESX LUN Load balance

After having fighting my way through the Virtual Client setting the active and preferred path for all my currently 22 Lun's on my 24 ESX servers to load balance the load over as many HBA's as possible i looked at the script i could find on the net and saw that the scripts all set the paths to the same for all hosts.
 
I was looking at different scripts out there and found the one below. I need an idea on how to randomize some of this because otherwise it will always be the same om all hosts and if there are 4 hba' as in my case the fourth one will newer be used. I need to set the counter to some random number between 1 and the PATHS value for the hba assignment to start somwhere..   :
#! /bin/bash
#
# by Allan Christiansen/2008 on esx 3.5.0 Update 1
#
for PATHS in 2 4 6 8 10 12 14 16
do
   STPATHS=${PATHS}
   COUNTER=${RANDOM}
   while [[ ${COUNTER} -gt ${PATHS} ]];
   do
      COUNT=`expr ${COUNTER} / ${PATHS}`
      COUNTER=${COUNT}
   done
   for LUN in $(esxcfg-mpath -l | grep "Disk" | grep "has ${STPATHS} paths" | awk '{print $2}')
   do
      esxcfg-mpath --lun=${LUN} --path=$(esxcfg-mpath -q --lun=${LUN} | grep FC | awk '{print $4}' | awk '{print NR "S\t " $0}' | grep ${COUNTER}S | awk '{print $2}') --preferred
      COUNT=`expr ${COUNTER} + 1`
      COUNTER=${COUNT}
      if [[ ${COUNTER} -gt ${STPATHS} ]]
      then
         COUNTER="1"
      fi
   done
done
 
 

Tuesday, June 17, 2008

Vmware ESX

I have for some time been using vmware ESX and found that the software from them is perfect for the growing IT environment that exists in a majority of companies. But i also discovered that managing an environment that is growing fast needs a lot of care and strict procedures. e.g. with vmware there is a possibility to move a vm from one host to another host without user interruption. This is sadly only possible if the cdrom drives are not locked to a iso or otherwise occupied.

I found that running this small powershell script helps alot on that.

Get-VM Get-CDDrive ? { $_.ConnectionState.Connected -eq "true" } Set-CDDrive -Connected:$false -Confirm:$false

This also is a great small script to have in mind when evacuating an ESX. there's nothing more annoying than the last vm will not evacuate because of a mounted cdrom drive or a not ended tools install.

BTW. Thanks alot to Microsoft for the powershell. And for vmware for implementing it in such a great fashion.