Can someone provide me an example of how to mute multiple hosts in New Relic using Python?

I want to mute few hosts on the basis of events using python

Hi, @vijay.k: What do you mean by “mute hosts”? New Relic muting rules apply to alert notifications, not hosts.

I meant the same @philweber I wanted to mute alert notifications triggered by few hosts. I need an example on how to use muting rules with python

You may find this post helpful:

1 Like

Hi @philweber Thanks for directing me to an example. I have made it working but i have one small issue. How can i pass variables from python like host, account id to nerdgraph i have tried few ways but its getting passed as null

import requests
import json

account_id = "XXXXXX"
host = "host.example.com"

def nerdgraph_muteinstance():
    query = """
   mutation($host: String!) {
  alertsMutingRuleCreate(
    accountId: XXXXX
    rule: {
      name: "host-1 mute rule"
      description: "Mute host-1 violations"
      enabled: true
      condition: {
        operator: AND
        conditions: [
          {
            attribute: "tag.hostname"
            operator: EQUALS
            values: [$host]
          }
        ]
      }
    }
  ) {
    id
  }
}
    """
    variables = {
        host: host
    }
    endpoint = "https://api.newrelic.com/graphql"
    headers = {
        'Content-Type': 'application/json',
        'Api-Key': 'api-key'
    }
    response = requests.post(endpoint, headers=headers, json={'query': query,'variable': json.dumps(variables)})
    if response.status_code == 200:
        dict_response = json.loads(response.content)
        print(dict_response)
    else:
        raise Exception(
            f'Nerdgraph query failed with a {response.status_code}')


nerdgraph_muteinstance()

Here is my python script

https://dgraph.io/docs/query-language/graphql-variables/