PowerCLI

  • 1.  Restrict CLuster Statistics to business Hours

    Posted Jun 01, 2009 11:39 PM

    I am trying to update my reporting script so that it filter the statistics that come back to just 5am - 7pm M-F but i cant figure out how.

    cls

    $Global:ErrorActionPreference="SilentlyContinue"

    $vctr = @("ap-vmvc-p001","ap-vmvc-p01")

    $output = @()

    Foreach ($vc in $vctr) {

    Connect-VIServer $VC

    $RootFolders = Get-Folder datacenters \

    Get-Folder -NoRecursion \

    Sort-Object $_.name

    Foreach ($RF in $RootFolders){

    $CurrentRootFolder = $RF \

    Get-Folder -NoRecursion \

    Sort-Object $_.name

    Foreach ($SF in $CurrentRootFolder){

    "`n" + $RF.Name + "`t" + $sf.Name

    trap{"`tNo Clusters";continue}

    $Clusters = $SF \

    Get-Cluster

    trap

    $clusters = $clusters \

    Sort-Object $_.name

    Foreach ( $Cluster in $Clusters){

    write-host $cluster.name

    $out = "" \

    select-object Vcenter, Site, Farm, Cluster, HostCount, VMCount, CpuTotalGhz, CpuAvg, CpuMax, CpuMin, MemTotalGB, MemAvg, MemMax, MemMin;

    $out.HostCount = ($cluster \

    Get-view ).summary.Numhosts

    $out.VMCount = ($cluster \

    Get-Vm).count

    if ($out.HostCount -gt 0 -and $out.VMCount -gt 0) {

    $out.Vcenter = $VC

    $out.Site = $RF.Name

    $out.Farm = $SF.name

    $out.Cluster = $cluster.Name

    $Out.CpuTotalGhz= ($cluster \

    Get-view ).summary.totalcpu / 1000

    $Out.MemTotalGB = ::Round(($cluster \

    Get-view ).summary.totalmemory/1024/1024/1024,2)

    $statCPU = Get-stat -entity $cluster -stat cpu.usagemhz.average -start (get-date).adddays(-30) \

    where { $_.value -gt 3}

    $statMem = Get-stat -entity $cluster -stat mem.usage.average -start (get-date).adddays(-30) \

    where { $_.value -gt 3}

    $out.CpuMin = ::round(((($statCpu \

    measure-object -property value -min ).minimum/1000)/$out.cputotalGhz)*100,2)

    $out.CpuMax = ::round(((($statCpu \

    measure-object -property value -max ).maximum/1000)/$out.cputotalGhz)*100,2)

    $out.CpuAvg = ::round(((($statCpu \

    measure-object -property value -average ).average/1000)/$out.cputotalGhz)*100,2)

    $out.MemMax = ::round(($statMem \

    measure-object -property value -max ).maximum ,2)

    $out.MemMin = ::Round(($statMem \

    measure-object -property value -min ).minimum,2)

    $out.MemAvg = ::round(($statMem \

    measure-object -property value -average ).average,2)

    if ($out.CpuMin -lt 0) {$out.CpuMin = 0}

    if ($out.MemMin -lt 0) {$out.MemMin = 0}

    if ($out.VMCount -eq "") {$out.VMCount = 0}

    $out

    $output = $output + $out

    }

    }

    }

    }

    }

    $output \

    Sort-object Site, Farm, CLuster\

    Export-Csv -Force -noTypeInformation c:\Cluster_Utilization_30day.csv

    Truth is a three edged sword



  • 2.  RE: Restrict CLuster Statistics to business Hours

    Posted Jun 02, 2009 06:25 AM

    The Get-Stat cmdlet doesn't allow to specify non-continuous time ranges.

    What you need to do is to filter out the statistical data that has a time stamp outside the window you want to cover.

    This can be done with the Where-Object cmdlet.

    If you change the lines containing the Get-Stat cmdlet like below it should only give you the statistical data inside the 05:00-19:00 window.

    ...
       $statCPU = Get-stat -entity $cluster -stat cpu.usagemhz.average -start (*get-date*).adddays(-30) | where { $_.value -gt 3} | where {(5..18) -contains $_.Timestamp.Hour}
       $statMem = Get-stat -entity $cluster -stat mem.usage.average -start (*get-date*).adddays(-30) | where { $_.value -gt 3} | where {(5..18) -contains $_.Timestamp.Hour}
    
    ...