VMware Cloud Community
swamynaveen
Enthusiast
Enthusiast
Jump to solution

PowerCLi script to consolidate all the hosts which are having Ramdiskfull errors

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

 

 

 

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

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

swamynaveen
Enthusiast
Enthusiast
Jump to solution

@LucD @Thanks a lot guru for the update. I will run the script and update the status.

0 Kudos
swamynaveen
Enthusiast
Enthusiast
Jump to solution

@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.

 

NameCluster NamevCenterRamDiskRamDiskUsed
hostname1Cluster1vCenter1vsantraces90
hostname2Cluster2vCenter2tmp93
hostname3Cluster3vCenter3var98
hostname4Cluster4vCenter6tmp100
hostname4Cluster5vCenter5var100
hostname6Cluster4vCenter6tmp100
hostname7Cluster4vCenter6tmp100

 

#######################################################

$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

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
swamynaveen
Enthusiast
Enthusiast
Jump to solution

@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

0 Kudos