Hi Friends,
I am using the following PowerCLi script to enable mail notifications for all ESXi host which are having Ramdisk full alerts across 70 vCenter servers. but i would like to consolidate all these alerts in single mail thread rather than each host. Any help would be really appreciated.
$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
$VMHosts = Get-VMHost
ForEach ($ESXiHost in $VMHosts) {
$esxcli = Get-EsxCli -VMhost $ESXiHost.Name -V2
#Gather ramdisk usage stats (Runs esxcli system visorfs ramdisk list)
$ESXiRamdisks = $esxcli.system.visorfs.ramdisk.list.Invoke()
ForEach ($Ramdisk in $ESXiRamdisks) {
$RamdiskFree = [int]$Ramdisk.Free
$RamdiskUsed = 100 - $RamdiskFree
$RamdiskName = [string]$Ramdisk.RamdiskName
If ($RamdiskFree -le 10) {
write-host $esxihost
$Report1 = Get-VMHost $esxihost | select Name,@{N="Cluster Name";E={($_ | Get-Cluster).Name}},@{N='vCenter';E={([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host}}
$Report1 = $Report1 | ConvertTo-HTML -Head $Header | Out-String
$sMail = @{
To = "XXXX"
From = "XXXX"
#Cc =
Subject = "Ramdisk $RamdiskName on Host $ESXiHost at $RamdiskUsed% On: $date "
Body = "Ramdisk $RamdiskName on Host $ESXiHost has exceeded 90% utilization, current value is $RamdiskUsed% kindly check it and clear older logs..!!" + " " + $Report1
BodyAsHtml = $true
Priority = 'High'
SmtpServer = "XXXX"
}
Send-MailMessage @smail
}
}
}
Regards,
Naveen
You could collect all the data in an array.
And then send 1 email with that array in a table in the body of the email.
Something like this for example
$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
$report1 = @()
$VMHosts = Get-VMHost
ForEach ($ESXiHost in $VMHosts) {
$esxcli = Get-EsxCli -VMHost $ESXiHost.Name -V2
#Gather ramdisk usage stats (Runs esxcli system visorfs ramdisk list)
$ESXiRamdisks = $esxcli.system.visorfs.ramdisk.list.Invoke()
ForEach ($Ramdisk in $ESXiRamdisks) {
$RamdiskFree = [int]$Ramdisk.Free
$RamdiskUsed = 100 - $RamdiskFree
$RamdiskName = [string]$Ramdisk.RamdiskName
If ($RamdiskFree -le 10) {
Write-Host $esxihost
$Report1 += Get-VMHost $esxihost |
select Name,
@{N = "Cluster Name"; E = { ($_ | Get-Cluster).Name } },
@{N = 'vCenter'; E = { ([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host } },
@{N='RamDisk';E={$Ramdisk.RamdiskName}},
@{N='RamDiskUsed';E={$RamdiskUsed}}
}
}
}
$Report1 = $Report1 | ConvertTo-Html -Head $Header | Out-String
$sMail = @{
To = "XXXX"
From = "XXXX"
#Cc =
Subject = "Ramdisk utilization over 90% On: $date "
Body = $Report1
BodyAsHtml = $true
Priority = 'High'
SmtpServer = "XXXX"
}
Send-MailMessage @smail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could collect all the data in an array.
And then send 1 email with that array in a table in the body of the email.
Something like this for example
$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
$report1 = @()
$VMHosts = Get-VMHost
ForEach ($ESXiHost in $VMHosts) {
$esxcli = Get-EsxCli -VMHost $ESXiHost.Name -V2
#Gather ramdisk usage stats (Runs esxcli system visorfs ramdisk list)
$ESXiRamdisks = $esxcli.system.visorfs.ramdisk.list.Invoke()
ForEach ($Ramdisk in $ESXiRamdisks) {
$RamdiskFree = [int]$Ramdisk.Free
$RamdiskUsed = 100 - $RamdiskFree
$RamdiskName = [string]$Ramdisk.RamdiskName
If ($RamdiskFree -le 10) {
Write-Host $esxihost
$Report1 += Get-VMHost $esxihost |
select Name,
@{N = "Cluster Name"; E = { ($_ | Get-Cluster).Name } },
@{N = 'vCenter'; E = { ([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host } },
@{N='RamDisk';E={$Ramdisk.RamdiskName}},
@{N='RamDiskUsed';E={$RamdiskUsed}}
}
}
}
$Report1 = $Report1 | ConvertTo-Html -Head $Header | Out-String
$sMail = @{
To = "XXXX"
From = "XXXX"
#Cc =
Subject = "Ramdisk utilization over 90% On: $date "
Body = $Report1
BodyAsHtml = $true
Priority = 'High'
SmtpServer = "XXXX"
}
Send-MailMessage @smail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD @Thanks a lot guru for the update. I will run the script and update the status.
@LucD I ran the script and it is working fine as per the requirement and in order to clear up space under ramdisk tmp/var I am trying to use below script however it's not covering all the required ramdisk partitions as some of the hosts have multiple ramdisks getting filled up. so Is there any way we can clear the space from multiple ramdisk partitions by passing host list to other script to initiate clean up task. Kindly advise.
Name | Cluster Name | vCenter | RamDisk | RamDiskUsed |
hostname1 | Cluster1 | vCenter1 | vsantraces | 90 |
hostname2 | Cluster2 | vCenter2 | tmp | 93 |
hostname3 | Cluster3 | vCenter3 | var | 98 |
hostname4 | Cluster4 | vCenter6 | tmp | 100 |
hostname4 | Cluster5 | vCenter5 | var | 100 |
hostname6 | Cluster4 | vCenter6 | tmp | 100 |
hostname7 | Cluster4 | vCenter6 | tmp | 100 |
#######################################################
$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"
$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
$report1 = @()
$VMHosts = Get-VMHost
ForEach ($ESXiHost in $VMHosts) {
$esxcli = Get-EsxCli -VMHost $ESXiHost.Name -V2
#Gather ramdisk usage stats (Runs esxcli system visorfs ramdisk list)
$ESXiRamdisks = $esxcli.system.visorfs.ramdisk.list.Invoke()
ForEach ($Ramdisk in $ESXiRamdisks) {
$RamdiskFree = [int]$Ramdisk.Free
$RamdiskUsed = 100 - $RamdiskFree
$RamdiskName = [string]$Ramdisk.RamdiskName
If ($RamdiskFree -le 10) {
Write-Host $esxihost
$HostList + =Get-VMHost $esxihost | select Name
$Report1 += Get-VMHost $esxihost |
select Name,
@{N='RamDisk Name';E={$Ramdisk.RamdiskName}},
@{N='RamDiskUsed %';E={$RamdiskUsed}},
@{N = "Cluster Name"; E = { ($_ | Get-Cluster).Name } },
@{N = 'vCenter'; E = { ([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host } }
}
}
}
$Report1 = $Report1 | ConvertTo-Html -Head $Header | Out-String
$sMail = @{
To = "XXXXXXX"
From = "XXXXX"
#Cc =
Subject = "Ramdisk utilization over 90% Consolidated Report On: $date "
Body = "Hi Team, Ramdisk is being full on the following vSphere hosts. Kindly check and clear older logs..!! " + $Report1
BodyAsHtml = $true
Priority = 'High'
SmtpServer = "XXXXX"
}
Send-MailMessage @smail
Write-host " Pls wait while clearing space from the Ramdisks...!!"
$vmhosts2 = get-vmhost $HostList
foreach ($esx in $vmhosts2){
$cmd_tmp = @'
rm /tmp/ams-*;
'@
$cmd_var = @'
find /var/log -mtime +1 | grep log.*.[0-9]|xargs rm;
'@
$secPswd = ConvertTo-SecureString 'XXXX' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)
Get-VMHostService -VMHost $esx | where{$_.Key -eq 'TSM-SSH'} | Start-VMHostService -Confirm:$false | Out-Null
$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Select -ExpandProperty Output
Invoke-SSHCommand -SSHSession $session -Command $cmd_var | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null
Get-VMHostService -VMHost $esx | where{$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false | Out-Null
}
############
Regards,
Naveen
Do you actually want the clearing of the RAM disk to be done in the same script that does the reporting?
I also suggest opening a new thread for the clearing of the RAM disks.
That is imho a different subject and would make it easier for others to find the thread.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD Yeah, I've opened the following new thread for the same. Could you pls advise best methods to clear space from RAMDisks as we do not have persistent scratch locations for around 2000 ESX hosts. Hence, We're trying to find best methods to automate the cleanup space by deleting older log files from ESXi hosts.
https://communities.vmware.com/t5/forums/filteredbylabelpage/board-id/2805/label-name/ramdisks
Regards,
Naveen