双for循环处理数据?
Double for-loop to process data?
我对处理从 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.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) ',"``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
在 space 秒时拆分 $Timestamps
,在每秒 space 时拆分 $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]
}
}
我对处理从 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.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) ',"``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
在 space 秒时拆分 $Timestamps
,在每秒 space 时拆分 $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]
}
}