VMware Cloud Community
Sulrich
Contributor
Contributor
Jump to solution

I need to capture -Realtime stats of 6 VM's every hour, 24x7 for 1 month for a software exercise

Hello VMware Community,

I learned that not every VM has the same Statistics available to aggregate.
When I run Get-StatType -Entity "VM-Name" the command returns all stats available. I have copied them and pasted them into excel (sort them so they are descending) and the 5 VMs have different StatTypes available.
All VM''s are vmx13/v13, four of them are built from the same template and only 3 of those have all the same StatTypes, 1 Winodws vm is different and the 1 Linux vm is different than the others.
So, I went thru them all and found common StatTypes to meet my requirements.


I found the code below and have worked on it for a few days to modify it for my requirements.
Objective of this script is to run every hour, 24hrs a day, via a windows task.
There is a variable $fileName that it generates a new file for each VM listed in a csv file it references, it generates a filename with vmname and the date. It should append every hour and at 12midnight when it runs generate a new file with the vm name and the new date. Currently it does not append since it isnt using the Export-CSV function.
Some of the code is grey in notepad++ but I cannot seem to find the offending code that is offsetting everything below. **I have attached a screenshot of this behavior if it helps.


I would like this file to append to the csv file every time it runs but fails when I comment out the $fileName line at the top of the script and add below where $fileName is located with " | Export-Csv -Path "/home/<file_directory>/$server-$(get-date -f yyyy-MM-dd).csv" -Append"
I would like to also capture but cannot seem to find details on how to, i dont know if there would be an array, etc. I'm not a coder but took a few coding classes in college so I can follow what the code is doing.
-Allocated Memory (MB or GB)
-Num CPU's allocated (1,2,3,4 etc)
-CPU Clock Speed per CPU (x.xGhz)
-CPU min/max, I have AVG already
-Total memory allocated
-Memory min/max, I have AVG already
-VM disk IOPs


Can I round to the nearest whole number or extend like 6 decimal places for more accuracy? Every returned value for mem.usage.average ends in .99 (like 2.99, or 4.99, no whole numbers)
The power.power.average has lots of zeros or low numbers like 1,2,3, which is strange because zero watts for a running VM?
(metal server is using 110-140 Watts)

I posted an example of my old script that didn't really work well or capture what I needed it to about 2-3 weeks ago and @LucD replied with a suggestion, but then the boss changed the requirements last Wednesday..

Can someone help me like (@Lucd) to tweak this code to do what i need it to do?

Thank you so very much in advance,
Stephen


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Connect-VIServer -Server '#####IP ADDR#####' -Protocol https -User "administrator@vsphere.local" -Password '#####PASS#####'

#only return VM's I specify
$servers=Get-Content /home/'####PATH######'/vms_SH50.csv #vm names in vmware server

foreach ($server in $servers)
{
$vm=Get-VM $server
if($? -eq $false)
{
continue
}

$fileName="/home/'#####PATH#####'/$server-$(get-date -f yyyy-MM-dd).csv"

$virtualdiskwriteAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘virtualDisk.write.average’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$virtualdiskreadAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘virtualDisk.read.average’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$networkbytesRXAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat 'net.bytesRx.average' | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$networkbytesTXAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘net.bytesTx.average' | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$networkAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat 'net.usage.average' | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$powerusageAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘power.power.average’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$memactiveAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘mem.active.average’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$CPUsummation=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘cpu.used.summation’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$CPUAVG=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘cpu.usage.average’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

$memusageaverage=Get-Stat -entity $vm -MaxSamples 180 -Realtime -stat ‘mem.usage.average’ | ? { $_.instance -eq "" } | select Entity,Timestamp,Value | Sort-Object Timestamp

Set-Content -Value "Server_Name , AVG_CPU_Time , AVG_CPU (%) , CPU_Used (millisecond), Active_MEM (kiloBytes) , AVG_MEM (%) , Disk_Write_Avg (kiloBytes Per Sec) , Disk_Read_Avg (kiloBytes Per Second) , Net_AVG_Tx (KiB) , Net_AVG_Rx (KiB) , AVG_Net (kiloBytes Per Second) , Watts" -Path $fileName

$diskus=0

for ($i=0;$i -lt $CPUAVG.Count ; $i++)

{

if (($diskus -lt $virtualdiskwriteAVG.count) -and ($CPUAVG[$i].Timestamp -eq $virtualdiskwriteAVG[$diskus].Timestamp))
{

Add-Content -Value "$($CPUAVG[$i].Entity.Name),$($CPUAVG[$i].Timestamp),$($CPUAVG[$i].Value),$($CPUsummation[$i].Value),$($memactiveAVG[$i].Value),$($memusageaverage[$i].Value),$($virtualdiskwriteAVG[$diskus].Value),$($virtualdiskreadAVG[$diskus].Value),$($networkbytesTXAVG[$i].Value),$($networkbytesRXAVG[$i].Value),$($networkAVG[$i].Value),$($powerusageAVG[$i].Value)" -Path $fileName

$diskus++
}
else
{
Add-Content -Value "$($CPUAVG[$i].Entity.Name),$($CPUAVG[$i].Timestamp),$($CPUAVG[$i].Value),$($memusageaverage[$i].Value),0,$($netusageaverage[$i].Value)" -Path $fileName
}
}
}

return(0)
Disconnect-VIServer -Confirm:$false

#######################################################################################
# Metrics collection
#-----------------------------------------------------------------------------------
#mem.usage.average Memory usage as percent of total configured or available memory. | percent
#mem.active.average The amount of memory used by the VM in the past small window of time. This is the "true" number of how much memory the VM currently has need of. Additional, unused memory may be swapped out or ballooned with no impact to the guest's performance. | kiloBytes
#cpu.used.summation CPU Used | millisecond
#cpu.usage.average The CPU utilization. This value is reported with 100% representing all processor cores on the system. As an example, a 2-way VM using 50% of a four-core system is completely using two cores. | percent
#net.bytesRx.average Average amount of data received per second. network adapter | KiB
#net.bytesTx.average Average amount of data transmitted per second. network adapter | KiB
#net.usage.average Network Usage (Average) | kiloBytesPerSecond
#power.power.average Current power usage. | Watts
#virtualDisk.write.average Average number of kilobytes written to the virtual disk each second. | KiB
#virtualDisk.read.average Average number of kilobytes written to the virtual disk each second. | KiB

0 Kudos
22 Replies
LucD
Leadership
Leadership
Jump to solution

Do you see the GPU under Monitoring - Performance- Advanced in the Web Client?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sulrich
Contributor
Contributor
Jump to solution

No, the esxi driver is not installed in the host since I’m using direct io passthrough. It’s needed?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid I don't know that.
Perhaps ask the question in the ESXi Community


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference