Automation

 View Only
  • 1.  Customising CSV Output

    Posted May 20, 2014 09:06 AM

    I'm using get-stat to get two different metrics for a cluster and I want to export them to a CSV file in a certain format. I'm getting CPU in Mhz and % and by default it outputs it in the following format:

    ValueTimestampMetricIdUnit
    9117/05/2014 01:00cpu.usagemhz.averageMHz
    9116/05/2014 01:00cpu.usagemhz.averageMHz
    9115/05/2014 01:00cpu.usagemhz.averageMHz
    79514/05/2014 01:00cpu.usagemhz.averageMHz
    2713/05/2014 01:00cpu.usagemhz.averageMHz
    1.5617/05/2014 01:00cpu.usage.average%
    1.5616/05/2014 01:00cpu.usage.average%
    1.5615/05/2014 01:00cpu.usage.average%
    13.714/05/2014 01:00cpu.usage.average%
    0.4613/05/2014 01:00cpu.usage.average%

    But in order for it to be usable I need it in the following format:

    TimestampMHz%
    17/05/2014 01:00911.56
    16/05/2014 01:00911.56
    15/05/2014 01:00911.56
    14/05/2014 01:0079513.7
    13/05/2014 01:00270.46

    Is there a way to control the manner in which it creates the CSV?

    Thanks in advance



  • 2.  RE: Customising CSV Output

    Posted May 20, 2014 09:58 AM

    Perhaps a stupid question, but why don't you get the cpu.usage.average counter, that is already in % ?



  • 3.  RE: Customising CSV Output

    Posted May 20, 2014 10:15 AM

    I'm getting the average and the mhz counters. It's the order I need them in when I open the CSV in Excel. I'll generate a graph from it then. Needs to be PowerShell 2.0 (I saw your Excel function in the other post).

    EDIT - notice how in my first post there's two tables. In the first table it lists the two counters in one column. In the second it lists them in a column each with the timestamp in a single column and not repeated.



  • 4.  RE: Customising CSV Output

    Posted May 20, 2014 10:48 AM

    Ok, I see.

    But how are you getting those 2 columns ?

    Are these from 2 separate calls to Get-Stat ?



  • 5.  RE: Customising CSV Output

    Posted May 20, 2014 11:03 AM

    I'm using the following the code at the moment:

    $WarningPreference = "SilentlyContinue"

    $metrics = "cpu.usagemhz.average","cpu.usage.average"

    $start = (get-date).AddDays(-7)

    $finish = get-date

    $stats = get-stat -Entity (get-cluster *) -Stat $metrics -Start $start -Finish $finish -Interval 86400

    $groups = $stats | Group-Object -Property {$_.TimeStamp, $_.MetricId, $_.Value, $_.Unit, $_.Entity}

    $report = $groups | % {

      New-Object PSObject -Property @{

      'TimeStamp' = $_.Group[0].TimeStamp

      'MetricId' = $_.Group[0].MetricId

      'Value' = $_.Group[0].Value

      'Unit' = $_.Group[0].Unit

      'Entity' = $_.Group[0].Entity

      }

    }

    $report | Select TimeStamp, Value, Unit | Export-Csv "C:\out.csv" -NoTypeInformation

    This gives me this:

    TimeStampValueUnit
    18/05/2014 01:0091MHz
    17/05/2014 01:0091MHz
    16/05/2014 01:0091MHz
    15/05/2014 01:0091MHz
    14/05/2014 01:00795MHz
    18/05/2014 01:001.56%
    17/05/2014 01:001.56%
    16/05/2014 01:001.56%
    15/05/2014 01:001.56%
    14/05/2014 01:0013.7%

    but I need to seperate the metrics into seperate columns like in the second table. I ran that off in excel to get an idea of what I need.



  • 6.  RE: Customising CSV Output
    Best Answer

    Posted May 20, 2014 11:31 AM

    Try like this

    $WarningPreference = "SilentlyContinue"
    $metrics = "cpu.usagemhz.average","cpu.usage.average"
    $start = (get-date).AddDays(-7)
    $finish = get-date
    $stats = get-stat -Entity (get-cluster *) -Stat $metrics -Start $start -Finish $finish -Interval 86400


    $groups = $stats | Group-Object -Property {$_.TimeStamp,$_.Entity.Name}
    $report = $groups | % {
     
    New-Object PSObject -Property @{
     
    'TimeStamp' = $_.Group[0].TimeStamp
     
    'Entity' = $_.Group[0].Entity.Name
     
    'MHz' = $_.Group | where {$_.MetricId -eq "cpu.usagemhz.average"} | Select -ExpandProperty Value
     
    '%' = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Select -ExpandProperty Value
      }
    }

    $report | Select TimeStamp, Entity, MHz, '%' |
    Export-Csv "C:\out.csv" -NoTypeInformation