Skip to content

Instantly share code, notes, and snippets.

@vmwarecode
Created June 1, 2016 08:29
Show Gist options
  • Select an option

  • Save vmwarecode/5053796fca6f657a0259c1b6d1e277d5 to your computer and use it in GitHub Desktop.

Select an option

Save vmwarecode/5053796fca6f657a0259c1b6d1e277d5 to your computer and use it in GitHub Desktop.
Get-INFRA statistics from VROPS for 24h uptime
########################################################################
# Code Generated By: Win@lex16
# Generated On: 14/03/2016 08:21
# Generated By: VROPS script
########################################################################
#Variables
$Vcenter = Read-host "vCenter target server"
$Vrops = Read-host "VROPS target server"
$Date = Get-Date -format "dd/MM/yyyy HH:mm"
#Connexion vcenter and vrops
$TrashOut = Add-PSSnapin VMware.VimAutomation.Core
$TrashOut = Import-Module Vmware.VimAutomation.VROps
$TrashOut = Connect-VIServer $Vcenter # -Credential (Get-Credential " VCENTER ")
$TrashOut = Connect-OMServer -Server $Vrops -user -password (Get-Credential " VROPS ")
function ClusterInventory
{
$clusters = Get-Cluster
$clusters = $clusters.name
Foreach ($cluster in $clusters)
{
$DateFile = Get-Date -format 'ddmmyyyy'
$pathhtml = "C:\apps\vrops\$Cluster.html"
$patharchive = "C:\apps\vrops\Archives"
$testpath = (Test-path $patharchive)
If ($testpath -eq $false)
{
$TrashOut = New-Item $patharchive -type directory
}
$testpath = (Test-path $pathhtml)
If ($testpath -eq $True)
{
$archivefile = "C:\apps\vrops\$Datefile-$Cluster.html"
$TrashOut = Copy-Item $pathhtml $archivefile
$TrashOut = Move-Item $archivefile "$patharchive"
$TrashOut = Remove-Item $pathhtml
}
################################################################################################################################################
# head of output html file
################################################################################################################################################
add-content -Path $pathhtml -value '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
add-content -Path $pathhtml -value '<html xmlns="http://www.w3.org/1999/xhtml">'
add-content -Path $pathhtml -value '<head>'
add-content -Path $pathhtml -value "<h1>Report VROPS Extract 24 hours<br><br>$Vcenter<br><br>$cluster</h1><br>This report was ran: $(Get-Date)"
add-content -Path $pathhtml -value "</head><body>"
add-content -Path $pathhtml -value "<h2>ESXi List</h2>"
add-content -Path $pathhtml -value "<table>"
add-content -Path $pathhtml -value "<tr><th>ESXi Name</th><th>Version</th><th>Statut</th><th>PowerState</th><th>NB CPU</th><th>CpuUsageMhz</th><th>CpuTotalMhz</th><th>MemoryUsageGB</th><th>MemoryTotalGB</th></tr>"
add-content -Path $pathhtml -value "<tr><td>"
#########################################################################################################################################################################################################################
$ESXi = Get-VmHost -Location $cluster
# first step ==>> list esxi by cluster
Foreach($item in $ESXi)
{
$ESXiName = $($item.name)
$ESXiVersion = $($item.Version)
$ESXiConnectionState = $($item.ConnectionState)
$ESXiPowerState = $($item.PowerState)
$ESXiNumCPU = $($item.NumCPU)
$ESXiCpuUsageMhz = $([math]::round($item.CpuUsageMhz))
$ESXiCpuTotalMhz = $([math]::round($item.CpuTotalMhz))
$ESXiMemoryUsageGB = $([math]::round($item.MemoryUsageGB))
$ESXiMemoryTotalGB = $([math]::round($item.MemoryTotalGB))
#######################################################################################################################################################################################################################################################################
add-content -Path $pathhtml -value "<tr><th>$ESXiName</th><th>$ESXiVersion</th><th>$ESXiConnectionStateatut</th><th>$ESXiPowerState</th><th>$ESXiNumCPU</th><th>$ESXiCpuUsageMhz</th><th>$ESXiCpuTotalMhz</th><th>$ESXiMemoryUsageGB</th><th>$ESXiMemoryTotalGB</th></tr>"
#######################################################################################################################################################################################################################################################################
}
#######################################################################################################################################################################################################################################################################
add-content -Path $pathhtml -value "</table>"
add-content -Path $pathhtml -value "<h2>Datastore List</h2>"
add-content -Path $pathhtml -value "<table>"
add-content -Path $pathhtml -value "<tr><th>DataStore Name</th><th>Type</th><th>State</th><th>FreeSpaceGB</th><th>CapacityGB</th></tr>"
add-content -Path $pathhtml -value "<tr><td>"
#######################################################################################################################################################################################################################################################################
$Dt = Get-VMhost -Location $cluster | Get-Datastore
# next step ==>> list datastores by cluster
Foreach($item in $Dt)
{
$DtName = $($item.name)
$DtType = $($item.type)
$DtState = $($item.State)
$DtFreeSpaceGB = $([math]::round($item.FreeSpaceGB,2))
$DtCapacityGB = $([math]::round($item.CapacityGB,2))
#######################################################################################################################################################################################################################################################################
add-content -Path $pathhtml -value "<tr><th>$DtName</th><th>$DtType</th><th>$DtState</th><th>$DtFreeSpaceGB</th><th>$DtCapacityGB</th></tr>"
#######################################################################################################################################################################################################################################################################
}
# second step ==>> create archive files
$path = "C:\apps\vrops\$cluster.csv"
$TrashOut = new-item $path -type file -force
#add description top of csv
$TrashOut = Set-Content $path -value "Name;Cpu;Ram;state"
# third step ==>> get vm list by cluster and export inside csv file
$VMs = Get-Vm -location $cluster
$VM = $VMs.name
Foreach ($VM in $VMs)
{
$Cpu = $VM.NumCpu
$Ram = $VM.MemoryMB
$State = $VM.PowerState
Add-Content $path -value "$VM;$Cpu;$Ram;$State;"
}
# Read CSV File and fixe param
$inventory = Import-Csv -delimiter ";" -path $path
################################################################################################################################################################################################################################################################################################
add-content -Path $pathhtml -value "</table>"
add-content -Path $pathhtml -value "<h2>Vm List</h2>"
add-content -Path $pathhtml -value "<table>"
add-content -Path $pathhtml -value "<tr><th>VM Name</th><th>VCPU</th><th>RAM</th><th>Status</th><th>Usage vcpu MAX</th><th>Usage vcpu MIN</th><th>Usage vcpu AVERAGE</th><th>Usage RAM MAX</th><th>Usage RAM MIN</th><th>Usage RAM AVERAGE</th><th>Usage IOPs MAX</th><th>Usage IOPs MIN</th><th>Usage IOPs AVERAGE</th></tr>"
add-content -Path $pathhtml -value "<tr><td>"
################################################################################################################################################################################################################################################################################################
Foreach ($item in $inventory)
{
$VM = $($item.Name)
$vCPUcount = $($item.Cpu)
$MemoryMB = $($item.Ram)
$State = $($item.State)
#Write-host $VM $vCPUcount $MemoryMB $State
#Start VROPS Extract CPU
$VROPScpu = Get-OMStat -Resource $VM -Key 'cpu|usage_average' -ErrorAction SilentlyContinue | Select Resource, Value -Last 288
$VROPScpu = $VROPScpu.value | Measure-Object -max -min -ave
$VROPScpuMax = [math]::round($VROPScpu.maximum,2)
$VROPScpuMin = [math]::round($VROPScpu.minimum,2)
$VROPScpuAve = [math]::round($VROPScpu.average,2)
#Start VROPS Extract RAM
$VROPSmem = Get-OMStat -Resource $VM -Key 'mem|usage_average' -ErrorAction SilentlyContinue | Select Resource, Value -Last 288
$VROPSmem = $VROPSmem.value | Measure-Object -max -min -ave
$VROPSmemMax = [math]::round($VROPSmem.maximum,2)
$VROPSmemMin = [math]::round($VROPSmem.minimum,2)
$VROPSmemAve = [math]::round($VROPSmem.average,2)
#Start VROPS Extract RAM
$VROPSmem = Get-OMStat -Resource $VM -Key 'mem|usage_average' -ErrorAction SilentlyContinue | Select Resource, Value -Last 288
$VROPSmem = $VROPSmem.value | Measure-Object -max -min -ave
$VROPSmemMax = [math]::round($VROPSmem.maximum,2)
$VROPSmemMin = [math]::round($VROPSmem.minimum,2)
$VROPSmemAve = [math]::round($VROPSmem.average,2)
#Start VROPS Extract Disk IOPs
$VROPSdisk = Get-OMStat -Resource $VM -Key 'virtualDisk:Aggregate of all instances|usage' -ErrorAction SilentlyContinue | Select Resource, Value -Last 288
$VROPSdisk = $VROPSdisk.value | Measure-Object -max -min -ave
$VROPSdiskMax = [math]::round($VROPSdisk.maximum,2)
$VROPSdiskMin = [math]::round($VROPSdisk.minimum,2)
$VROPSdiskAve = [math]::round($VROPSdisk.average,2)
#######################################################################################################################################################################################################################################################################
add-content -Path $pathhtml -value "<tr><th>$VM</th><th>$vCPUcount</th><th>$MemoryMB</th><th>$State</th><th>$VROPScpuMax</th><th>$VROPScpuMin</th><th>$VROPScpuAve</th><th>$VROPSmemMax</th><th>$VROPSmemMin</th><th>$VROPSmemAve</th><th>$VROPSdiskMax</th><th>$VROPSdiskMin</th><th>$VROPSdiskAve</th></tr>"
#######################################################################################################################################################################################################################################################################
}
##########################################################
# CSS
add-content -Path $pathhtml -value "</table>"
add-content -Path $pathhtml -value ' <style>table{margin:auto; width:98%}'
add-content -Path $pathhtml -value 'Body{background-color:green; Text-align:Center;}'
add-content -Path $pathhtml -value 'Body{background-color:silver;}'
add-content -Path $pathhtml -value 'th{background-color:white; color:black;}'
add-content -Path $pathhtml -value 'tr{background-color:White; color:; Text-align:Center;}'
add-content -Path $pathhtml -value '</style>'
##########################################################
# Close Html
add-content -Path $pathhtml -value '</body></html>'
##########################################################
$smtpServer = "vip-m-parpop.fr.net.intra"
$smtpFrom = "vcenter-bp2s@bnpparibas.com"
$smtpTo = "alexandre.vaussant@bnpparibas.com"
$messageSubject = " VWware Clusters Reports $Cluster "
$att = new-object Net.Mail.Attachment($pathhtml)
$texte = " Extract from VROPS during last 24 hours. "
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto , $text
$message.Subject = $messageSubject
$message.Attachments.Add($att)
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
}
}
Function PurgeLogs
{
# Delete all Files in D:\apps older than 30 day(s)
$Path = "C:\apps\vrops\Archives"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
$OutFile = Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
}
ClusterInventory
Start-Sleep -s 30
PurgeLogs
$TrashOut = Disconnect-VIServer -server $Vcenter -Confirm:$False # Disconnect from vCenter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment