Do you sometimes notice high CPU or memory usage on your EC2 instances or WorkSpaces? Do you know what’s causing it? For how long and how frequently?
It’s easy to find a rogue process in Task Manager and stop it, but is it a part of the pattern?
Enter a century-old tool -> Performance Monitor.
Sometimes, the rogue process is an application or a service which spikes at random times for random intervals. In such cases, we need to set up a Data Collector in the Performance Monitor and use it to track resource usage over a period of time. Logs are then replayed and root cause identified.
However, not everyone is tech-savvy enough to be able to create a Data Collector on their own. With enough persistence, they’d do it, but if we can help them help themselves, all the better.
To avoid errors during the data collection and make life easier on the person affected by the system issues we can provide them with the PowerShell one-liner code which will automate the creation of the full Data Collector. Furthermore, after helping them create a full dump of the system resource usage, and teaching them how to read it, they stand a better chance to find the root cause on their own.
Everybody wins.
When executed from the elevated (Run as Administrator) PowerShell console, this command will download the XML template for Data Collector, create it and set to run automatically:
1 |
Invoke-Expression (New-Object Net.WebClient).DownloadString("https://raw.githubusercontent.com/PowerSix/MyPowerShellSpace/master/Set-PerformanceCounters.ps1") |
The new collector will be named “CustomPerformance” and will automatically start collecting data. Two files are created per day, and logs older than 14 days are removed, for safety – if we or they forget that the log collection is running. But we never forget, do we? 🙂
During the time when the Performance Monitor is running, simply use your instance or a WorkSpace as you normally would. This will provide a good sample of a real working conditions.
To access the Performance Log Files, navigate to the folder:
C:\PerfLogs\System\Performance\CustomPerformance\.
The date of the log file is in the sub-folder name.
All performance metrics collected are graphed during the application’s initial startup. To interpret the data, start Performance Monitor (perfmon.msc), remove all the metrics and start from scratch. Select any of the counters listed below the line chart -> Press Ctrl+A to select all -> Press the Delete key or the red X in the toolbar.
To begin adding performance metrics to the graph, select the green plus sign in the toolbar. To examine which processes are consuming CPU resources drill down from Process -> % Processor Time ->
Performance monitor will graph the % Processor Time used by all processes running on the instance so that CPU hogging applications can be quickly identified. Additional performance metrics can be layered on top of the current view such as disk write operations per second.
Refer to this webpage for a complete list of counters definitions: https://technet.microsoft.com/en-us/library/cc768048.aspx
Get the code (and any future updates) from GitHub: https://github.com/PowerSix/MyPowerShellSpace
As usual, if the script is updated, the blog post won’t be.
Always check the GitHub repo for the latest version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
<# .SYNOPSIS Automate creation of Performance Monitor Data Collector on an EC2 instance or a WorkSpace .DESCRIPTION Script takes an XML file with defined settings and automates the creation of a a data collector. Helps with the troubleshooting of inaccessible / secure systems. .NOTES File name : Set-PerformanceCounters.ps1 Author : Sinisa Mikasinovic - six@mypowershell.space Published : 2018-05-10 Version : 2017-10-12 - v1.1 - Improved error capture and handling. 2017-10-11 - v1.0 - Initial version. Scripts are usually created as part of http://mypowershell.space learning tutorials. Original post: http://mypowershell.space/index.php/2018/05/10/automate-creation-of-performance-monitor-data-collector-on-an-ec2-instance-or-a-workspace Blog posts are NOT getting updated! Look for script updates on GitHub! More scripts at GitHub: https://github.com/PowerSix/MyPowerShellSpace Script execution may differ from expectations. Make sure to give it a test run first. Feel free to update/modify. Suggestions and improvements are welcome. Obligatory "not my fault if you break stuff" disclaimer: This script example is provided "AS IS", without warranties or conditions of any kind, either expressed or implied. By using this script, you agree that only you are responsible for any resulting damages, losses, liabilities, costs or expenses. .PARAMETER Help Displays script information and usage. .EXAMPLE Set-PerformanceCounters.ps1 Runs with no specified parameters. Default script run. .EXAMPLE Invoke-Expression (New-Object Net.WebClient).DownloadString("https://raw.githubusercontent.com/PowerSix/MyPowerShellSpace/master/Set-PerformanceCounters.ps1") Invoke the latest version remotely. Be sure to check the script beforehand to know what you're executing :-) .EXAMPLE Set-PerformanceCounters.ps1 -Help Displays detailed script information. .LINK http://mypowershell.space .LINK https://github.com/PowerSix/MyPowerShellSpace #> Param( [switch]$Help ) # Display script information and exit if ($Help) { Write-Host "`nFile name : " -ForegroundColor Cyan -NoNewLine Write-host "Set-PerformanceCounters.ps1" -ForegroundColor Yellow Write-Host "Author : " -ForegroundColor Cyan -NoNewLine Write-host "Sinisa Mikasinovic - sinisam@gmail.com" -ForegroundColor Yellow Write-Host "Update : " -ForegroundColor Cyan -NoNewLine Write-host "https://github.com/PowerSix/MyPowerShellSpace`n" -ForegroundColor Green Write-Host "Published : " -ForegroundColor Cyan -NoNewLine Write-host "2018-05-10" -ForegroundColor Yellow Write-Host "Version : " -ForegroundColor Cyan -NoNewLine Write-host "2017-10-12 - 1.1 - Improved error capture and handling." -ForegroundColor Yellow Write-Host " : " -ForegroundColor Cyan -NoNewLine Write-host "2017-10-11 - 1.0 - Initial version." -ForegroundColor Yellow Write-Host "Examples : " -ForegroundColor Cyan -NoNewLine Write-Host "Get-Help Set-PerformanceCounters -Examples" -ForegroundColor Yellow Write-Host "Full help : " -ForegroundColor Cyan -NoNewLine Write-Host "Get-Help Set-PerformanceCounters -Full`n" -ForegroundColor Yellow break } # Download Data Collector XML template try { (New-Object System.Net.WebClient).DownloadFile("https://raw.githubusercontent.com/PowerSix/MyPowerShellSpace/master/Set-PerformanceCounters.xml", "$env:TEMP\PerformanceCounters.xml") } catch { Write-Output "`n[ERROR] Download failed. Are you running PowerShell as Administrator?" Write-Output $Error[0].Exception.Message Write-Output "`n`n`n[ERROR] Full error output for analysis:" Write-Output $Error[0] | Format-List * -Force break } # Create the Data Collector try { $DataCollectorSet = New-Object -COM Pla.DataCollectorSet $XML = Get-Content "$env:TEMP\PerformanceCounters.xml" $DataCollectorSet.SetXml($xml) $DataCollectorSet.Commit("CustomPerformance", $null, 0x0003) | Out-Null $DataCollectorSet.Start($true) Write-Output "[SUCCESS] Data Collector 'CustomPerformance' successfully created." } catch [System.UnauthorizedAccessException] { Write-Output "`n[ERROR] Insufficient permissions to create Data Collector! Are you running PowerShell as Administrator?" Write-Output $Error[0].Exception.Message Write-Output "`n`n`n[ERROR] Full error output for analysis:" Write-Output $Error[0] | Format-List * -Force } catch [System.Runtime.InteropServices.COMException] { Write-Output "`n[ERROR] Duplication detected! Task already exists. Remove it and all files first, and retry." Write-Output $Error[0].Exception.Message Write-Output "`n`n`n[ERROR] Full error output for analysis:" Write-Output $Error[0] | Format-List * -Force } catch { Write-Output "`n[ERROR] Error creating Data Collector!" Write-Output $Error[0].Exception.Message Write-Output "`n`n`n[ERROR] Full error output for analysis:" Write-Output $Error[0] | Format-List * -Force } |
Leave a Comment