RSS

PowerShell: Setting JVM heap sizes for vCenter server 5.x

10 Jul

One of the changes from vCenter 5.0 to 5.1 that affect java memory allocation is vFabric tomcat server. This server effective decouples the java heap size memory management to the various vCenter server components, when this used to be a single setting in the tomcat configuration. An effect of this is that you many need to adjust the JVM heap size of your vCenter after upgrading (or even fresh install), even though you have specific the correct sizing during install.

From my experience, since we had upgraded vCenter from 5.0 to 5.5, the JVM heap size may not be sized correctly after the upgrade. Especially the Inventory servces, which can impact performance is set too low.

KB2021302 describes the sizing in details, but we should be really only interested in 3 settings for correct sizing, i.e. VC service, Inventory service and profile-driven service. The script below was developed to configure the JVM heap sizes of these 3 services after they have been upgrade to vCenter 5.5, to the correct sizing.

The script is pretty rudimentary, i.e not elegant, but it gets the work done.

  • The basic logic is just to read the JVM heap size setting for each conf file and replace them with the correct size via get-content|set-content pair.
  • Because services need to be started to be effective, I have also included this in the script.
  • Different organisations will have different standards where they install their services, so you may need to modify the path search accordingly. I have only included C: and D: as search paths for the conf files.
  • Regarding restarting of services with dependencies, you will need to start those dependent services after the main service is restarted, which is why the function additional starts up dependent service after the main service is restarted. For some reasons, it is possible for the service restart to fail or timeout, you just need to manually reboot the server or services on your end.
<#
    .SYNOPSIS
		Script to set java heap size in VC5.5
		
    .DESCRIPTION
        This script is used to set the java heap size on vCenter 5.5 to S, M or L according to KB2021302
		Only vCenter server, inventory and profile-driven storage services are set as the rest does not
		need setting in general.
			
	.PARAMETER
	    vc: name of vc (default is localhost)
		vc_size: [S|M|L] (default is M)
		auto: used for batch processing, will suppress prompts
	
	.NOTES
		File Name	: Set-VCJavaHeapSize.ps1
		Version		: 1.0
		Date		: 10/07/2015

    .EXAMPLE
	# Set vc to L
	Set-VCJaveHeapSize.ps1 -vc vcenter01 -vc_size L 
	
	# Set vc to M, run in batch mode
	Set-VCJaveHeapSize.ps1 -vc vcenter01 -vc_size M -auto
	
#>

param($vc="localhost", [ValidateSet("S","M","L")]$vc_size="M",[switch]$auto)

Function Restart-Svc
{ 
Param($svc,$hostname)

$mainsvc = Get-Service $svc -ComputerName $hostname -ErrorAction SilentlyContinue -ErrorVariable err
if ($err.count -ne 0) { Write-Host "Cannot bind to service $svc on $hostname" -ForegroundColor Red ; return}

Write-Host "Restarting $svc on $hostname..."
Write-Host "this can take 5 mins or longer..."
Restart-Service -InputObject $mainsvc -Force -WarningAction SilentlyContinue
$mainsvc.Refresh() 
If ($mainsvc.Status -eq "Running") 
 {Write-Host "$($mainsvc.Name) is Running" -foregroundColor green}
else 
 {Write-Host "$($mainsvc.Name) is $($mainsvc.status), please check service" -foregroundColor red}
	 
Get-Service $svc -ComputerName $hostname -DependentServices |
 % { $dpsvc = Get-Service $_.name -ComputerName $hostname ;
     Start-Service -InputObject $dpsvc -WarningAction SilentlyContinue
	 $dpsvc.Refresh() 
	 If ($dpsvc.Status -eq "Running") {Write-Host "$($dpsvc.Name) is Running" -foregroundColor green}
	 else {Write-Host "$($dpsvc.Name) is $($dpsvc.status), please check service" -foregroundColor red}
   }

}

$cont = $true
$cfgdir_vc = "program files\VMware\Infrastructure\tomcat\conf\wrapper.conf"
$cfgdir_is = "program files\VMware\Infrastructure\Inventory Service\conf\wrapper.conf"
$cfgdir_ps = "program files\VMware\Infrastructure\Profile-Driven Storage\conf\wrapper.conf"

#verify path for conf file. Assuming they are either in C: or D:
$conf_vc = "\\$vc\d$\$cfgdir_vc"
if (!(test-Path $conf_vc)) {$conf_vc = "\\$vc\c$\$cfgdir_vc"}
if (!(test-Path $conf_vc)) {write-host "ERROR: VC service conf file location not found!" -foregroundcolor red ; $cont = $false}

$conf_is = "\\$vc\c$\$cfgdir_is"
if (!(test-Path $conf_is)) {$conf_is = "\\$vc\d$\$cfgdir_is"}
if (!(test-Path $conf_is)) {write-host "ERROR: Inventory service conf file location not found!" -foregroundcolor red ; $cont = $false}

$conf_ps = "\\$vc\d$\$cfgdir_ps"
if (!(test-Path $conf_ps)) {$conf_ps = "\\$vc\c$\$cfgdir_ps"}
if (!(test-Path $conf_ps)) {write-host "ERROR: Profile-driven storage service conf file location not found!" -foregroundcolor red ; $cont = $false}

if (!$cont) {exit}

get-content $conf_vc | ? { $_ -match "wrapper\.java\.additional\.9" } | % { if ($_ -match "\d\d\d*"){[int]$jvm_vc = $matches[0]}}
get-content $conf_is | ? { $_ -match "wrapper\.java\.maxmemory" } | % { if ($_ -match "\d\d\d*"){[int]$jvm_is = $matches[0]}}
get-content $conf_ps | ? { $_ -match "wrapper\.java\.maxmemory" } | % { if ($_ -match "\d\d\d*"){[int]$jvm_ps = $matches[0]}}

switch ($vc_size.ToUpper())
  {
    "S" { $jvm_vc_new = 1024; $jvm_is_new = 3072 ; $jvm_ps_new = 1024}
    "M" { $jvm_vc_new = 2048; $jvm_is_new = 6144 ; $jvm_ps_new = 1024}
    "L" { $jvm_vc_new = 3072; $jvm_is_new = 12288 ; $jvm_ps_new = 4096}
  }

Write-Host "*** Host is $vc ***`n" -foregroundcolor green
Write-Host "VC JVM heap size : $jvm_vc"
Write-Host "IS JVM heap size : $jvm_is"
Write-Host "PS JVM heap size : $jvm_ps`n"
Write-Host "*** vCenter size to set is $vc_size ***" -backgroundcolor blue
Write-Host "### New values are... ###`n"
Write-Host "New VC JVM heap size : $jvm_vc_new" -foregroundcolor yellow 
Write-Host "New IS JVM heap size : $jvm_is_new" -foregroundcolor yellow 
Write-Host "New PS JVM heap size : $jvm_ps_new`n"  -foregroundcolor yellow 

if (!$auto) {[string]$ans = Read-Host -prompt "Confirm new settings; services will be restarted? [Y/N]"}

If ($auto -or ($ans.ToUpper() -eq "Y")) {

Copy-Item $conf_vc "$conf_vc.bak1" -Force
(get-content $conf_vc) | 
 % {if ($_ -match "wrapper\.java\.additional\.9") {
      if ($jvm_vc -ne $jvm_vc_new) {$_ -replace "\d\d\d*",[string]$jvm_vc_new } 
      } 
	else {$_}
 }| Set-Content $conf_vc

Copy-Item $conf_is "$conf_is.bak1" -Force
(get-content $conf_is) | 
 % {if ($_ -match "wrapper\.java\.maxmemory") {
      if ($jvm_is -ne $jvm_is_new) {$_ -replace "\d\d\d*",[string]$jvm_is_new } 
      } 
 	else {$_}
 }| Set-Content $conf_is 
 
 Copy-Item $conf_ps "$conf_ps.bak1" -Force
 (get-content $conf_ps) | 
  % {if ($_ -match "wrapper\.java\.maxmemory") {
      if ($jvm_ps -ne $jvm_ps_new) {$_ -replace "\d\d\d*",[string]$jvm_ps_new } 
      } 
 	else {$_}
 }| Set-Content $conf_ps

Restart-Svc -svc "vpxd" -hostname $vc 
Restart-Svc -svc "vimQueryService" -hostname $vc
Restart-Svc -svc "vimPBSM" -hostname $vc

}
Advertisements
 
Leave a comment

Posted by on July 10, 2015 in powershell, Scripts, vmware

 

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: