Skip to content

Commit

Permalink
working database functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeder committed Mar 1, 2015
1 parent b2ad7b3 commit 4b82893
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.json
*.swp
*.pyc
88 changes: 69 additions & 19 deletions ticketer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

# ticketer.py
# Shinken command script for generating ServiceNow Incidents via the REST API
import datetime
import getopt
import json
import sqlite3
import sys
import os
import requests

# Determine current working directory and set database path
cwd = os.path.dirname(os.path.realpath(__file__))
dbpath = cwd + '/data/incident.db'

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

def main(argv):
Expand All @@ -28,7 +30,8 @@ def main(argv):
print(err)
usage()
sys.exit(2)
if not args:
# If no options are given, print usage
if not opts:
usage()
# Handle all the options and arguments passed in
for opt, arg in opts:
Expand All @@ -51,10 +54,10 @@ def main(argv):
initdb()
# Generate new ticket if this is a new alarm
if alarm[0] == 'PROBLEM':
create(alarm)
createInc(alarm)
# Resolve existing ticket if the alarm has recovered
elif alarm[0] == 'RECOVERY':
resolve(alarm)
resolveInc(alarm)

# Print usage, even though this is called by a daemon?
def usage():
Expand All @@ -68,37 +71,84 @@ def usage():

# Setup sqlite database
def initdb():
print("INIT THAT DB YO!")
sql = '''
CREATE TABLE IF NOT EXISTS
data(id INTEGER PRIMARY KEY,
incident(id INTEGER PRIMARY KEY,
date DATETIME,
type TEXT,
sysid TEXT,
host TEXT,
addr TEXT,
res INT)'''
srvc TEXT)'''
db = sqlite3.connect(dbpath)
cursor = db.cursor()
cursor.execute(sql)
db.commit()
db.close()
print("Database successfully created.")

# Add new incident record to database
def dbAdd(ticketInfo):
print("Adding new record to DB %s" % ticketInfo)
def dbAdd(sysid,host,srvc):
date = datetime.datetime.now()
sql = '''
INSERT INTO incident(date, sysid, host, srvc)
VALUES(?,?,?,?)'''
values = [(date,sysid,host,srvc)]
db = sqlite3.connect(dbpath)
cursor = db.cursor()
cursor.executemany(sql,values)
db.commit()
db.close()
print('New record added to database.')

# 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)
db.text_factory = str
cursor = db.cursor()
values = [(host,srvc)]
print(values)
cursor.execute(sql,(host,srvc))
sysid = cursor.fetchone()
db.close()
return sysid

# Delete resolved incident record from database
def dbDel(ticketInfo):
print("Removing record from DB")
def dbDel(sysid):
sql = '''
DELETE FROM incident WHERE sysid = ?'''
db = sqlite3.connect(dbpath)
cursor = db.cursor()
try:
cursor.execute(sql,sysid)
db.commit()
db.close()
print("Record deleted")
except Exception as err:
print('\033[1;91mThere was an error while deleting the record\033[1;m')
print('\033[1;91m%s\033[1;m' % err)
pass

# Create new ticket in Service Now
def create(alarm):
def createInc(alarm):
print("Creating ticket for %s" % alarm)
ticketInfo = 'INC31337'
dbAdd(ticketInfo)
### Insert call to SN API here and grab sysid from response body
sysid = '2nk20912kl32u109312'
host = alarm[1]
srvc = alarm[3]
dbAdd(sysid,host,srvc)

# Resolve incident when alarm has cleared
def resolveInc(alarm):
host = alarm[1]
srvc = alarm[3]
sysid = dbLookup(host,srvc)
if not sysid:
print("No record found, nothing to resolve")
else:
print("Resolving ticket and removing entry for %s" % sysid)
dbDel(sysid)

# Delete record from database when alarm clears
def resolve(alarm):
print("Resolving ticket and removing entry for %s" % alarm[1:])
if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit 4b82893

Please sign in to comment.