Create a closed-loop incident management system where New Relic automatically creates ServiceNow incidents when CPU usage is high and resolves them when usage returns to normal.
POST request.correlation_id, and decides whether to insert() (Create) or update() (Close).Before configuring the integration, you need to establish your “Sandbox” environment.
ServiceNow does not run locally; you must use their cloud sandbox.
Go to the ServiceNow Developer Site.
Sign up for a free account.
Click Request Instance.
Wait a few minutes. You will receive:
https://devXXXXX.service-now.comadminXXXXXX(Save this securely)Log in to your instance using the provided URL, username, and password.
curl -Ls https://download.newrelic.com/install/newrelic-cli/scripts/install.sh | bash && sudo NEW_RELIC_API_KEY=YOUR_API_KEY NEW_RELIC_ACCOUNT_ID=YOUR_ACCOUNT_ID newrelic install
We will build a “Smart Endpoint” that accepts the alert data.
NewReliWebhooknewrelic_handlerNewRelicWebhook record you just created.HandleAlertPOST/incidentThe Script: Delete the default code in the Script field and paste the following “Traffic Cop” logic:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// 1. Parse the incoming JSON from New Relic
var requestBody = request.body.data;
var nrIssueId = requestBody.issue_id;
var nrState = requestBody.current_state;
var nrTitle = requestBody.title;
var nrCpu = requestBody.cpu_value;
// 2. Look for an existing Incident with this New Relic Issue ID
var gr = new GlideRecord('incident');
gr.addQuery('correlation_id', nrIssueId);
gr.query();
if (gr.next()) {
// --- SCENARIO: UPDATE EXISTING INCIDENT ---
if (nrState === 'CLOSED') {
gr.state = 6; // Set to 'Resolved' (Standard ID)
gr.close_code = 'Solved (Permanently)';
gr.close_notes = 'Cloased automatically by New Relic. Alert cleared.';
gr.work_notes = "New Relic reports issue is closed.";
gr.update();
return { status: "Incident Resolved", number: gr.number };
} else {
// Optional: Update work notes if it's just an update
gr.work_notes = "Received update from New Relic. Current CPU: " + nrCpu + "%";
gr.update();
return { status: "Incident Updated", number: gr.number };
}
} else {
// --- SCENARIO: CREATE NEW INCIDENT ---
if (nrState == 'ACTIVATED') {
var newInc = new GlideRecord('incident');
newInc.initialize();
newInc.short_description = "New Relic Alert: " + nrTitle;
newInc.description = "Automatic alert from New Relic.\nIssue ID: " + nrIssueId + "\nCPU Usage: " + nrCpu + "%";
newInc.correlation_id = nrIssueId; // THIS IS CRITICAL
newInc.caller_id = gs.getUserID(); // Assign to API user
newInc.urgency = 2; // High
newInc.insert();
return { status: "Incident Created", number: newInc.number };
}
}
})(request, response);
https://devXXXXX.service-now.com/api/x_scope_id/newrelic_handler/incidentadmin username and password.Policy Name = 'Low CPU Test' ( we will create policy in the next step) {
"issue_id": "",
"title": "",
"current_state": "",
"cpu_value": "N/A"
}
Local CPU Test. SELECT average(cpuPercent) FROM SystemSample WHERE `hostname = 'YOUR_HOSTNAME'
above 5 (Low threshold for easy testing) for at least 1 minute.5 minutes.Verify your serviceNow script works without waiting for New Relic.
curl "https://<YOUR_INSTANCE>.service-now.com/api/<YOUR_SCOPE_ID>/newrelic_handler/incident" \
--request POST \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--user 'admin:<YOUR_PASSWORD>' \
--data '{
"issue_id": "TEST12345",
"title": "Test High CPU Alert",
"current_state": "ACTIVATED",
"cpu_value": "95"
}'
Result</b>: Check ServiceNow incidents table. You should see a new ticket.
curl "https://<YOUR_INSTANCE>.service-now.com/api/<YOUR_SCOPE_ID>/newrelic_handler/incident" \
--request POST \
--header "Accept: application/json" \
--header "Content-Type: application/json" \
--user 'admin:<YOUR_PASSWORD>' \
--data '{
"issue_id": "TEST12345",
"title": "Test High CPU Alert",
"current_state": "CLOSED",
"cpu_value": "20"
}'
Result</b>: The ticket created in step 1 should now be marked as “Resolved”.
Now, use the strees tool to forced a real CPU spike on your local machine.
sudo apt-get install stressbrew install stressstress --cpu 8 --timeout 180 (Runs for 3 minutes).| Error | Cause | Fix |
|---|---|---|
| 401 Unauthorized | Bad credentials | Check your PDI admin password. Update the Destination in New Relic. |
| 403 Forbidden | Scope/ACL | Ensure the user has the admin or rest_service role. |
| 404 Not Found | Bad URL | Check your API ID and Resource Path. Ensure the Resource is set to POST. |
| Duplicate Incidents | Logic Error | Verify the gr.addQuery in the script matches the issue_id being sent. |