From 7e38a1bc4226ceafdeaa67889cbacfbf9e63f537 Mon Sep 17 00:00:00 2001 From: Sam King <43866616+SamDesmondKing@users.noreply.github.com> Date: Thu, 18 Aug 2022 19:48:11 +0530 Subject: [PATCH] MS4W -Measurements (#230) * Added -Measurements flag which specifies the output data structure * Trim unneccessary properties from the response object * Fixed typo * Update windows/runner.ps1 Co-authored-by: Mario Mejia * Added example invokation Co-authored-by: Mario Mejia --- windows/runner.ps1 | 44 ++++++++++++++++---- windows/server_stats.ps1 | 88 +++++++++++++++++++++++++--------------- 2 files changed, 90 insertions(+), 42 deletions(-) diff --git a/windows/runner.ps1 b/windows/runner.ps1 index ada2594..3219ad6 100644 --- a/windows/runner.ps1 +++ b/windows/runner.ps1 @@ -34,14 +34,20 @@ The default value is 30. Capture point-in-time CPU utilization value rather than a peak and average over a given time. +.PARAMETER Measurements + +Specifies if the data should be output in a structure that matches the TMP /measurements endpoint. +This is intended to capture point-in-time CPU Utilization. + .INPUTS None. You cannot pipe objects to runner.ps1 .OUTPUTS -runner.ps1 generates a JSON output with all the gathered data suitable to be -used with Tidal Tools or Tidal Migrations API. +Default behaviour will output JSON which is suitable to pipe to the Tidal Tools command "tidal sync servers". + +Adding the -Measurements flag will output JSON which is suitable to pipe to the Tidal Migrations Platform /measurements API endpoint. .EXAMPLE @@ -51,6 +57,8 @@ used with Tidal Tools or Tidal Migrations API. .\runner.ps1 -UserName myuser +.\runner.ps1 -UserName myuser -Measurements + #> [CmdletBinding()] param ( @@ -84,9 +92,19 @@ param ( [Parameter()] [switch] - $CpuUtilizationOnlyValue + $CpuUtilizationOnlyValue, + + [Parameter()] + [switch] + $Measurements ) +# If the -Measurements flag is used, set -CPUUtilizationOnlyValue and -CPUUtilizationTimeout 1 +if ($Measurements) { + $CpuUtilizationOnlyValue = $true + $CpuUtilizationTimeout = 1 +} + if (![System.IO.File]::Exists($SecurePwdFilePath)) { Write-Error "$SecurePwdFilePath does not exist. Be sure to run save_password.ps1 before trying again." exit 1 @@ -98,7 +116,7 @@ $secPwd = Get-Content $SecurePwdFilePath | ConvertTo-SecureString $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $secPwd try { - $env_user = Invoke-Command -ComputerName ([Environment]::MachineName) -Credential $cred -ScriptBlock { $env:USERNAME } -ErrorAction Stop + $env_user = Invoke-Command -ComputerName ([Environment]::MachineName) -Credential $cred -ScriptBlock { $env:USERNAME } -ErrorAction Stop Write-Host "Executing inventory gathering as user: $env_user..." } catch [System.Management.Automation.Remoting.PSRemotingTransportException] { Write-Host "Executing inventory gathering..." @@ -122,7 +140,7 @@ $server_list | ForEach-Object { if ($NoWinRM) { $startJobParams = @{ ScriptBlock = $ServerStats - ArgumentList = $_, $cred, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM + ArgumentList = $_, $cred, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM, $Measurements } $jobs += Start-Job @startJobParams } else { @@ -130,7 +148,7 @@ $server_list | ForEach-Object { ComputerName = $_ Credential = $cred ScriptBlock = $ServerStats - ArgumentList = "localhost", $null, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM + ArgumentList = "localhost", $null, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM, $Measurements } $jobs += Invoke-Command @invokeCommandParams -AsJob } @@ -155,14 +173,22 @@ Do { } Until (($jobs | Get-Job | Where-Object { (($_.State -eq "Running") -or ($_.state -eq "NotStarted")) }).count -eq 0) $jobs | Receive-Job | ForEach-Object { - $server_stats += $_ + $server_stats += $_ | Select -Property * -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName } $num_results = $server_stats.Count Write-Host "$num_results results received out of $num_servers servers." -# Output results -$results = @{ servers = $server_stats } +## Build result + +# Set root object key +$root_object_key = "servers" +if ($Measurements) { + $root_object_key = "measurements" +} + +# output result +$results = @{ $root_object_key = $server_stats } $json = $results | ConvertTo-Json -Depth 99 Write-Output $json diff --git a/windows/server_stats.ps1 b/windows/server_stats.ps1 index 8a09738..7ca8c35 100644 --- a/windows/server_stats.ps1 +++ b/windows/server_stats.ps1 @@ -25,7 +25,11 @@ $ServerStats = { [Parameter()] [bool] - $NoWinRM + $NoWinRM=$false, + + [Parameter()] + [bool] + $Measurements=$false ) $getWmiObjectParams = @{ @@ -133,42 +137,60 @@ $ServerStats = { } # Create an object to return, convert this to JSON or CSV as you need: - $server_info = New-Object -TypeName psobject -Property @{ - host_name = $cpu.SystemName - ram_allocated_gb = $PhysicalMemory - ram_used_gb = $OSUsedMemory - storage_allocated_gb = $Total_DriveSpaceGB - storage_used_gb = $Total_UsedDriveSpaceGB - cpu_count = $cpu_count - operating_system = $OSInfo.Caption - operating_system_version = $OSInfo.Version - cpu_name = $cpu.Name - } - $custom_fields = New-Object -TypeName psobject -Property @{ - CPU_Description = $cpu.Description - CPU_Manufacturer = $cpu.Manufacturer - CPU_L2CacheSize = $cpu.L2CacheSize - CPU_L3CacheSize = $cpu.L3CacheSize - CPU_SocketDesignation = $cpu.SocketDesignation - TotalVisible_Memory_GB = $OSTotalVisibleMemory - TotalVirtual_Memory_GB = $OSTotalVirtualMemory - } - - if ($CpuUtilizationOnlyValue) { - $custom_fields | Add-Member -NotePropertyName cpu_utilization -NotePropertyValue $CPUUtilization - $custom_fields | Add-Member -NotePropertyName cpu_utilization_timestamp -NotePropertyValue $CPUUtilizationTimestamp + # When running with the -Measurements flag, the output will match TMP API /measurements expected data structure + if ($Measurements) { + $server_info = New-Object -TypeName psobject -Property @{ + field_name = "cpu_utilization_timeseries" + measurable_type = "server" + value = $CPUUtilization + external_timestamp = $CPUUtilizationTimestamp + measurable = New-Object -TypeName psobject -Property @{ + host_name = $cpu.SystemName + } + } + # When running without the -Measurements flag, the output will match 'tidal sync servers' expected data structure } else { - if (!$NoWinRM) { - $custom_fields | Add-Member -NotePropertyName cpu_average -NotePropertyValue $CPUUtilization.Average - $custom_fields | Add-Member -NotePropertyName cpu_peak -NotePropertyValue $CPUUtilization.Maximum - $custom_fields | Add-Member -NotePropertyName cpu_sampling_timeout -NotePropertyValue $CPUUtilization.Count + $server_info = New-Object -TypeName psobject -Property @{ + host_name = $cpu.SystemName + ram_allocated_gb = $PhysicalMemory + ram_used_gb = $OSUsedMemory + storage_allocated_gb = $Total_DriveSpaceGB + storage_used_gb = $Total_UsedDriveSpaceGB + cpu_count = $cpu_count + operating_system = $OSInfo.Caption + operating_system_version = $OSInfo.Version + cpu_name = $cpu.Name } - } - Add-Member -InputObject $server_info -MemberType NoteProperty -Name "custom_fields" -Value $custom_fields - if ($process_stats) { - Add-Member -InputObject $server_info -MemberType NoteProperty -Name "process_stats" -Value $process_stats + $custom_fields = New-Object -TypeName psobject -Property @{ + CPU_Description = $cpu.Description + CPU_Manufacturer = $cpu.Manufacturer + CPU_L2CacheSize = $cpu.L2CacheSize + CPU_L3CacheSize = $cpu.L3CacheSize + CPU_SocketDesignation = $cpu.SocketDesignation + TotalVisible_Memory_GB = $OSTotalVisibleMemory + TotalVirtual_Memory_GB = $OSTotalVirtualMemory + } + + if ($CpuUtilizationOnlyValue) { + $custom_fields | Add-Member -NotePropertyName cpu_utilization -NotePropertyValue $CPUUtilization + $custom_fields | Add-Member -NotePropertyName cpu_utilization_timestamp -NotePropertyValue $CPUUtilizationTimestamp + } else { + if (!$NoWinRM) { + $custom_fields | Add-Member -NotePropertyName cpu_average -NotePropertyValue $CPUUtilization.Average + $custom_fields | Add-Member -NotePropertyName cpu_peak -NotePropertyValue $CPUUtilization.Maximum + $custom_fields | Add-Member -NotePropertyName cpu_sampling_timeout -NotePropertyValue $CPUUtilization.Count + } + } + + Add-Member -InputObject $server_info -MemberType NoteProperty -Name "custom_fields" -Value $custom_fields + + if ($process_stats) { + Add-Member -InputObject $server_info -MemberType NoteProperty -Name "process_stats" -Value $process_stats + } } + + # Return the object $server_info }