Accessing the Zendesk API with LogicMonitor.
Accessing the Zendesk API with LogicMonitor.
Details for the Zendesk API can be found from the below link
https://developer.zendesk.com/rest_api/docs/core/introduction
The below blog is not intended as a copy\paste Zendesk datasource but as instructions to create Groovy Script datasources based on custom criteria.
Zendesk data can be imported and alerted on with LogicMonitor using various API methods. This post will focus on using Zendesk Views and the Zendesk query
First we will focus on the View, specifically the count of tickets in the view. Zendesk views are a way to organize your tickets by grouping them into lists based on certain criteria. In my example I’ve created a view for tickets for my rated tickets within the last 7 days. The criteria can be anything you require. Zendesk has various json files already created for views. This example will use the count.json.
Create a Zendesk view. Load the view in Zendesk and the view ID will be in the URL
For example:
https://logicmonitor.zendesk.com/agent/filters/90139988
90139988 is the view ID.
Zendesk documentation on views can be viewed with the below link
https://support.zendesk.com/hc/en-us/articles/203690806-Using-views-to-manage-ticket-workflow
From the LogicMonitor side we can use a datasource with an embedded groovy script with the built-in json slurper to parse the data.
More information on Groovy Script Datasources can be found in the below URLs
https://www.logicmonitor.com/support/datasources/scripting-support/embedded-groovy-scripting
https://www.logicmonitor.com/support/datasources/groovy-support/how-to-debug-your-groovy-script/
Create a new Groovy Script Datasource and be sure to import the Jason slurper and http API by adding the below 3 lines to the top of the script.
// import the logicmonitor http and JsonSlurp class
import com.santaba.agent.groovyapi.http.*;
import groovy.json.JsonSlurper;
The URL and authentication information to retrieve the view’s count json is defined as
url=’https://logicmonitor.zendesk.com/api/v2/views/90139988/count.json’
Authentication is using a Zendesk token instead of a password by appending the ‘/token’ string to the user ID
user = 'jeff.woeber@logicmonitor.com' + '/token'
pwd = '##ZENDESK TOKEN##'
Next use the groovyapi HTTP
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
This will return the count json file which should look similar to the below
view_count=[url:https://logicmonitor.zendesk.com/api/v2/views/101684888/count.json, view_id:101684888, value:45, pretty:45, fresh:false]
Value=45 is the count data, so we need to parse the Value data
You can print the json to output in the !groovy debug window by using the below code
// Debug - print json slurp to identify keyvalues
// iterate over the response object, assigning key-value pairs
response_obj.each()
{ key, value ->
// print each key-value pair
println key + "=" + value;
}
This can be done by using the json slurper
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
LogicMonitor can use multi-line key value pairs, so parse so adding in a key of “TicketCount=” to the ouput will make it easy to add a datapoint for the count value. Do this by printing to the output
// Print key Value pair for Logicmonitor Datapoint
println "TicketCount=" + response_obj.view_count.value
In your datasource you can add a new datapoint
*Content the script writes to the standard output
Inturpret output with - Multi-line Key Value Pairs
Key = TicketCount
LogicMonitor will reconize the TicketCount as a datapoint and alert thresholds can be set accordingly.
In the attached Example Zendesk_TixCnt.xml The zendesk specific values have been tokenized to be added on the device level were the datasource will be applied.
Tokens required on the device are
ZEN.ACCOUNT - i.e. logicmonitor
ZEN.EMAIL - i.e. Jeff.Woeber@LogicMonitor.com
ZEN.TOKEN - Token for authentication
ZEN.VIEW - view ID if using a view (This can be found in the URL while viewing the view)
The second example uses the search API to query for tickets with a specified status for the last 48 hours.
When building the URL it’s important to remember spaces and special characters are not allowed. Use Encoded Characters instead
http://www.w3schools.com/tags/ref_urlencode.asp
an example for solved tickets in the last 48 hours query=type:ticket status:solved created>48hours
The URL after including encoded Characters.
The output will look similar to
~brand_id:854608, allow_channelback:false, result_type:ticket]]
facets=null
next_page=null
previous_page=null
count=29
We only need the count. Using the same key value output as the previous example we can add a key for "Solved"
println "solved=" + response_obj.count.value
In the attached example ZenDesk_TixStatus this process is repeated for Created, Open, Solved, Pending, On-Hold, and closed.
Zendesk_TixStatus
// Zendesk Rest API docs - https://developer.zendesk.com/rest_api/docs/core/introduction
// import the logicmonitor http and JsonSlurp class
import com.santaba.agent.groovyapi.http.*;
import groovy.json.JsonSlurper;
user = '##ZEN.EMAIL##' + '/token'
pwd = '##ZEN.TOKEN##'
//define URL and Auth
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
println "created=" + response_obj.count.value
//define URL and Auth
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20status:open%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
println "open=" + response_obj.count.value
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20status:pending%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
println "pending=" + response_obj.count.value
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20status:solved%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
println "solved=" + response_obj.count.value
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20status:On-hold%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
println "On-Hold=" + response_obj.count.value
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20status:new%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
response_obj = json_slurper.parseText(http_body);
println "New=" + response_obj.count.value
url = "https://##ZEN.ACCOUNT##.zendesk.com/api/v2/search.json?query=tickets%20status:closed%20created%3E48hours"
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
response_obj = json_slurper.parseText(http_body);
println "closed=" + response_obj.count.value
// return with a response code that indicates we ran successfully
return (0);
10:47
Zendesk_TixCount
// Zendesk Rest API docs - https://developer.zendesk.com/rest_api/docs/core/introduction
// import the logicmonitor http and JsonSlurp class
import com.santaba.agent.groovyapi.http.*;
import groovy.json.JsonSlurper;
//define URL and Auth
url = 'https://##ZEN.ACCOUNT##.zendesk.com/api/v2/views/##ZEN.VIEW##/count.json'
user = '##ZEN.EMAIL##' + '/token'
pwd = '##ZEN.TOKEN##'
// get data from the ZenDesk API
http_body = HTTP.body(url, user, pwd);
// user groovy slurper
json_slurper = new JsonSlurper();
response_obj = json_slurper.parseText(http_body);
// Debug - print json slurp to identify keyvalues
// iterate over the response object, assigning key-value pairs
// response_obj.each()
// { key, value ->
// // print each key-value pair
// println key + "=" + value;
// }
//Print KeyValues from Json slurp
println "TicketCount=" + response_obj.view_count.value
// return with a response code that indicates we ran successfully
return (0);