Thursday 13 September 2012

powershell script to check the cluster volume storage usage

Another powershell script to check the disk usage on the CSV's

Import-Module FailoverClusters

#Function to send an email
function sendMail($body)
{
    $smtpServer = "192.168.1.25"
    $emailFrom = "ClusterStorageScript@email.ie" 
    $emailTo = ("alerts@email.com")
    $subject = "Cluster disk space usage"
    Send-MailMessage -From $emailFrom -To $emailTo -Subject $subject  -Body $body -SmtpServer $smtpServer
}


#Set up required arrays
$objs = @()
$alerts = @()

#code snippet to get cluster information
$csvs = Get-ClusterSharedVolume
foreach ( $csv in $csvs )
{
   $csvinfos = $csv | select -Property Name -ExpandProperty SharedVolumeInfo
   foreach ( $csvinfo in $csvinfos )
   {
      $obj = New-Object PSObject -Property @{
         Name        = $csv.Name
         Path        = $csvinfo.FriendlyVolumeName
         Size        = $csvinfo.Partition.Size
         FreeSpace   = $csvinfo.Partition.FreeSpace
         UsedSpace   = $csvinfo.Partition.UsedSpace
         PercentFree = $csvinfo.Partition.PercentFree
      }
      $objs += $obj
   }
#If there is less than 25% free add an alert to our alert array
#set sendalert to 1
   if ($csvinfo.Partition.PercentFree -le 25) 
   {
        $75alert =  "Warning 75% or more disk usage on " + $csv.Name + "`r`n"
        $alerts += $75alert
        $sendalert = 1
   }
#Similar as above adds alert for less than 20% free
   if ($csvinfo.Partition.PercentFree -le 20) 
   {
       $80alert =  "CRITICAL!!! 80% or more disk usage on " + $csv.Name + "`r`n"
       $alerts += $80alert
       $sendalert = 1
   }
     
}
#If send alert is set to 1 above then do the following
if ($sendalert -eq 1)
{
#Put all the alerts into a string $smsg
$a = 0
$smsg = ""
while ($a -le $alerts.count)
{
$smsg += $alerts[$a] + "`r`n"
$a++
}
#Put the full output of the cluster information into a string $msg
$objs2 = $objs | ft Name,@{ Label = "Size(GB)" ; Expression = { "{0,8:N2}" -f ($_.Size/1024/1024/1024) } },@{ Label = "FreeSpace(GB)" ; Expression = { "{0,13:N2}" -f ($_.FreeSpace/1024/1024/1024) } },@{ Label = "UsedSpace(GB)" ; Expression = { "{0,13:N2}" -f ($_.UsedSpace/1024/1024/1024) } },@{ Label = "PercentFree" ; Expression = { "{0,11:N2}" -f ($_.PercentFree) } }
$msg = out-string -inputobject $objs2 -width 85
#append new lings to $smsgm
$smsg += "`r`n`r`n"
#crate a new string comprised of the alerts and the full information
$emailmsg = $smsg + $msg
write-host $emailmsg
#email all of the information using the function at the top of this script
sendMail($emailmsg)

}

No comments:

Post a Comment