Tuesday 28 September 2010

Reporting ESX datastore usage

The NAS that our datastores are held on don't understand the vmfs disk format, and report that the LUNs are near full when they aren't - just I've touched over 90% of sectors at some point. The below is scheduled as a weekly report and reports usage in total to a CSV file and datastores over 75% used in the message body. I'm going to use the HTML conversion routine a lot more now, it's miles better than attaching CSV files - here's to uglier looking emails..

+===============================================+

add-pssnapin VMware.VimAutomation.Core #see? useful already.

$myVCServer="myvcsvr.example.com"

$mySMTPServer = "smtp.example.com"

#munge a date-time and report filename together
$now=get-date -format "MMM-dd_hhhh-mm-ss"
$filename = "d:\temp\diskusage_" + $now + ".csv"
$htmlfilename = "d:\temp\diskusage_" + $now + ".html"
#probably this is redundant
$file=$filename

#connect
connect-viserver $myVCServer

#get our snapshot list
$report=get-datastore|select-object Name, FreeSpaceMB, CapacityMB, @{Name="PctUsed";expression={[math]::Round(100-(100/$_.CapacityMB)*$_.FreeSpaceMB)}} | sort-object -property PctUsed
$report | export-csv $filename -NoTypeInformation
$summary=$report|where-object {$_.PctUsed -ge 75}
$summary|convertto-html|out-file $htmlfilename

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($file)
$smtp = new-object Net.Mail.SmtpClient($mySMTPServer)
#mail the file out
$msg.From = "helpdesk@example.com"
#$msg.To.Add("admin@example.com")
$msg.To.Add("helpdesk@example.com")
$msg.To.Add("barry@example.com")
$msg.Subject = "ESX environment - Disk utilisation"
#$msg.Body = "Utilisation of each datastore, generated $now`n"
#$msg.Body = $msg.Body + "This report runs weekly from $myVCServer.`n"
#$msg.Body = $msg.Body + "Full report attached.`n`n`nDisks greater than 75% usage:`n"
#$msg.Body = $msg.Body + {$report|select-object Name, PctUsed|where-object {$_.PctUsed -ge 75}}
$mycontent=get-content $htmlfilename
$msg.body = $mycontent
$msg.Attachments.Add($att)
$msg.IsBodyHTML = $true
$smtp.Send($msg)
$att.Dispose()

+===============================================+

No comments:

Post a Comment