Friday 21 August 2015

Report Changes to DRS/HA settings

Do your vCenter settings get changed when they shouldn't?  This script will email an alert when anything has changed.  

First you need to run once and it'll create "E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-File.txt" and copy that file to "E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-Baseline.txt".

From then on each time the script runs (schedule in Windows Task Scheduler), a new GetCluster-File is created and compared to the baseline file.

___________________________________________________________________

# created by KC on 2015/06/04
#
#
#pre-reqs are:
# 1.  set vCenter server for Connect command
# 2.  verify/create directory/file
# E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-Baseline.txt (Cam)
# F:\scripts\PowerCLI\logs\HA-DRS-GetCluster-Baseline.txt(Basel)
#
#
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vSphere
#
# after valid change to HA/DRS settings in vSphere, copy
# E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-File.txt
# overwriting and replacing:
# E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-Baseline.txt
# to prevent alerts by this script
#
#get email variables from vCenter 
#
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'
$emailFrom = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.sender"}).Value
$smtpServer = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.smtp.server"}).Value
#set email variables manaully
$emailRcpt = "email@email.com"

$MailSendingTo = "email@email.com"
#
#set variables for baseline file and new cluster-file
#
$GetClusterFile =  'E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-File.txt'
$GetClusterBaseline = 'E:\scripts\PowerCLI\logs\HA-DRS-GetCluster-Baseline.txt'
$GetClusterMail = 'E:\scripts\PowerCLI\logs\HA-DRS-GetClusterMail.txt'
#
#
#collect cluster config info and send to screen
#
Get-Cluster | Sort-Object -Property Name | 
Select-Object -Property Name,HAEnabled,HAAdmissionControlEnabled,
@{N="AdmissionControlPolicy";E={$_.ExtensionData.Configuration.Dasconfig.AdmissionControlPolicy.GetType().Name}},
DrsEnabled,DrsMode,DrsAutomationLevel
#
#
#collect cluster config info and send to file
#
Get-Cluster | Sort-Object -Property Name | 
Select-Object -Property Name,HAEnabled,HAAdmissionControlEnabled,
@{N="AdmissionControlPolicy";E={$_.ExtensionData.Configuration.Dasconfig.AdmissionControlPolicy.GetType().Name}},
DrsEnabled,DrsMode,DrsAutomationLevel | out-file $GetClusterFile
#
# Get remaining cluster info, send to screen
#
Get-Cluster | Select-Object -Property Name,
@{N="CpuFailoverResourcesPercentage";E={$_.ExtensionData.Configuration.DasConfig.AdmissionControlPolicy.CpuFailoverResourcesPercent}},
@{N="MemoryFailoverResourcesPercentage";E={$_.ExtensionData.Configuration.DasConfig.AdmissionControlPolicy.MemoryFailoverResourcesPercent}} 

#
# Get remaining cluster info, appending to file
#
Get-Cluster | Select-Object -Property Name,
@{N="CpuFailoverResourcesPercentage";E={$_.ExtensionData.Configuration.DasConfig.AdmissionControlPolicy.CpuFailoverResourcesPercent}},
@{N="MemoryFailoverResourcesPercentage";E={$_.ExtensionData.Configuration.DasConfig.AdmissionControlPolicy.MemoryFailoverResourcesPercent}} >> $GetClusterFile
#
# load GetCluster info into variable for email later
$hadrs_getclusterfile = Get-Content $GetClusterFile
#$hadrs_baselinefile = Get-Content $GetClusterBaseline
#
#
$Report = Compare-Object -Ref (Get-Content $GetClusterFile) -Diff (Get-Content $GetClusterBaseline) -Verbose | Out-String 

write-output $Report $hadrs_getclusterfile > $GetClusterMail
#
$Same_Or_Diff = Compare-Object -Ref (Get-Content $GetClusterFile) -Diff (Get-Content $GetClusterBaseline) | Measure | select Count
#
#
$z = $Same_Or_Diff.Count
if ( $z -eq 0)
{
write-host $Same_Or_Diff 
write-host "is Same_Or_Diff"
write-host $z 
write-host "is Count of differences, should be equal to 0" 
write-host "which means no change to HA/DRS config at" 

#$Report = "$MailSender HA/DRS config is unchanged, no action needed for $MailSender"
$MailSender 

$Report = (Get-Content $GetClusterFile | out-string)

#commented email so we only get alerts for problems
#email results
#Send-MailMessage -from $emailFrom -to $emailRcpt -subject "HA/DRS Config Alert OK - not changed" -body $Report -smtpServer $SmtpServer
}
#
else
#
{
#
write-host $Same_Or_Diff 
write-host "is Same_Or_Diff"
write-host $z 
write-host "is Count of differences, should not be equal to 0" 
write-host "A change to HA/DRS config at" $MailSender "needs investigation" >> $GetClusterMail
write-host $Report
write-host is email body
#
#
#email results

$Report = (Get-Content $GetClusterMail | out-string)

#Send-MailMessage -from $MailSender -to kevin.cade@napp.co.uk -subject "Alert: HA/DRS Change Config Change Changed" -body $Report -smtpServer $MailSmtpServer

#prepare email
$emailSubject = "Alert: HA/DRS Change Config Changed!" 
$msgBody = $Report
$smtp = New-Object Net.Mail.SmtpClient -arg $smtpServer
#send email
$smtp.Send($emailFrom,$emailRcpt,$emailSubject,$msgBody)

Send-MailMessage -from $emailFrom -to $emailRcpt -subject "HA/DRS Config Alert changed!" -body $Report -smtpServer $SmtpServer


}

disConnect-VIServer vSphere -confirm:$false

No comments:

Post a Comment