【问题标题】:Double for-loop to process data?双for循环处理数据?
【发布时间】:2016-06-18 12:11:40
【问题描述】:

我有一个关于处理我从 VMWARE vROPS 6.X 中提取的数据的问题。

基本上,我编写了一个脚本来从套件 API 中提取指标数据。我几乎有了我想要的格式,但我需要进一步拆分 Metric 和 Timestamp 列。

基本上我构建了一个foreach 循环,然后在其中嵌套了另一个循环,但我没有以正确的顺序获取指标和时间戳(所以我从下面的代码中删除了它)。

当前输出:

"resourceId","Timestamp","METRIC","value"
"ef951a38-3063-477d-af32-baa6d2744357","1466085599999 1466171999999","cpu:1|costop_summation","4.6296298710836307E-4 0.0 4.5836298710836307E-4 0.
"ef951a38-3063-477d-af32-baa6d2744357","1466085599999 1466171999999","mem|usage_average","12.678446789582571 15.390000343322754"

期望的输出:

"resourceId","Timestamp","METRIC","value"
"ef951a38-3063-477d-af32-baa6d2744357","1466085599999","cpu:1|costop_summation","4.6296298710836307E-4 0.0"
"ef951a38-3063-477d-af32-baa6d2744357","1466171999999","cpu:1|costop_summation","4.5836298710836307E-4 0.0"
"ef951a38-3063-477d-af32-baa6d2744357","1466085599999","mem|usage_average","12.678446789582571"
"ef951a38-3063-477d-af32-baa6d2744357","1466171999999","mem|usage_average","15.390000343322754"

我的代码:

#Call vROPS SUITE-API with Invoke-Rest

#Take all certs.
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
 }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#date
[DateTime]$StartDate = (Get-date).adddays(-5)
[DateTime]$EndDate = (Get-date)

$StartDateEpoc = Get-Date -Date $StartDate -UFormat %s
$EndDateEpoc = Get-Date -Date $EndDate -UFormat %s

#Variables
$username = "admin"
$password = "password"
$secPw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object PSCredential -ArgumentList $username,$secPw
$ContentType = "application/xml;charset=utf-8"
$header = New-Object "System.Collections.Generic.Dictionary[[String,[String]]"
$header.Add("Accept", 'application/xml')

#intervalType=
$SECONDS = 'SECONDS'
$MINUTES = 'MINUTES'
$HOURS = 'HOURS'
$DAYS = 'DAYS'

#rollUpType=
$AVG = 'AVG'
$MAX = 'MAX'
$SUM = 'SUM'
$MIN = 'MIN'
$COUNT = 'COUNT'

Invoke-RestMethod -Method GET -uri "https://192.168.0.125/suite-api/api/resources/stats?resourceId=ef951a38-3063-477d-af32-baa6d2744357&resourceId=1ef459e5-789e-446b-9852-3dc92c43e74a&statKey=cpu|usage_average&rollUpType=$AVG&intervalType=$DAYS" -Credential $cred -ContentType $ContentType -Headers $header -OutFile     d:\quickcheck.xml

[xml]$Data = Get-Content 'D:\quickcheck.XML'

$report = @()

$resources = $Data.'stats-of-resources'
$UUIDS = $Resource.'resourceId'

foreach ($Resource in $Resources.'stats-of-resource') {
  foreach ($node in $Resource.'stat-list'.stat)
   {
   #Collection Date, not run time
   $MetricName = $node.statKey.Key

    $Values     = @($node.data -replace '( \d\.\d) ',"`$1`n" -split "`n")
    $Timestamps = @($node.timestamps -split ' ')
        for ($i=0; $i -lt $Values.Count -and $i -lt $Timestamps.Count; $i++) {
        $report += New-Object PSObject -Property @{
            METRIC     = $MetricName
            resourceId = $Resource.'resourceId'
            Timestamp  = $Timestamps[$i]
            value      = $Values[$i]
  }
}

   }

$report | Export-Csv D:\reprop.csv -NoTypeInformation

【问题讨论】:

    标签: csv vmware metrics powershell-4.0


    【解决方案1】:

    在空格处拆分$Timestamps,在每隔一个空格处拆分$Values,并为每个生成的片段对创建一个对象,例如像这样:

    $Values     = @($Values -split ' ')
    $Timestamps = @($Timestamps -split ' ')
    for ($i=0; $i -lt $Values.Count -and $i -lt $Timestamps.Count; $i++) {
      $report += New-Object PSObject -Property @{
        METRIC     = $MetricName
        resourceId = $Resource.'resourceId'
        Timestamp  = $Timestamps[$i]
        value      = $Values[$i]
      }
    }
    

    【讨论】:

    • 感谢您的回复,它删除了 2 个时间戳值之一,我仍然得到 2 个值并且没有额外的行。下面是新的输出。 "resourceId","Timestamp","METRIC","value" "ef951a38-3063-477d-af32-baa6d2744357","1466258399999","cpu|usage_average","1.9282337965236769 1.9452419329074122" "1ef459e95-8-44-8-64 3dc92c43e74a","1466258399999","cpu|usage_average","2.8392222217387624 3.6437365691508017"
    • @Humpadilly 代码应该进入嵌套的foreach循环内部。另外,$Values 是否包含 2 个或 4 个数字?以及您想如何拆分它们?
    • 好的,谢谢,值和时间戳可以包含 1 个或多个值,这取决于我指定长度的粒度和持续时间。基本上第一个值与第一个时间戳、第二个和第二个......等等等等。
    • 然后在空格处简单地拆分$Values$Timestamp 就足够了。查看更新的答案。
    • 谢谢,现在很好用。我会为想要使用 vrops 和这种收集方法的人发布完整的脚本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2015-06-17
    • 2021-03-20
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多