Skip to content

Commit

Permalink
MS4W -Measurements (#230)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Added example invokation

Co-authored-by: Mario Mejia <[email protected]>
  • Loading branch information
SamDesmondKing and jmariomejiap authored Aug 18, 2022
1 parent 8562ea5 commit 7e38a1b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 42 deletions.
44 changes: 35 additions & 9 deletions windows/runner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,6 +57,8 @@ used with Tidal Tools or Tidal Migrations API.
.\runner.ps1 -UserName myuser
.\runner.ps1 -UserName myuser -Measurements
#>
[CmdletBinding()]
param (
Expand Down Expand Up @@ -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
Expand All @@ -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..."
Expand All @@ -122,15 +140,15 @@ $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 {
$invokeCommandParams = @{
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
}
Expand All @@ -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

Expand Down
88 changes: 55 additions & 33 deletions windows/server_stats.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ $ServerStats = {

[Parameter()]
[bool]
$NoWinRM
$NoWinRM=$false,

[Parameter()]
[bool]
$Measurements=$false
)

$getWmiObjectParams = @{
Expand Down Expand Up @@ -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
}

0 comments on commit 7e38a1b

Please sign in to comment.