Get Daily CPU statistics in your mail

Thanks to the previous post (http://lucd.info/?p=1455) and a little extra scripting, i am able to run a script which will run daily scheduled and email the results to my mailbox.

This script will mail a CSV file with all cpu averages in 5 minutes samples between 7.30 and 19:30 off a specific Virtual machine.

If other counters are needed, you can find them in the following document: http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/visdk25programmingguide.pdf

Beneath is the script i’m using (change all bold items):

Add-PSSnapin VMware.VimAutomation.Core

# Mail variables
$enablemail="yes"
$smtpServer = "smtpserver"
$mailfrom = "emailadres"
$mailto = "emailadres1,emailadres2"

# Server Variables
$VIServer = "viserver"
$vmName = "vmname"

# Log File Location
$log = "path\CPU_Stats" + "_" + $VMname+ ".csv"

# Connect to the VI Server
Connect-VIServer $VIServer

# Script
$esxImpl = Get-VM -Name $vmName
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
$dayStart = New-Object DateTime(1,1,1,7,30,0)     # 07:30 AM
$dayEnd = New-Object DateTime(1,1,1,19,30,0)      # 07:30 PM

$stats = Get-Stat -Entity $esxImpl -Stat cpu.usage.average -Start $todayMidnight.AddDays(-0) -Finish $todayMidnight.AddDays(+1)
$report = $stats | Where-Object {
	$workingDays -contains $_.Timestamp.DayOfWeek -and
	$_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
	$_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
}

$report | Export-Csv $log -NoTypeInformation

if ($enablemail -match "yes")
{
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($log)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $mailfrom
$msg.To.Add($mailto)
$msg.Subject = “CPU Statistics $VMname”
$msg.Body = “Business-hours CPU Statistics are attached   ”
$msg.Attachments.Add($att)
$smtp.Send($msg)
}

$VIServer | Disconnect-VIServer -Confirm:$false

One thought on “Get Daily CPU statistics in your mail

  1. Pingback: facebook

Leave a Reply

Your email address will not be published. Required fields are marked *