Back to top icon

New Relic Developers

Automate your workflows

Reduce toil by programmatically managing assets via APIs and auto-instrumentation.

Auto-instrument infrastructure

Setting up a New Relic Infrastructure agent on a single host is a pretty painless task. But doing that for 1,000 hosts? Less so. That's why New Relic integrates with popular config management tools like Chef, Puppet, and Ansible.

This example shows how to manage the installation of the New Relic Infrastructure agent via Ansible.

- hosts: ap_northeast_1
  roles:
    - name: newrelic.newrelic-infra
      vars:
        nrinfragent_os_name: YOUR_OS_NAME
        nrinfragent_os_version: YOUR_OS_VERSION
        nrinfragent_config:
          license_key: YOUR_LICENSE_KEY
          log_file: /var/log/newrelic/nr-infra.log
          log_to_stdout: false
Programmatically manage entities

Manually managing hundreds of alerts, dashboards, Synthetic monitors, cloud integrations, and more resources can be daunting. Thankfully, you can use APIs to help manage your New Relic entities, including basic CRUD operations.

As an example, you can create the following dashboard directly via New Relic Insights or you can create it programmatically via an API. Using an API will allow you to save the dashboard in source control and modify it as part of your deployment.

nrql-101

The dashboard above represented in JSON for the Dashboards API.

{
  "dashboard": {
    "title": "NRQL 101",
    "icon": "bar-chart",
    "visibility": "all",
    "editable": "editable_by_owner",
    "owner_email": "alarson@newrelic.com",
    "metadata": {
      "version": 1
    },
    "widgets": [
      {
        "visualization": "event_table",
        "layout": {
          "width": 2,
          "height": 2,
          "row": 1,
          "column": 1
        },
        "data": [
          {
            "nrql": "SELECT * FROM Transaction WHERE duration > 2 SINCE 1 day ago"
          }
        ],
        "presentation": {
          "title": "APM",
          "notes": null
        }
      },
      {
        "visualization": "facet_bar_chart",
        "layout": {
          "width": 1,
          "height": 2,
          "row": 1,
          "column": 3
        },
        "data": [
          {
            "nrql": "SELECT average(duration) FROM PageView WHERE countryCode != 'US' SINCE this week FACET pageUrl"
          }
        ],
        "presentation": {
          "title": "Browser",
          "notes": null
        }
      },
      {
        "visualization": "facet_bar_chart",
        "layout": {
          "width": 1,
          "height": 1,
          "row": 3,
          "column": 1
        },
        "data": [
          {
            "nrql": "SELECT average(duration), percentage(count(*), WHERE result='SUCCESS') FROM SyntheticCheck FACET dateOf(timestamp) SINCE 1 week ago"
          }
        ],
        "presentation": {
          "title": "Synthetics",
          "notes": null
        }
      },
      {
        "visualization": "facet_pie_chart",
        "layout": {
          "width": 1,
          "height": 1,
          "row": 3,
          "column": 2
        },
        "data": [
          {
            "nrql": "SELECT uniqueCount(uuid) FROM MobileSession FACET device"
          }
        ],
        "presentation": {
          "title": "Mobile",
          "notes": null
        }
      },
      {
        "visualization": "line_chart",
        "layout": {
          "width": 1,
          "height": 1,
          "row": 3,
          "column": 3
        },
        "data": [
          {
            "nrql": "SELECT average(cpuSystemPercent), average(cpuIOWaitPercent), average(cpuUserPercent), average(cpuStealPercent) FROM SystemSample SINCE 60 minutes ago TIMESERIES auto"
          }
        ],
        "presentation": {
          "title": "Infrastructure",
          "notes": null
        }
      }
    ],
    "filter": null
  }
}

If you put the JSON into a file named 'dashboard.json', you can upload it to New Relic with the following command.

cat dashboard.json | curl -d @- -X POST -H "Content-Type: application/json" \
    -H "X-Api-Key: YOUR_ADMIN_API_KEY" https://api.newrelic.com/v2/dashboards.json
Automate alert response

When your system goes down and alerts fire, you can use webhooks to automate your issue response and remediation services.

Webhooks can be configured to point to any URL and even perform basic authentication and custom payload posting. To set up and manage webhooks, add them as a notification channel from New Relic Alerts.

uc3-sec3-alert

Example webhook JSON payload.

{
  "event_type": "$EVENT_TYPE",
  "account_id": "$ACCOUNT_ID",
  "account_name": "$ACCOUNT_NAME",
  "condition_id": "$CONDITION_ID",
  "condition_name": "$CONDITION_NAME",
  "current_state": "$EVENT_STATE",
  "details": "$EVENT_DETAILS",
  "duration": "$DURATION",
  "incident_acknowledge_url": "$INCIDENT_ACKNOWLEDGE_URL",
  "incident_id": "$INCIDENT_ID",
  "incident_url": "$INCIDENT_URL",
  "owner": "$EVENT_OWNER",
  "policy_name": "$POLICY_NAME",
  "policy_url": "$POLICY_URL",
  "runbook_url": "$RUNBOOK_URL",
  "severity": "$SEVERITY",
  "targets": "$TARGETS",
  "timestamp": "$TIMESTAMP",
  "violation_callback_url": "$VIOLATION_CALLBACK_URL",
  "violation_chart_url": "$VIOLATION_CHART_URL"
}