logo

NJP

stail: tail -f the ServiceNow System Log

Import · May 20, 2016 · article

Ever miss the 'tail -f' in the Unix world on ServiceNow? I did, so I quickly wrote a Python script to tail the System log. It works by polling your ServiceNow instance looking for new records to simulate tailing the logs.

image

How to use:

1. Copy and paste the code below

2. Make the script executable

chmod +x stail.py

3. Edit the script for your instance

4. Execute

./stail.py

OR

./stail.py "Workflow"

stail.py

!/usr/bin/python

STAIL - Pulls logs from a Service Now Instance

v0.2

Usage.

1. Define the core variables.

2. Execute - optional search for a keyword

              ./stail.py

              ./stail.py "SLA"

Define core variables

host = 'xxxxxx.service-now.com'

user = 'dan.young'

pwd = 'xxxx'

rowLimit = '200'

pollTime = 8

Need to install requests package for python

easy_install requests

import requests

import time

import re

import sys

requests.packages.urllib3.disable_warnings()

sys.tracebacklimit = 0

Set the request parameters

url = 'https://' + host + '/api/now/table/syslog?sysparm_query=ORDERBYDESCsys_created_on&sysparm_limit=' + rowLimit

Inputs

grep = ''

if len(sys.argv) > 1:

      grep = sys.argv[1]

Set proper headers

headers = {"Content-Type":"application/json","Accept":"application/json"}

levelType = {'-1': 'Debug', '2': "Error", '0': 'Information', '1': 'Warning'}

logs = []

def getLogs(logs):

      # Do the HTTP request

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

      # Check for HTTP codes other than 200

      if response.status_code != 200:

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

      # Decode the JSON response into a dictionary and use the data

      data = response.json()

      results = data["result"]

      for row in reversed(results):  

              iD = row["sys_id"]

              if iD in logs:

                      i = 0 # do nothing

              else:

                      l = levelType[row["level"]]

                      p = 0

                      if grep == '':

                              p = 1

                      elif grep and re.search(grep, row["message"]):

                              print 'ex';

                              p = 1

                      else:

                              p = 0

                      if p == 1:

                              print row["sys_created_on"] + "\t" + l + "\t" + row["message"]

                      logs.append(iD)

                      if len(logs) > int(rowLimit + rowLimit):

                              del logs[0]          

      return logs

while (1 > 0):

      logs = getLogs(logs)

      time.sleep(pollTime)

Warning

Code has not been throughly tested and please take it easy on your ServiceNow instance.

View original source

https://www.servicenow.com/community/developer-articles/stail-tail-f-the-servicenow-system-log/ta-p/2323231