This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.
Alerts can be sent for any particular state of an elastic job. In this article, we discuss the case of 'Failed' jobs, however the solution can be easily extended to other scenarios. The script can be executed on client machines in background on a schedule or in Azure Automation Runbook. If failed jobs are identified, email notifications are sent, with an attachment of a log file containing only the failed jobs.
Elastic Jobs are currently in public preview and the monitoring capabilities are resuming for now to ad-hoc executions of T-SQL / PowerShell commands to retrieve the elastic job execution status, with no embedded option for sending alerts in case of a job failure.
This need has been addressed through a PowerShell script that filters job executions based on their status and sends the output to a log file, that is further sent as an attachment in a notification email and in the same time the log file is stored in an Azure Storage Account for further reference.
The jobs are filtered with the below T-SQL, that can be modified as per the requirements:
Other possible values for job execution states are Created, InProgress, WaitingForRetry, Succeeded, SucceededWithSkipped, Failed, TimedOut, Canceled, Skipped and WaitingForChildJobExecutions.
Generated log file:
Save log file to storage account:
The email functionality can be leveraged through any Simple Mail Transfer Protocol server. The proposed script is using smtp.mail.yahoo.com on port 587.
How to run and automate alerts
As elastic jobs can be scheduled to run on a regular basis from both PowerShell and T-SQL, so our alert script can. Depending on the desired deployment type you choose, there are several options. For any of the below options, you have to set the required parameters in the script: Insert SQL Server Details, Credentials & SMTP server details:
OPTION#1
Run the script on schedule in background on client machine
In order to run it you need to:
-
Open Windows PowerShell ISE in Administrator mode
-
Open a New Script window
-
Paste the content in script window
-
Run it
-
If failed jobs are found, an alert email will be triggered and the log file containing details on the failed jobs will be attached to the email and either saved locally or sent to a storage account. The result of the script can be followed in the output window.
-
To run the script in background on a schedule, you can use the following commands:
OPTION#2
Run the script from Azure Runbook
- Create a new Automation Account as described here and make sure you choose "YES" for option "Create Azure Run As Account".
- Import the following Azure Modules by browsing the gallery:
Az.Accounts (≥ 2.2.2)
Az.Storage
Az.Automation
- Create a runbook to run the script and make sure you choose Powershell runbook type.
- Add the following login section when connecting from a Runbook
- Schedule task
Note: Script execution can be monitored in the portal and alerts can be set for any script execution failure.
The script can be found in the following Git repository. This project welcomes contributions and suggestions.