Needed to pull some custom metrics from my Windows instances running within AWS and pop them into Cloudwatch.
Specifically I needed to get the number of “Active Sessions” from the Terminal Services counter.
To start within, AWS provide some nice little PowerShell scripts that take custom metrics and pop them into Cloudwatch for you.
Once the scripts are downloaded and extracted into a local folder, you need to create an IAM user with enough rights for Cloud watch operations (I started with cloud watch full access, but I could probably be a little more granular)
Once that was done, I downloaded and extracted the contents of the AmazonCloudWatchMonitoringWindows.zip file into c:\_Scripts\Cloudwatch.
Next step was to add the credentials into the awscreds.conf file.
Now, there are few scripts available, each one assists in grabbing specific metric sets from the Window Instances.
My specific requirement was to grab the number of active terminal server connections, so I opted to customize the mon-put-metrics-perfmon.ps1 script and include the specific metrics I was after.
Opening up the PS1 script within notepad and jumping down to the section which starts “#### Add More Counters here.”
Below this section you can add a list of the counters you wish to pull from perfmon and send up to cloudwatch.
First step was to determine the correct syntax for the metric I was after.
Running the command “(get-counter –ListSet ‘Terminal Services’).Paths” returns the available metrics and the paths for those metrics.
Now it was time to mod the PowerShell script, So I followed the format of the existing counters and added the following:
$Counters.Add('\\localhost\Terminal Services\Active Sessions’,‘Count’)
Which resulted in the file looking a bit like this..
Save the file now. Notice the second parameter, “count”, this is the unit of data the counter provides. In the case of Active Sessions, it’s simply a counter 1..2..3….10, you get the idea.
All that’s left do to now is schedule the script to run at whatever interval meets your requirements.
In my case I opted for every 5 minutes.
I configured the task to run as SYSTEM and the action looks a bit like this:
The argument is: ‘ -command “C:\_Scripts\Cloudwatch\mon-put-metrics-perfmon.ps1 -aws_credential_file C:\_Scripts\Cloudwatch\awscreds.conf” ’
As you can see, I’ve simply specified PowerShell as the program to run, with the the mon-put-metrics-perfmon.ps1 script as the argument (I also specified the credentials file).
Now time to click save and let it run for few minutes / hours /days….
The result is a nice new metric on CloudWatch – now time to have fun with Autoscaling.