Feature Idea: How to get notification channel information for a Synthetic monitor with REST API?

Hi all,

Specifically, how can I get a list of notification channel email addresses of each Synthetic monitor with REST API?

Regards,
Alan


New Relic Edit

  • I want this too
  • I have more info to share (reply below)
  • I have a solution for this

0 voters

We take feature ideas seriously and our product managers review every one when plotting their roadmaps. However, there is no guarantee this feature will be implemented. This post ensures the idea is put on the table and discussed though. So please vote and share your extra details with our team.

Hi, @Alan_Tsui: I think you will have to:

  1. Get a list of all alert policies
  2. For each alert policy, check if it includes Synthetic monitors
  3. Get a list of all alert channels
  4. Compare the channels[links][policy_ids] list of each alert channel with the list of Synthetics-containing alert policies from Step 2
1 Like

That is really painful. I hear a feature request being warmed up :slight_smile:

Thank you @philweber for your confirmation.
And yes @stefan_garnham, it’s very painful…
May I know which feature request did you refer to?

I meant that your original enquiry would make a great feature request. An API method to get the notification channels for a synthetic would be a great new feature.

Hey @Alan_Tsui - @stefan_garnham is right, a clean solution for this doesn’t currently exist. I have created a feature idea for you, and I have converted your post into a feature idea poll :slight_smile:

1 Like

Hi @RyanVeitch,

Please loop me in.

Happy new year~

1 Like

Thanks Alan, Happy New Year to you too. :slight_smile:

We’ll try to keep this thread up to date as we hear more on the feature idea!
@Alan_Tsui

-Ryan.

I agree that the nested queries via API can be a bit troublesome; but just in case you need a solution faster than the FR can be implemented; you can try this out:

note: this is based on a few of the functions I keep in my public Github repo

results:

PowerShell script:

#region Top of Script

#requires -version 2

<#
.SYNOPSIS
	Queries the New Relic API to audit alert policies and their associated Synthetics alert conditions and notification channels

.DESCRIPTION
    Requires the Account API Key for the target account

.NOTES
	Version:		1.0
	Author:			Zack Mutchler
	Creation Date:	12/31/2019
    Purpose/Change:	Initial script development.
#>

#endregion

#####-----------------------------------------------------------------------------------------#####

#region Script Parameters

Param(

    [ Parameter( Mandatory = $true ) ] [ValidateNotNullOrEmpty() ] [ string ] $AccountAPIKey

)

#endregion Script Parameters

#####-----------------------------------------------------------------------------------------#####

#region Functions

# Create a function to enumerate all Alert Policies for an account
Function Get-NRAlertPolicies {

    Param (

        [ Parameter (Mandatory = $true ) ] [ string ] $AccountAPIKey,
        [ Parameter (Mandatory = $false ) ] [ string ] $FilterName,
        [ Parameter (Mandatory = $false ) ] [ ValidateSet( 'true', 'false' ) ] [ string ] $ExactMatch = 'false'

    )

# Set the target URI if no FilterName is provided
If ( ( !$FilterName ) ) {

$getPoliciesUri = "https://api.newrelic.com/v2/alerts_policies.json"

}

# Set the target URI with provided filter
Else {

$getPoliciesUri = "https://api.newrelic.com/v2/alerts_policies.json/?filter[name]=" + $FilterName + '&filter[exact_match]=' + $ExactMatch

}

# Set the headers to pass
$headers = @{ 'X-Api-Key' = $AccountAPIKey; 'Content-Type' = 'application/json' }

# Query the API
$results = ( Invoke-RestMethod -Method Get -Uri $getPoliciesUri -Headers $headers ).policies

RETURN $results

}

# Create a function to enumerate all Synthetics Alert Conditions for an account
Function Get-NRSyntheticsConditions {

    Param (

        [ Parameter (Mandatory = $true ) ] [ string ] $AccountAPIKey,
        [ Parameter (Mandatory = $true ) ] [ string ] $PolicyID

    )

# Set the target URI with query string
$getSyntheticsConditionsUri = "https://api.newrelic.com/v2/alerts_synthetics_conditions.json?policy_id=" + $PolicyID

# Set the headers to pass
$headers = @{ 'X-Api-Key' = $AccountAPIKey; 'Content-Type' = 'application/json' }

# Query the API
$results = ( Invoke-RestMethod -Method Get -Uri $getSyntheticsConditionsUri -Headers $headers ).synthetics_conditions

RETURN $results

}

# Create a function to enumerate all Notification Channels for an account
Function Get-NRNotificationChannels {

Param(

    [ Parameter ( Mandatory = $true ) ] [ string ] $AccountAPIKey

)

# Set the target URI
$uri = 'https://api.newrelic.com/v2/alerts_channels.json'

# Set the headers to pass
$headers = @{
	'X-Api-Key' = $AccountAPIKey
}

# Query the API
$results = ( Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -ContentType 'application/json' ).channels

RETURN $results

}

#endregion Functions

#####-----------------------------------------------------------------------------------------#####

#region Execution

# Get a list of all Alert Policies
$policies = Get-NRAlertPolicies -AccountAPIKey $AccountAPIKey

# Get a list of all Notification Channels
$channels = Get-NRNotificationChannels -AccountAPIKey $AccountAPIKey

# Build an empty array to hold our Alert Policy IDs that have Synthetics Alert Conditions
$syntheticsPolicies = @()

# Iterate through each policy and isolate the ones with Synthetics Alert Conditions
foreach ( $p in $policies ) {

    $query = Get-SyntheticsConditions -AccountAPIKey $AccountAPIKey -PolicyID $p.id

    foreach ( $q in $query ) {

            Write-Host "Synthetics Conditions found in Alert Policy: $( $p.name )" -ForegroundColor Cyan
            $syntheticsPolicies += $p.id

    }

}

# Build an empty array to hold our results
$results = @()

# Iterate through the Notification Channels to see if any are assigned to a Policy with Synthetics Alert Conditions
foreach ( $c in $channels ) {

    foreach( $l in $c.links.policy_ids ) {

        # Build a PSObject to add to our results
        $item = New-Object -TypeName psobject

        if( $l -in $syntheticsPolicies ) {

            # Grab the alert policy name for readability in the results
            $pName = ( $policies | Where-Object {  $_.id -eq $l } ).name
            Write-Host "Notification Channel "$( $c.name )" is assigned to Alert Policy: $( $pName )" -ForegroundColor Yellow
            
            # Fill the PSObject
            $item | Add-Member -MemberType NoteProperty -Name 'AlertPolicyID' -Value $l
            $item | Add-Member -MemberType NoteProperty -Name 'AlertPolicyName' -Value $pName
            $item | Add-Member -MemberType NoteProperty -Name 'ChannelID' -Value $c.id
            $item | Add-Member -MemberType NoteProperty -Name 'ChannelName' -Value $c.name
            $item | Add-Member -MemberType NoteProperty -Name 'ChannelType' -Value $c.type
          
            # Add the PSObject to our results array
            $results += $item

        }

    }

}

#endregion Execution

# Play that beautiful bean footage...
$results | Format-Table -AutoSize
4 Likes

Thank you @anon85944545.
I programmed a Python script for this too, please free feel to comment on my public GitHub repo.

Sample output

2 Likes

This is great! Thanks both of you for sharing those scripts! @anon85944545 @Alan_Tsui