Redgate Monitor 14

Setting up script notifications

Script notifications are currently only available for PowerShell scripts.

About script notifications

You can set up Redgate Monitor to execute a script using the monitor service account as a response to alert notifications. 

Script notifications are executed whenever any alert begins, escalates, de-escalates, or ends. You can make use of the alert data object that gets passed as a parameter. See Alert data properties for more details.

Note that like all other notification types, the scripts are configured per Base Monitor.

Security considerations

Monitor will run the script you've provided as the same user that runs the Redgate Monitor Base Monitor service. You should make sure that this user's permissions are as limited as possible in order to avoid security risks.

Enabling this feature should only be done after careful consideration since it could open your system to potential attacks by a bad actor.

Enabling script notifications

By default, script notifications are disabled at the feature flag level.

In order to enable script notifications, you need to enable the ScriptAlertResponse feature flag. You can do this by setting the environment variable SQLMONITOR_ScriptAlertResponse to 1.

In order for the change to make its effect, you will need to restart both the Redgate Monitor Base Monitor and the Redgate Monitor Web services.

If you have multiple base monitors, you will need to enable the feature flag on all of the Base Monitors you wish the scripts to run from, plus the Website.

Configuring script notifications

Script notifications can be configured, once enabled, as follows:

Go to the Configuration page. Under Alerts, select Notification settings. You will need to be an administrator to access this.

The Alerts section of the configuration page

Under Script Notifications, click the checkbox and click Proceed.

You can then write your PowerShell script inside the box. The parameter $AlertData will be available within the script, containing alert information.

Note again that this script will only apply to the base monitor selected at the top of the page, if you have multiple.

You can choose how many notifications each alert type can send for each monitored object in a 24-hour period. The default limit is 30 notifications, and it can’t be set lower than 1.

Alert data properties

The following parameters are present within the $AlertData object and can be used within a script.

ParameterDescriptionExampleData type
AlertIdId of the alert1234Integer
AlertNameName of the alertDisk space alertString
AlertStatusStatus of the alertRaisedEnum (None, Raised, Ended, Escalated, DeEscalated)
AlertSeveritySeverity of the alertHighEnum (Unknown, None, Low, Medium, High)
AlertUrlAlert details Url of the alerthttps://your-sql-monitor-path/Alerts/localhost/Details/4521988String
EventDateTimeWhen this event occurred (different to the raised time for Escalated, DeEscalated, or Ended alert-status types)2022-10-12 18:02:12.1651874ZDateTime
RaisedDateTimeWhen the alert was originally raised2022-10-12 17:46:30.2567915ZDateTime
PreviousAlertSeveritySeverity of the previous alertMediumEnum (Unknown, None, Low, Medium, High)
AlertDescriptionDescription of the alertThe instance is not running or cannot be contacted on the network.String
Target.CirChannel instance reference of the alert targetRoot[].[Cluster][[Name]=failover-cluster.domain.local].[SqlServer][[Name]=node-0\sql2019]String
Target.NameName of the alert targetnode-0\sql2019String
Target.GroupNameName of the group that the alert target is part of3 - DevelopmentString
Target.ClusterNameName of the cluster that the alert target is part of (if any)failover-cluster.domain.localString
Target.MachineNameName of the machine that the alert targetsDESKTOP-123ABCString
Target.FailoverClusterActiveNodeNameName of the active node hosting the failover cluster SQL Server instance (iff the alert targets a failover cluster SQL Server instance)node-0String
Target.AzureSqlServerNameName of the Azure SQL Server that the alert is targetingexample-azure-sql-server.database.windows.netString
Target.AzureSqlDatabaseNameName of the Azure SQL database that the alert is targetingexample-azure-sql-databaseString
Target.AzureElasticPoolNameName of the Azure Elastic Pool that the alert is targetingexample-azure-elasticpoolString
Target.SqlInstance.NameName of the SQL Server instance that the alert is targeting

node-0\sql2019

String

Target.SqlInstance.DisplayNameDisplay name of the SQL Server instance that the alert is targeting

node-0\(local)

String

Target.SqlInstance.AliasThe alias that has been assigned to the sql instance alert target  

sql2019 (primary)

String

Target.TagsThe list of tags the alert target is tagged with 

[ { Name = tag1 }, { Name = tag2 } ]

[ { Name: String } ]

BaseMonitorGuidGuid identifying the Base Monitor servicea1b2c3d4-e5f6-a7b8-c9d0-e1f2a3b4c5d6String

Example script

Here is an example script that writes the alert data object to a JSON file.

Example script

param (
    # The alert data object
    $AlertData
)

# Default working directory is the install directory of monitor on the alerting base monitor. This changes it to another directory.
Set-Location -Path "C:\temp"

# Append alert data object as JSON
$AlertData | ConvertTo-Json | % {"$($_),"} | Out-File -Append -FilePath alert.json

Didn't find what you were looking for?