VMware Cloud Community
SetClear
Enthusiast
Enthusiast
Jump to solution

Monitoring ESX Host Storage Usage (eg: /var/log) with PowerShell

Is there a way to get information like that provided with vdf from VITK-W?

Are there API calls to the SDK that can be used to get these details?

Had a ESX host run out of log space and it took it down. I have created a script to archive the logs but would like to take a more proactive approach to monitoring the Hosts usage.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can move the ConvertTo-Html cmdlet outside the loop and feed it an array ($report) that holds all the custom objects ($line).

That way you get a nice table with all the entries for a specific host.

      $report = @()
	foreach ($dfl in $esx_df)
	{ 
		$dcnter += 1
		if ($dcnter -gt 1)
		{
			$cnter = 0
			$dfa = $dfl.split("",[StringSplitOptions]::RemoveEmptyEntries)
			$line = "" | select FileSystem, Size, Used, Available, UsedPerc, MountPoint, Warning
			foreach ($dfal in $dfa)
			{
				$cnter += 1
				if ($cnter -eq 1)
				{					Write-Host "FileSystem:" $dfal
					$line.FileSystem = $dfal
				}
				elseif ($cnter -eq 2)
				{					Write-Host "Size:" $dfal
					$line.Size = $dfal
				}
				elseif ($cnter -eq 3)
				{					Write-Host "Used:" $dfal
					$line.Used = $dfal
				}
				elseif ($cnter -eq 4)
				{					Write-Host "Avail:" $dfal
					$line.Available = $dfal
				}
				elseif ($cnter -eq 5)
				{					Write-Host "Used%:" $dfal
					$line.UsedPerc = $dfal
					if ([Int]$dfal.Trim("%") -gt 75)
					{						Write-Host "Usage over 75%"
						$line.Warning = "Warning Storage Usage is above 75%"
					}
				}
				elseif ($cnter -eq 6)
				{					Write-Host "Mount Point:" $dfal
					$line.MountPoint = $dfal
				}
			}
			$report += $line
		}
	}
	$report | ConvertTo-Html –title "VMware VI3 Server Storage ... type='text/css'></![CDATA[>" | Out-File -Append $filelocation

As a side remark, the ConvertTo-Html cmdlet is rather limited and cumbersome for producing fancy web pages.

Have a look at this Windows PowerShell Tip of the Week to see how this can be ameliorated.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Access to ESX filesystem, the datastores excluded, is not possible with the VITK nor with any of the SDK methods that I know of.

In Hal's blog there an article that describes how to do this with the help of plink.exe


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

SetClear
Enthusiast
Enthusiast
Jump to solution

Ok,

So I have the code to gather the information and also to maintain the volumes.

What I would like to do with the data is output it into a HTML document and mail it to the team which monitors it daily.

The issue I am having is that its not a .NET object so the ConvertTo-HTML does not work and converts my output to a set of numbers.

Can anyone help with making this possible?

Here is the code: (Formatting is a bit off so I have attached the code also.)

$vcserver="localhost"

Add-PSsnapin VMware.VimAutomation.Core

Initialize-VIToolkitEnvironment.ps1

connect-VIServer $vcserver

$filelocation="C:\Scripts\VI3_SSI.html"

$smtpServer = "xxx.xxx.xxx.xxx"

$mailfrom = "ESX Storage &lt;XXXXXX@XXXXXXXX.XXX&gt;"

$mailto = "XXXXXX@XXXXXXXX.XXX"

ConvertTo-Html –title "VI3 Server Storage Information " –body "&lt;H1&gt;VI3 Server Storage Information&lt;/H1&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File $filelocation

ConvertTo-Html –title " VI3 Server Storage Information " –body "&lt;H4&gt;Date and time&lt;/H4&gt;",$date -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

  1. VMware ESX Volume Information

get-vmhost | sort-object -unique | ForEach-Object -process {

$esx_df = & "C:\Program Files\Veeam\Veeam Backup and FastSCP\Putty\plink" -pw XXXXXXXX root@$_ df -h

$cnter = 0

$dcnter = 0

$_.Name | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

foreach ($dfl in $esx_df)

{

$dcnter += 1

if ($dcnter -gt 1)

{

$cnter = 0

$dfa = $dfl.split("",[StringSplitOptions]::RemoveEmptyEntries)

foreach ($dfal in $dfa)

{

$cnter += 1

if ($cnter -eq 1)

{ Write-Host "FileSystem:" $dfal

$dfal | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

}

ElseIf ($cnter -eq 2)

{ Write-Host "Size:" $dfal

$dfal | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

}

ElseIf ($cnter -eq 3)

{ Write-Host "Used:" $dfal

$dfal | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

}

ElseIf ($cnter -eq 4)

{ Write-Host "Avail:" $dfal

$dfal | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

}

ElseIf ($cnter -eq 5)

{ Write-Host "Used%:" $dfal

$dfal | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

if ($dfal.Trim("%") -gt 75)

{Write-Host "Usage over 75%"

"Warning Storage Usage is above 75%" | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

}

}

ElseIf ($cnter -eq 6)

{ Write-Host "Mount Point:" $dfal

$dfal | ConvertTo-Html –title "VMware VI3 Server Storage Configuration" –body "&lt;H2&gt;VMware VI3 Server Storage Configuration.&lt;/H2&gt;" -head "&lt;link rel='stylesheet' href='style.css' type='text/css' /&gt;" | Out-File -Append $filelocation

}

}

}

}

}

  1. E-mail HTML output

$msg = new-object Net.Mail.MailMessage

$att = new-object Net.Mail.Attachment($filelocation)

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = $mailfrom

$msg.To.Add($mailto)

$msg.Subject = “VMware Healthscript”

$msg.Body = “VMware healthscript”

$msg.Attachments.Add($att)

$smtp.Send($msg)

  1. Disconnect session from VC #

disconnect-viserver -confirm:$false

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The easiest way is to create a custom PSObject and feed that to the ConvertTo-Html cmdlet.

The following shows how you can do this for the output of the 'df -h' command

...
	foreach ($dfl in $esx_df)
	{ 
		$dcnter += 1
		if ($dcnter -gt 1)
		{
			$cnter = 0
			$dfa = $dfl.split("",[StringSplitOptions]::RemoveEmptyEntries)
			$line = "" | select FileSystem, Size, Used, Available, UsedPerc, MountPoint, Warning
			foreach ($dfal in $dfa)
			{
				$cnter += 1
				if ($cnter -eq 1)
				{					Write-Host "FileSystem:" $dfal
					$line.FileSystem = $dfal
				}
				elseif ($cnter -eq 2)
				{					Write-Host "Size:" $dfal
					$line.Size = $dfal
				}
				elseif ($cnter -eq 3)
				{					Write-Host "Used:" $dfal
					$line.Used = $dfal
				}
				elseif ($cnter -eq 4)
				{					Write-Host "Avail:" $dfal
					$line.Available = $dfal
				}
				elseif ($cnter -eq 5)
				{					Write-Host "Used%:" $dfal
					$line.UsedPerc = $dfal
					if ([Int]$dfal.Trim("%") -gt 75)
					{						Write-Host "Usage over 75%"
						$line.Warning = "Warning Storage Usage is above 75%"
					}
				}
				elseif ($cnter -eq 6)
				{					Write-Host "Mount Point:" $dfal
					$line.MountPoint = $dfal
				}
			}
			$line | ConvertTo-Html | Out-File -Append $filelocation
		}
	}
...

To make it easier to see the logic I simplified the ConvertTo-Html cmdlet at the end.

Attached your updated script.

The other lines with ConvertTo-Html can be done in a similar way.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

SetClear
Enthusiast
Enthusiast
Jump to solution

LucD,

Thank you for the sound advice(And code)!

When I use your example though it places the html output into seperate tables for each foreach statement.

How would I avoid this and have a single table with all entires for a single server?

I have added a column to the dataset which contains the server/hostname to tell which server is being viewed.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can move the ConvertTo-Html cmdlet outside the loop and feed it an array ($report) that holds all the custom objects ($line).

That way you get a nice table with all the entries for a specific host.

      $report = @()
	foreach ($dfl in $esx_df)
	{ 
		$dcnter += 1
		if ($dcnter -gt 1)
		{
			$cnter = 0
			$dfa = $dfl.split("",[StringSplitOptions]::RemoveEmptyEntries)
			$line = "" | select FileSystem, Size, Used, Available, UsedPerc, MountPoint, Warning
			foreach ($dfal in $dfa)
			{
				$cnter += 1
				if ($cnter -eq 1)
				{					Write-Host "FileSystem:" $dfal
					$line.FileSystem = $dfal
				}
				elseif ($cnter -eq 2)
				{					Write-Host "Size:" $dfal
					$line.Size = $dfal
				}
				elseif ($cnter -eq 3)
				{					Write-Host "Used:" $dfal
					$line.Used = $dfal
				}
				elseif ($cnter -eq 4)
				{					Write-Host "Avail:" $dfal
					$line.Available = $dfal
				}
				elseif ($cnter -eq 5)
				{					Write-Host "Used%:" $dfal
					$line.UsedPerc = $dfal
					if ([Int]$dfal.Trim("%") -gt 75)
					{						Write-Host "Usage over 75%"
						$line.Warning = "Warning Storage Usage is above 75%"
					}
				}
				elseif ($cnter -eq 6)
				{					Write-Host "Mount Point:" $dfal
					$line.MountPoint = $dfal
				}
			}
			$report += $line
		}
	}
	$report | ConvertTo-Html –title "VMware VI3 Server Storage ... type='text/css'></![CDATA[>" | Out-File -Append $filelocation

As a side remark, the ConvertTo-Html cmdlet is rather limited and cumbersome for producing fancy web pages.

Have a look at this Windows PowerShell Tip of the Week to see how this can be ameliorated.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
SetClear
Enthusiast
Enthusiast
Jump to solution

LucD,

Thank you!

below is the final code for the script;

I will also post this as an alteration to the VMware HealthCheck.ps1 which I have altered and added to...

Being that the formating is off I have also attached the code.

-


*

ConvertTo-Html -title "VMware ESX Server Volume Information." -body "&lt;H2&gt;VMware ESX Server Volume Information.&lt;/H2&gt;" -head $a | Out-File -Append $filelocation

*

$report = @()

get-vmhost | sort-object -unique | ForEach-Object -process {

$esx_df = & "C:\Program Files\Veeam\Veeam Backup and FastSCP\Putty\plink" -pw Asd123 root@$_ df -h

$cnter = 0

$dcnter = 0

foreach ($dfl in $esx_df)

{

$dcnter += 1

if ($dcnter -gt 1)

{ $cnter = 0

$dfa = $dfl.split("",[StringSplitOptions]::RemoveEmptyEntries)

$line = "" | select ESXHost, FileSystem, VolumeSize, VolumeUsed, Available, UsedPercent, MountPoint, VolumeWarning

foreach ($dfal in $dfa)

{ $cnter += 1

$line.ESXHost = $_.Name

if ($cnter -eq 1)

{ $line.FileSystem = $dfal }

elseif ($cnter -eq 2)

{ $line.VolumeSize = $dfal }

elseif ($cnter -eq 3)

{ $line.VolumeUsed = $dfal }

elseif ($cnter -eq 4)

{ $line.Available = $dfal }

elseif ($cnter -eq 5)

{ $line.UsedPercent = $dfal

if ($dfal.Trim("%") -gt 75)

{ $line.VolumeWarning = "Warning Volume Usage is above 75%" }

}

elseif ($cnter -eq 6)

{ $line.MountPoint = $dfal }

}

$report += $line

}

}

}

$report | ConvertTo-Html -head $a | Out-File -Append $filelocation

-


0 Kudos