Introduction
This document is a walk through on how to configure Vyopta monitors to generate tickets in ServiceNow using webhook notifications. This document also covers the changes needed within Service Now to enable the integration.
Requirements
- Created a Scripted REST API in Service Now
- Create a custom Table in Service Now to track alerts coming in
Configuring Webhooks in Vyopta
This section will walk through setting up a webhook for a monitor in Vyopta.
- Go to Vyopta Tech Insights Monitoring
- Click on Rules located under Monitors on the Left Menu
- Click Add Monitor if you’re configuring a new monitor or click the name of an existing monitor
- Once the monitor is configured, go to the “Notify” section and click “Add Webhook”
- Provide a name for the monitor and ensure the all the days are selected and the Active Schedule is set from 00:00 to 23:59
- In the “Data” field add the following – We will use the example below:
Content-Type: application/json
{
"id": "[(${alertId})]",
"name": "[(${name})]",
"description": "[(${category})] [(${metric})] [(${description})] [(${operation})] [(${threshold})] for [(${alertDurationMinutes})] minutes",
"group": "[(${group})]",
"status": "[(${status})]",
"category": "[(${category})]",
"metric": "[(${metric})]",
"alertFiredTime": "[(${alertFiredTimeMs})]"
}
**Note: Additional fields can be added by clicking on the following icon:
7. Save the Monitor
Service Now Setup
To configure ServiceNow to accept webhooks from Vyopta and track the status, both a custom table is required and a Scripted Rest API.
Custom Table Configuration
- Head to the Tables located under the System Definition Menu
- Click on “New”
- Fill in the Label Name. We recommend using vyopta_alerts
- Under the Columns Tab, click double click on “Insert a new row…” as seen below Scripted Rest API code snippet:
5. Create the following columns as Type string:
- sys_id
- vyopta_monitor_id
- vyopta_name
- vyopta_status
Create a scripted Rest API
- Go to “Scripted Rest API” in the ServiceNow Menu
- Click the “New” button on the right hand side
- Give the Scripted Rest API the name “Vyopta Webhooks” and hit submit
- Once you hit submit, it exits the area, so search for “Vyopta Webhooks” and click on it to launch.
- Click New under the resources tab as seen in the below image
6. Give the resource a name of “Incoming Vyopta Alerts”
7. Change the HTTP Method from “Get” to “Post”
8. Under the Security Tab, uncheck “Requires authentication”
9. Hit Save
10. Return to “Incoming Vyopta Alerts” and paste the following code:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body;
var requestData = requestBody.data;
shortDescription = requestData.id;
description = requestData.name + " " + requestData.status + " " + requestData.category;
/* Need to lookup the DB first before creating an incident. */
var db = new GlideRecord('u_vyopta_alerts');
var query = 'u_vyopta_monitor_id=' + requestData.id + '^' + 'u_vyopta_name=' + requestData.group;
db.initialize();
db.addQuery(query);
db.query();
var issueBody = '[code]';
if(requestData.status === 'CRITICAL') {
issueBody += '<h2 style="text-align: center; color: red;">' + requestData.status + '</h2>';
} else if(requestData.status === 'WARN') {
issueBody += '<h2 style="text-align: center; color: orange;">' + requestData.status + '</h2>';
} else if(requestData.status === 'INFO') {
issueBody += '<h2 style="text-align: center; color: blue;">' + requestData.status + '</h2>';
} else if(requestData.status === 'CLEAR') {
issueBody += '<h2 style="text-align: center; color: green;">' + requestData.status + '</h2>';
} else {
issueBody += '<h2 style="text-align: center; color: purple;">' + requestData.status + '</h2>';
}
issueBody += '<table style="width: 50%;"><tbody>';
issueBody += '<tr><td>Name</td><td>'+ requestData.name + '</td></tr>';
issueBody += '<tr><td>Description</td><td>'+ requestData.description + '</td></tr>';
issueBody += '<tr><td>Group</td><td>'+ requestData.group + '</td></tr>';
issueBody += '<tr><td>Category</td><td>'+ requestData.category + '</td></tr>';
issueBody += '<tr><td>Metric</td><td>'+ requestData.metric + '</td></tr>';
issueBody += '<tr><td>Metric</td><td>'+ requestData.alertFiredTime + '</td></tr>';
issueBody += '</tbody></table>[/code]';
//Check if a record already exists
if(db.next() == true) {
//If record exists, get sys_id of ticket which is u_sys_id
var u_sys_id = db.getDisplayValue('u_sys_id');
var incLookup = new GlideRecord('incident');
incLookup.addQuery('sys_id', u_sys_id);
//If the status is has not changed - Update ticket with worknotes
if(requestData.status === 'CRITICAL' || requestData.status === 'WARN' || requestData.status === 'INFO') {
incLookup.query();
db.setValue('u_vyopta_status', requestData.status);
db.update();
while(incLookup.next()) {
incLookup.work_notes = issueBody;
incLookup.update();
}
} else if (requestData.status === 'CLEAR') {
//If a clear message is sent, update worknotes with a clear message - Also have the ability to close ticket.
db.setValue('u_vyopta_status', requestData.status);
db.deleteRecord();
incLookup.addQuery('sys_id', u_sys_id);
incLookup.query();
while(incLookup.next()) {
incLookup.work_notes = issueBody;
incLookup.update();
}
} else {
//If the status is another status, update worknotes with info.
db.setValue('u_vyopta_status', requestData.status);
db.update();
incLookup.addQuery('sys_id', u_sys_id);
incLookup.query();
while(incLookup.next()) {
incLookup.work_notes = issueBody;
incLookup.update();
}
}
} else {
//If record does not exist in u_vyopta_alerts table - Create a new incident and record it in the table
var grIncident = new GlideRecord('incident');
grIncident.initialize();
grIncident.impact = '2'; //Set the Impact
grIncident.urgency = '2'; //Set the Urgency
grIncident.short_description = requestData.name; //Set Short Description of the ticket
grIncident.description = description; //Set the description of the ticket
grIncident.work_notes = issueBody; //Create a worknote if needed
var sys_id = grIncident.insert(); //Create incident and grab the sys_id of the ticket
db.setValue('u_vyopta_monitor_id', requestData.id); //Set the Vyopta Monitor ID in u_vyopta_alerts table
db.setValue('u_vyopta_name', requestData.group); //Set the Vyopta name in u_vyopta_alerts table
db.setValue('u_vyopta_status', requestData.status); //Set the status in u_vyopta_alerts table
db.setValue('u_sys_id', sys_id); //Set the ticket sys_id in the u_vyopta_alerts table
db.insert();
return 'Insert Into DB';
}
})(request, response);
You have now completed the Vyopta/ServiceNow integration! You should now see tickets appear once a monitor alert is triggered!
Comments
Please sign in to leave a comment.