The CreatorCon Call for Content is officially open! Get started here.

satkaysatish
Kilo Explorer

This requirement popped up during my recent engagement wherein the customer had deployed Service now as the ticketing tool. Service now does has notifications / alerts for new tickets but sending out these notifications & managing them can be an overwhelming task if they have high number of users and multiple accounts handled via same ServiceNow instance. Typical use case would be a service oriented organisation supporting multiple clients. On the other hand these notifications play a crucial role to ensure new tickets don't miss the agreed SLA.   To cut short, ServiceNow folks didn't wanted to configure for individual users & I desperately needed this functionality.

Checking the Service-Now portal every 10 mins could be a daunting task.

since i was still waiting for my credentials to be created, i started to explore options to get this automated, (i'm the lazy guy that Mr. Gates was searching for his tasks )

Read some articles and learnt that Python can do the job, so i started to explore in that direction.

I gave a try to all my imaginations , but nothing seemed to work.

Then one day i striked gold wherein i came across the support portal of Service now which discussed the API's provided in it with some scripts and to my luck they had python scripts as well ( wow )

I knew that this would work, but my system was behind internet proxy, and https access via python 2.7 i didn't know how to do that.

Then this tool called as fiddler came to my rescue & voilí  it worked for the first time. Just let fiddler auto authenticate you to your proxy. Later i learnt of another tool cntml which is much better hence integrated it with my solution.

I was glad and could see a new ray of hope. I was confident of getting the output file parsed and extract the info required. Rest is history, i spent another 2 weeks writing the parser in VB. Installed and configured CNTML and integrated it with blat to send those lovely reports.

Here are the steps to make them working on your system.

  • Install Python 2.7.12, I've installed it in c:\python

  • Install "requests" module for python required to access websites

                  https://github.com/kennethreitz/requests/tarball/master

                  Extract & install

                    python setup.py install

                  To verify the installation

                  pip install requests

                  Requirement already satisfied (use --upgrade to upgrade): requests in c:\python\lib\site-packages\requests-2.11.1-py2.7.egg

  • Download and copy blat in c:\tools\blat\

              Folder content.

Blat_listing.png

  • Prepare the query that needs to be executed.

Logon to ServiceNow portal, from the top section of the window near filter icon, right click and select "Copy Query"

        Service-Now-Query.png

  • Create Python script to call ServiceNow API with the required query to be executed & based on output call the parsing tool

import requests    

import os    

import codecs    

user = 'SatKay'

pwd = 'Satish'

# Change the myinstance.service-now.com to corresponding URL

# Append the query generated in previous step

url = 'https://myinstance.service-now.com/api/now/table/incident?sysparm_query=your_query_here'

# Set proper headers

headers = {"Accept":"application/json"}

# Do the HTTP request

response = requests.get(url, auth=(user, pwd), headers=headers ,verify=False )

# Check for HTTP codes other than 200

if response.status_code != 200:      

print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())

      exit()

a = (response.json())

# Check if there are any records to be processed

if str(a) == "{u'result': []}":                       # This means the returned string is empty, no further

# processing is required

      b = True  

#     Else output the content to file, INPUT for the parser

else:          

      f1 = open('C:\\temp\\out.txt', 'w')      

      f1.write(str(a))

      f1.close()

      os.system('"C:\\tools\\Snow-Processor.exe"')   # Call the parsing tool

  • Create the batch script to periodically call Python script
c:\python\python.exe C:\tools\Incident_Monitor.py  
  • Setup the parsing tool to parse the output and send out notifications
Multiple instances of Parsing tools [Snow-Processor]   can be configured to run.
Create folder for each run &   copy the binary and configuration .ini file
Snow-Processor.INI file configuration

In=out11.txt                                                                             # Captured from Python script output

From=New-Incident@SatKay.net                                                    

TO=""birje@satkay.net,varma@satkay.net"" # please note 2 ""

Sub=New-Incident                                                                           # No spaces, blat limitations

Body=Please do the following    

CNTLM Config:

  • Download and install CNTML
  • Generate password hash

cntlm -I -M http://test.com

Copy the Auth and PassNTLMv2 lines — or whatever shows up for you in that section,                   there might be more — and paste them into the configuration file below the Domain                   line.

  • Update the config file with proxy and authentication details

        cntml.png

Running Multiple Instances of Snow-Processor:

  • Create a folder for this run & copy the Snow-Processor binary and ini file
  • Update the python script with the query to run, query output file and path of the parser

        multiple-instance.jpg

  • Update the ini file

        multiple-instance_INI.jpg      

Sample Report:

        Sample_Report.jpg

With this you can make ServiceNow dance to your tunes ;), Prepare whatever query you want & fire it . What returns back is output in JSON format.

  References & Credits

http://wiki.servicenow.com/index.php?title=Table_API#GET_.2Fapi.2Fnow.2Fv1.2Ftable.2F.28tableName.29...

http://wiki.servicenow.com/index.php?title=Table_API_Python_Examples#gsc.tab=0

CNTML Reference
I don't know what happened to this blog, but its the one that i referred. A VERY Big Thanks to Scott Offen
http://scottoffen.com/2013/10/29/solving-proxy-problems-with-cntlm/
Scripts & Binary Downloads:


Parser
Parser_INI

Sample_Python_Script

Batch File : c:\python\python.exe C:\Users\A645336\Desktop\scripts\Incident_Monitor.py

My Blog: Satkay's Blog: Leveraging ServiceNow API's for notifications

Version history
Last update:
‎02-12-2017 08:36 AM
Updated by: