- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-19-2016 09:50 PM
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.
How to use:
1. Copy and paste the code below
2. Make the script executable
chmod +x stail.py3. Edit the script for your instance
- Host
- Username
- Password
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.
- 1,450 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hello Daniel,
The service now platform actually comes bundled with a logtail module that replicates the tail - f command. It is hidden from end users in the navigation menu and admin rights are required to access it. I have documented how to utilise the logtail module in the link below:
K*I*S*S REST API Troubleshooting made simple
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Bonswa mw se haisyen map chèche èd