VMware Cloud Community
LOAGANATHAN
Enthusiast
Enthusiast
Jump to solution

Script to get VM memory usage with metrics like Cosumed ,active,granted


Hi Friends

Can someone help me to get the powercli scripts to get VM folder wise memory usage with metricks like Consumed,Active,Granted for two week with particular time period for example buisness hours with output to csv file like below

VmnameEsxhostActivememoryGrantedmemoryConsumed memoryFolder
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It looks like the Statistics Level for Historical Interval 3 (past month) is not set to at least 2.

The active and granted counters both require level 1.

mem-active.png

mem-granted.png

The consumed requires level 1

mem-consumed.png


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

What do you already have ?

Where do you get stuck ?


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

0 Kudos
LOAGANATHAN
Enthusiast
Enthusiast
Jump to solution

Hi Lucd

below is the scrip which i hve tried and i didnt get the output values for active,consumed,granted and please let me know how to get the esxhost name to the corresponding Vm in output csv files


Add-PSSnapin -Name "VMware.VimAutomation.Core"


Connect-VIServer "VCserver"
Write-host "Gathering MEM utilization  VMs for last 2 weeks"

$metrics = "mem.active.average","mem.consumed.average","mem.granted.average"

#$start = (Get-Date).AddDays(-7)
#$start=$todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7)
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
$dayStart = New-Object DateTime(2014,07,01,00,00,0)     # 00:00 AM
$dayEnd = New-Object DateTime(2014,07,20,23,59,0)      # 23:59 PM
 
$folders = Get-Folder -Location folder1
&{foreach($folder in $folders){
    $vms = Get-VM -Location $folder
    if($vms){
      $stats = Get-Stat -Entity $vms -Stat $metrics -Start $todayMidnight.AddDays(-15) -Finish $todayMidnight.AddDays(-1) -ErrorAction SilentlyContinue
      #$stats = Get-Stat -Entity $vms -Stat $metrics -Start $todayMidnight.AddDays(-7) -ErrorAction SilentlyContinue
$report = $stats | Where-Object {
$workingDays -contains $_.Timestamp.DayOfWeek -and
$_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
$_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
}

     if($report){
        $report | Group-Object -Property {$_.Entity.Name} | %{
        $Memactive = $_.Group | where {$_.MetricId -eq "Mem.active.average"} | Measure-Object -Property Value -Average
        $Memconsumed = $_.Group | where {$_.MetricId -eq "Mem.consumed.average"} | Measure-Object -Property Value -Average
        $Memgranted = $_.Group | where {$_.MetricId -eq "Mem.granted.average"} | Measure-Object -Property Value -Average

           New-Object PSObject -Property @{
           Folder = $folder.Name
           VM = $_.Values[0]
          
         

            Memactive = $Memactive
            Memconsumed = $Memconsumed
            Memgranted = $Memgranted
        }
      }
    }
  }}} | Export-Csv C:\users\administrator\desktop\vm.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to have forgotten the selection of the Average

Try like this

Add-PSSnapin -Name "VMware.VimAutomation.Core"
Connect-VIServer "VCserver"

Write-host "Gathering MEM utilization  VMs for last 2 weeks"

$metrics = "mem.active.average","mem.consumed.average","mem.granted.average"
$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
$dayStart = New-Object DateTime(2014,07,01,00,00,0)     # 00:00 AM
$dayEnd = New-Object DateTime(2014,07,20,23,59,0)      # 23:59 PM

$folders = Get-Folder -Location Folder1
&{foreach($folder in $folders){
   
$vms = Get-VM -Location $folder
   
if($vms){
       
$stats = Get-Stat -Entity $vms -Stat $metrics -Start $todayMidnight.AddDays(-15) -Finish $todayMidnight.AddDays(-1) -ErrorAction SilentlyContinue
       
$report = $stats | Where-Object {
           
$workingDays -contains $_.Timestamp.DayOfWeek -and
           
$_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
           
$_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
        }

       
if($report){
           
$report | Group-Object -Property {$_.Entity.Name} | %{
               
$Memactive = $_.Group | where {$_.MetricId -eq "Mem.active.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
               
$Memconsumed = $_.Group | where {$_.MetricId -eq "Mem.consumed.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average
               
$Memgranted = $_.Group | where {$_.MetricId -eq "Mem.granted.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average

               
New-Object PSObject -Property @{
                   
Folder = $folder.Name
                   
VM = $_.Values[0]
                   
Memactive = $Memactive
                   
Memconsumed = $Memconsumed
                   
Memgranted = $Memgranted
                }
            }
        }
  }}}
| Export-Csv C:\users\administrator\desktop\vm.csv -NoTypeInformation -UseCulture


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

LOAGANATHAN
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Thanks for the quick response ,i have tried with the modified script but getting the below output with no values on active and granted column and also i need to get ESX host name on output CSV file with Respective VMs where it is residing

MemactiveMemgrantedFolderMemconsumedVM
Test5191640.667VM-Test1
Test7669387VM-Test2
Test2688585.458VM-Test3
Test4193169.392VM-Test4
Test4193140.567VM-Test5
Test4146308.945VM-Test6
Test4190891.483VM-Test7

please help me on this

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks like the Statistics Level for Historical Interval 3 (past month) is not set to at least 2.

The active and granted counters both require level 1.

mem-active.png

mem-granted.png

The consumed requires level 1

mem-consumed.png


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

0 Kudos
LOAGANATHAN
Enthusiast
Enthusiast
Jump to solution

Hi LucD

yes your correct currently all statistics level are at Level 1

please let me know that if i changed the level 1 to level 2 and immediately  run the script will i get the output( i tried but no luck got the same output:smileysilly: as above one)

and please let me know how to get the ESX host name correspond to the VM where its residing with this script

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid you will have to wait at least 2 weeks (if you want data from the past 2 weeks).


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

0 Kudos
LOAGANATHAN
Enthusiast
Enthusiast
Jump to solution

ya got it once changed the statistics level then only the vcenter will start capture the data

please help me on to get the esx host details of that VM

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing the New-Object like this

New-Object PSObject -Property @{
 
Folder = $folder.Name
 
VM = $_.Values[0]
 
Host = $_.Group[0].Entity.Host.Name
 
Memactive = $Memactive
 
Memconsumed = $Memconsumed
 
Memgranted = $Memgranted
}


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

LOAGANATHAN
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Thanks for the timely help and Thank you very much.got the exact output from the script what i need Smiley Happy

0 Kudos
Solsoli
Contributor
Contributor
Jump to solution

How can we modify the script to export usage in Gb ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since the value is in KB, you'll have to divide by 1 MB.
And you probably want to round the value.

Something like this

$Memactive = [math]::Round(($_.Group | where {$_.MetricId -eq "Mem.active.average"} | Measure-Object -Property Value -Average | Select -ExpandProperty Average)/1MB)


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