Using OMS to perform synthetic web transactions
One of the common requests I hear for OMS is the ability to perform synthetic web transactions like we can in the box with System Center Operations Manager. Think of these as a simple web “ping” which can also check for things like simple content on a page and the size of the web page which is being returned. If you want jump straight to the solution, here’s what you will need:
- An Azure Automation job which you schedule to run as frequently as you need the website to be tested.
- An OMS solution which visualizes the data for the websites which are being tested.
- Queries to create alerts in OMS based on the conditions which you want to be notified for (this blog post has the queries and the download does as well).
The pieces of this solution are available for download here: https://gallery.technet.microsoft.com/Synthetic-Web-Test-for-OMS-1dd5e44d
Please note, this solution is dependent upon the module to log to OMS which is available at https://www.powershellgallery.com/packages/OMSIngestionAPI/1.5.
How to build the synthetic web transaction functionality from scratch
The process I sent through goes through the steps of building a PowerShell script and moving that script into Azure Automation and then utilizing the data in Log Analytics. The six steps in this process are below.
1. Starter Script: Write the PowerShell script and get it working locally. This is the quickest way to develop a functional script which can then be moved to Azure Automation.
- Write it in PowerShell ISE, focusing on getting the script itself to work.
- Hard-coded values are fine at this point, parameters can be added later.
- Logging to OMS is optional as this functionality is available in a module that Microsoft provides which is discussed in the “Azure Automation OMS logging module” section of this blog post.
2. Azure Automation: Update the script to run in Azure Automation by making changes to remove variables, add parameters and make any structural changes required.
- Remove variables from code – move into variables
- Add input parameters to the code
- UseBasicParsing for non-hybrid runbook worker execution when using Invoke-Webrequest
3. Azure Automation OMS logging module: Use a module to log to OMS instead of the existing PowerShell scripts.
- To speed up the process you can log to OMS using an existing module such as the one available at: https://www.powershellgallery.com/packages/OMSIngestionAPI/1.5
4. Schedule the script to run: Schedule the script to run in Azure Automation.
- Create a schedule for the script to run
- Link the schedule to the runbook
5. Configure visualization in Log Analytics: Visualize the information which you are gathering in OMS.
- Creating a solution to visualize the data provided via the synthetic web transactions
6. Configure alerting in Log Analytics: Alert on the conditions which you want to for the websites that you are monitoring.
- Alerting for conditions such as content not matching, URL offline, or slow response time.
Each of these steps is detailed in their corresponding sections in this blog post. We’ll start with creating the “Starter Script”.
The content below was built from pre-existing scripts for the New-Signature and Send-OMSData functions which are available here. The new code written in this was the “GatherWebSitedata” function and the MainScript areas. See the references section of this blog post for additional scripts which this starter script was built from. The code used in the “Starter Script” version is available from the download link provided above (https://gallery.technet.microsoft.com/Synthetic-Web-Test-for-OMS-1dd5e44d) under the name “Sample – Starter Script”. This was the initial hard-coded version of the script.
The following is the updated version of the script for Azure Automation (changes are in bold below). The changes were to add parameters and variables and updates the Invoke-Webrequest to use the UseBasicParsing switch. The code used in the “Azure Automation” version is available from the download link provided above (https://gallery.technet.microsoft.com/Synthetic-Web-Test-for-OMS-1dd5e44d) under the name “Sample – Azure Automation”.
# Retrieve values from variables defined for WorkspaceID and WorkspaceSharedKey
$CustomerID = Get-AutomationVariable -Name "WorkspaceID"
$SharedKey = Get-AutomationVariable -Name "WorkspaceSharedKey"
To use the Get-AutomationVariable functionality you need to define variables for each of the two items above (WorkspaceID and WorkspaceSharedKey). These are created in the automation account under assets and variables.
Copy the WorkspaceID value from the ”Workspace ID” value in the OMS workspace under connected sources, Windows Servers, Workspace ID. Copy the “Primary Key” value to for the WorkspaceSharedKey value.
Azure Automation OMS logging module
Use the “Deploy to Azure Automation” button in the OMS PowerShell module link (https://www.powershellgallery.com/packages/OMSIngestionAPI/1.5) and chose the automation account you are working with. Once this occurs, it will add the OMSIngestionAPI and begin extracting the activities in it.
Once it has finished extracting it will be available now as shown below:
This module includes two activities (Get=OMSAPISignature, Send-OMSAPIIngestionFile).
To determine what parameters to pass to the activity I downloaded the script locally and reviewed the content (the match the PowerShell parameters so the substitution here is simple).
- [Parameter(Mandatory = $True)]$customerId,
- [Parameter(Mandatory = $True)]$sharedKey,
- [Parameter(Mandatory = $True)]$body,
- [Parameter(Mandatory = $True)]$logType,
- [Parameter(Mandatory = $False)]$TimeStampField
By adding this module we can call the module instead of using the script pieces which were added to log to OMS.
Azure Automation script calling the OMSIngestionAPI module
The OMSIngestionAPI module can replace the existing logging functions which were in the PowerShell script. This shortens the script and should also increase the speed that the script executes (remember, you are being charged for how long your automations run so efficiency is important!). The updated script is shown below (with the updated script line and new TimeStampField in bold). The code used in the “Azure Automation” version is available from the download link provided above () under the name “SyntheticTestWebsiteandContent”.
$TimeStampfield = Date
-customerId $customerId -sharedKey $sharedKey -body $ContentFromOMS -logType $logType -TimeStampField $Timestampfield
Schedule the script to run
We define a new schedule in Azure Automation to run this synthetic web transaction on an hourly basis. In this case we are configuring to run it on the top of the hour.
Next we configure the parameters (if this environment has a hybrid runbook worker group we could choose that here).
Based on my tests it appears that Azure Automation doesn’t allow for the creation of a schedule which would run every 15 minutes. If I wanted to run this every 15 minutes I could create four of these and link each of them to the runbook. For now we’ll run it just once an hour on the top of the hour.
Bonus tip: Want an easy way to run this every 15 minutes, every 5 minutes or even every minute? Try Microsoft Flow, and create a Flow which calls the webhook for the runbook on the schedule that you want!
Configure visualization in OMS
Now that we have the data writing to OMS we can visualize it via a custom OMS solution. For samples on how to create OMS solution see the following blog posts.
The visualization for this solution in OMS is shown below.
This view is available for download as part of the full solution at https://gallery.technet.microsoft.com/Synthetic-Web-Test-for-OMS-1dd5e44d
Configure alerting in OMS
Now that we have the data we need and we can visualize it we can write simple queries and generate alerts for the conditions that we want to be notified for. Example queries are shown below:
- Content does not match: SyntheticWebTest_CL | summarize AggregatedValue = max(ResponseLength_d) by UrlToCheck_s, bin(TimeGenerated, 60m)
- URL offline: SyntheticWebTest_CL | where StatusDescription_s != “OK”
- Slow response time: SyntheticWebTest_CL | summarize AggregatedValue = avg(TimeTaken_d) by UrlToCheck_s, bin(TimeGenerated, 60m)
These queries are contained in the pre-built solution on the “List of Queries for Alerting” view.
- PowerShell script which checks a website for various metrics: https://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-for-13a551b3
- PowerShell script which checks website content: http://stackoverflow.com/questions/16993347/determine-if-webpage-has-content-using-powershell
- PowerShell script to log to OMS: https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api
- PowerShell module to log to OMS: https://www.powershellgallery.com/packages/OMSIngestionAPI/1.5
Using the combination of the Azure Automation script and the Log Analytics solution you can easily add synthetic web testing to your OMS capabilities. The script is available for download here and the OMS solution is available for download here.
Thank you to Matt Dowst for his assistance with debugging on my original PowerShell script!
[I know that this would make a great ARM template, I just haven’t gotten to that point with this solution yet]