Skip to content

Commit

Permalink
working ticket creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeder committed Mar 1, 2015
1 parent 4b82893 commit 34343c2
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions ticketer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sqlite3
import sys
import os
import pprint
import requests

# Determine current working directory and set database path
Expand All @@ -16,7 +17,7 @@

# Read credentials for accessing ServiceNow REST API
with open('credentials.json') as data_file:
sn_data = json.load(data_file)
sn_access = json.load(data_file)
data_file.close()

def main(argv):
Expand Down Expand Up @@ -101,7 +102,6 @@ def dbAdd(sysid,host,srvc):

# Lookup an incident record in the database and pass ID to dbDel()
def dbLookup(host,srvc):
# CHANGE id TO sysid ONCE RECORDS ARE GETTING POPULATED
sql = '''
SELECT sysid FROM incident WHERE host = ? AND srvc = ?'''
db = sqlite3.connect(dbpath)
Expand Down Expand Up @@ -132,11 +132,31 @@ def dbDel(sysid):

# Create new ticket in Service Now
def createInc(alarm):
print("Creating ticket for %s" % alarm)
print("Creating ticket...")
### Insert call to SN API here and grab sysid from response body
sysid = '2nk20912kl32u109312'
url = sn_access["api"] + 'now/table/incident'
user = sn_access["user"]
password = sn_access["pass"]
alarmType = alarm[0]
host = alarm[1]
addr = alarm[2]
srvc = alarm[3]
output = alarm[4]
state = alarm[5]
headers = {"Accept":"application/json","Content-Type":"application/json"}
payload = {}
payload["short_description"] = "%s %s is %s" % (host,srvc,state)
payload["comments"] = "AutoGenerated by Shinken"
payload["customer"] = "Mike Eder"
payload["Affected Players"] = "Unknown"
r = requests.post(url, data=json.dumps(payload), headers=headers \
, auth=(user,password))
data = json.loads(r.content)
# Walks JSON response and grabs sys_id to insert into database
sysid = data["result"]["sys_id"]
number = data["result"]["number"]
print("Status Code: %d" % r.status_code)
print("Incident Num: %s" % number)
dbAdd(sysid,host,srvc)

# Resolve incident when alarm has cleared
Expand Down

0 comments on commit 34343c2

Please sign in to comment.