Skip to content

Commit 7e38a1b

Browse files
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 <jmariomejiap@gmail.com> * Added example invokation Co-authored-by: Mario Mejia <jmariomejiap@gmail.com>
1 parent 8562ea5 commit 7e38a1b

File tree

2 files changed

+90
-42
lines changed

2 files changed

+90
-42
lines changed

windows/runner.ps1

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ The default value is 30.
3434
3535
Capture point-in-time CPU utilization value rather than a peak and average over a given time.
3636
37+
.PARAMETER Measurements
38+
39+
Specifies if the data should be output in a structure that matches the TMP /measurements endpoint.
40+
This is intended to capture point-in-time CPU Utilization.
41+
3742
.INPUTS
3843
3944
None. You cannot pipe objects to runner.ps1
4045
4146
.OUTPUTS
4247
43-
runner.ps1 generates a JSON output with all the gathered data suitable to be
44-
used with Tidal Tools or Tidal Migrations API.
48+
Default behaviour will output JSON which is suitable to pipe to the Tidal Tools command "tidal sync servers".
49+
50+
Adding the -Measurements flag will output JSON which is suitable to pipe to the Tidal Migrations Platform /measurements API endpoint.
4551
4652
.EXAMPLE
4753
@@ -51,6 +57,8 @@ used with Tidal Tools or Tidal Migrations API.
5157
5258
.\runner.ps1 -UserName myuser
5359
60+
.\runner.ps1 -UserName myuser -Measurements
61+
5462
#>
5563
[CmdletBinding()]
5664
param (
@@ -84,9 +92,19 @@ param (
8492

8593
[Parameter()]
8694
[switch]
87-
$CpuUtilizationOnlyValue
95+
$CpuUtilizationOnlyValue,
96+
97+
[Parameter()]
98+
[switch]
99+
$Measurements
88100
)
89101

102+
# If the -Measurements flag is used, set -CPUUtilizationOnlyValue and -CPUUtilizationTimeout 1
103+
if ($Measurements) {
104+
$CpuUtilizationOnlyValue = $true
105+
$CpuUtilizationTimeout = 1
106+
}
107+
90108
if (![System.IO.File]::Exists($SecurePwdFilePath)) {
91109
Write-Error "$SecurePwdFilePath does not exist. Be sure to run save_password.ps1 before trying again."
92110
exit 1
@@ -98,7 +116,7 @@ $secPwd = Get-Content $SecurePwdFilePath | ConvertTo-SecureString
98116
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $secPwd
99117

100118
try {
101-
$env_user = Invoke-Command -ComputerName ([Environment]::MachineName) -Credential $cred -ScriptBlock { $env:USERNAME } -ErrorAction Stop
119+
$env_user = Invoke-Command -ComputerName ([Environment]::MachineName) -Credential $cred -ScriptBlock { $env:USERNAME } -ErrorAction Stop
102120
Write-Host "Executing inventory gathering as user: $env_user..."
103121
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
104122
Write-Host "Executing inventory gathering..."
@@ -122,15 +140,15 @@ $server_list | ForEach-Object {
122140
if ($NoWinRM) {
123141
$startJobParams = @{
124142
ScriptBlock = $ServerStats
125-
ArgumentList = $_, $cred, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM
143+
ArgumentList = $_, $cred, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM, $Measurements
126144
}
127145
$jobs += Start-Job @startJobParams
128146
} else {
129147
$invokeCommandParams = @{
130148
ComputerName = $_
131149
Credential = $cred
132150
ScriptBlock = $ServerStats
133-
ArgumentList = "localhost", $null, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM
151+
ArgumentList = "localhost", $null, $ProcessStats, $CpuUtilizationTimeout, $CpuUtilizationOnlyValue, $NoWinRM, $Measurements
134152
}
135153
$jobs += Invoke-Command @invokeCommandParams -AsJob
136154
}
@@ -155,14 +173,22 @@ Do {
155173
} Until (($jobs | Get-Job | Where-Object { (($_.State -eq "Running") -or ($_.state -eq "NotStarted")) }).count -eq 0)
156174

157175
$jobs | Receive-Job | ForEach-Object {
158-
$server_stats += $_
176+
$server_stats += $_ | Select -Property * -ExcludeProperty PSComputerName,RunSpaceID,PSShowComputerName
159177
}
160178

161179
$num_results = $server_stats.Count
162180
Write-Host "$num_results results received out of $num_servers servers."
163181

164-
# Output results
165-
$results = @{ servers = $server_stats }
182+
## Build result
183+
184+
# Set root object key
185+
$root_object_key = "servers"
186+
if ($Measurements) {
187+
$root_object_key = "measurements"
188+
}
189+
190+
# output result
191+
$results = @{ $root_object_key = $server_stats }
166192
$json = $results | ConvertTo-Json -Depth 99
167193
Write-Output $json
168194

windows/server_stats.ps1

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ $ServerStats = {
2525

2626
[Parameter()]
2727
[bool]
28-
$NoWinRM
28+
$NoWinRM=$false,
29+
30+
[Parameter()]
31+
[bool]
32+
$Measurements=$false
2933
)
3034

3135
$getWmiObjectParams = @{
@@ -133,42 +137,60 @@ $ServerStats = {
133137
}
134138

135139
# Create an object to return, convert this to JSON or CSV as you need:
136-
$server_info = New-Object -TypeName psobject -Property @{
137-
host_name = $cpu.SystemName
138-
ram_allocated_gb = $PhysicalMemory
139-
ram_used_gb = $OSUsedMemory
140-
storage_allocated_gb = $Total_DriveSpaceGB
141-
storage_used_gb = $Total_UsedDriveSpaceGB
142-
cpu_count = $cpu_count
143-
operating_system = $OSInfo.Caption
144-
operating_system_version = $OSInfo.Version
145-
cpu_name = $cpu.Name
146-
}
147140

148-
$custom_fields = New-Object -TypeName psobject -Property @{
149-
CPU_Description = $cpu.Description
150-
CPU_Manufacturer = $cpu.Manufacturer
151-
CPU_L2CacheSize = $cpu.L2CacheSize
152-
CPU_L3CacheSize = $cpu.L3CacheSize
153-
CPU_SocketDesignation = $cpu.SocketDesignation
154-
TotalVisible_Memory_GB = $OSTotalVisibleMemory
155-
TotalVirtual_Memory_GB = $OSTotalVirtualMemory
156-
}
157-
158-
if ($CpuUtilizationOnlyValue) {
159-
$custom_fields | Add-Member -NotePropertyName cpu_utilization -NotePropertyValue $CPUUtilization
160-
$custom_fields | Add-Member -NotePropertyName cpu_utilization_timestamp -NotePropertyValue $CPUUtilizationTimestamp
141+
# When running with the -Measurements flag, the output will match TMP API /measurements expected data structure
142+
if ($Measurements) {
143+
$server_info = New-Object -TypeName psobject -Property @{
144+
field_name = "cpu_utilization_timeseries"
145+
measurable_type = "server"
146+
value = $CPUUtilization
147+
external_timestamp = $CPUUtilizationTimestamp
148+
measurable = New-Object -TypeName psobject -Property @{
149+
host_name = $cpu.SystemName
150+
}
151+
}
152+
# When running without the -Measurements flag, the output will match 'tidal sync servers' expected data structure
161153
} else {
162-
if (!$NoWinRM) {
163-
$custom_fields | Add-Member -NotePropertyName cpu_average -NotePropertyValue $CPUUtilization.Average
164-
$custom_fields | Add-Member -NotePropertyName cpu_peak -NotePropertyValue $CPUUtilization.Maximum
165-
$custom_fields | Add-Member -NotePropertyName cpu_sampling_timeout -NotePropertyValue $CPUUtilization.Count
154+
$server_info = New-Object -TypeName psobject -Property @{
155+
host_name = $cpu.SystemName
156+
ram_allocated_gb = $PhysicalMemory
157+
ram_used_gb = $OSUsedMemory
158+
storage_allocated_gb = $Total_DriveSpaceGB
159+
storage_used_gb = $Total_UsedDriveSpaceGB
160+
cpu_count = $cpu_count
161+
operating_system = $OSInfo.Caption
162+
operating_system_version = $OSInfo.Version
163+
cpu_name = $cpu.Name
166164
}
167-
}
168165

169-
Add-Member -InputObject $server_info -MemberType NoteProperty -Name "custom_fields" -Value $custom_fields
170-
if ($process_stats) {
171-
Add-Member -InputObject $server_info -MemberType NoteProperty -Name "process_stats" -Value $process_stats
166+
$custom_fields = New-Object -TypeName psobject -Property @{
167+
CPU_Description = $cpu.Description
168+
CPU_Manufacturer = $cpu.Manufacturer
169+
CPU_L2CacheSize = $cpu.L2CacheSize
170+
CPU_L3CacheSize = $cpu.L3CacheSize
171+
CPU_SocketDesignation = $cpu.SocketDesignation
172+
TotalVisible_Memory_GB = $OSTotalVisibleMemory
173+
TotalVirtual_Memory_GB = $OSTotalVirtualMemory
174+
}
175+
176+
if ($CpuUtilizationOnlyValue) {
177+
$custom_fields | Add-Member -NotePropertyName cpu_utilization -NotePropertyValue $CPUUtilization
178+
$custom_fields | Add-Member -NotePropertyName cpu_utilization_timestamp -NotePropertyValue $CPUUtilizationTimestamp
179+
} else {
180+
if (!$NoWinRM) {
181+
$custom_fields | Add-Member -NotePropertyName cpu_average -NotePropertyValue $CPUUtilization.Average
182+
$custom_fields | Add-Member -NotePropertyName cpu_peak -NotePropertyValue $CPUUtilization.Maximum
183+
$custom_fields | Add-Member -NotePropertyName cpu_sampling_timeout -NotePropertyValue $CPUUtilization.Count
184+
}
185+
}
186+
187+
Add-Member -InputObject $server_info -MemberType NoteProperty -Name "custom_fields" -Value $custom_fields
188+
189+
if ($process_stats) {
190+
Add-Member -InputObject $server_info -MemberType NoteProperty -Name "process_stats" -Value $process_stats
191+
}
172192
}
193+
194+
# Return the object
173195
$server_info
174196
}

0 commit comments

Comments
 (0)