OpsGenie to ServiceNow Automated Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""inc-create.py: OG2SNOW Incident Creation""" | |
# owned | |
__author__ = 'Rich Bocchinfuso' | |
__copyright__ = 'Copyright 2020, OG2SNOW Incident Creation' | |
__credits__ = ['Rich Bocchinfuso'] | |
__license__ = 'MIT' | |
__version__ = '0.1.0' | |
__maintainer__ = 'Rich Bocchinfuso' | |
__email__ = 'rbocchinfuso@gmail.com' | |
__status__ = 'BETA' | |
import os | |
import configparser | |
import pysnow | |
import json | |
import sys | |
import logging | |
from jsonpath_ng import jsonpath, parse | |
import opsgenie_sdk | |
from opsgenie_sdk.rest import ApiException | |
def map2snow(data): | |
custKey = parse('$.Company') | |
custVal = (custKey.find(data)[0].value) | |
logger.debug(custVal) | |
callerKey = parse('$.Caller') | |
callerVal = (callerKey.find(data)[0].value) | |
logger.debug(callerVal) | |
openedByKey = parse('$.OpenedBy') | |
openedByVal = (openedByKey.find(data)[0].value) | |
logger.debug(openedByVal) | |
# aeKey = parse('$.AE') | |
# aeVal = (custKey.find(data)[0].value) | |
# logger.debug(aeVal) | |
# userKey = parse('$.entity.source') | |
# userVal = (userKey.find(data)[0].value) | |
# logger.debug(userVal) | |
messageKey = parse('$.entity.message') | |
messageVal = (messageKey.find(data)[0].value) | |
logger.debug(messageVal) | |
descKey = parse('$.entity.description') | |
descVal = (descKey.find(data)[0].value) | |
logger.debug(descVal) | |
priKey = parse('$.entity.priority') | |
priVal = (priKey.find(data)[0].value) | |
logger.debug(priVal) | |
snowSev = (int(priVal[1:]) - 1) | |
logger.debug(snowSev) | |
typeKey = parse('$.entity.type') | |
typeVal = (typeKey.find(data)[0].value) | |
logger.debug(typeVal) | |
tagsKey = parse('$.entity.tags') | |
tagsVal = (tagsKey.find(data)[0].value) | |
logger.debug(tagsVal) | |
tinyKey = parse('$.entity.tinyId') | |
tinyVal = (tinyKey.find(data)[0].value) | |
logger.debug(tinyVal) | |
idKey = parse('$.entity.id') | |
idVal = (idKey.find(data)[0].value) | |
logger.debug(idVal) | |
now(custVal,callerVal,openedByVal,messageVal,descVal,snowSev) | |
def now(customer,caller,openedBy,message,description,sev): | |
# servicenow client connection | |
c = pysnow.Client(instance=(config['now_api']['instance']), user=(config['no | |
w_api']['user']), password=(config['now_api']['password'])) | |
# Define a resource, here we'll use the incident table API | |
incident = c.resource(api_path='/table/incident') | |
# Set the payload | |
new_record = { | |
'company': customer, | |
'u_customer': customer, | |
'caller_id': caller, | |
'opened_by': openedBy, | |
'short_description': message, | |
'description': description, | |
'impact': sev, | |
'urgency': sev | |
} | |
try: | |
# Create a new incident record | |
result = incident.create(payload=new_record) | |
except requests.exceptions.Timeout: | |
# Connection timeout | |
logger.debug('Connection timeout') | |
except requests.exceptions.TooManyRedirects: | |
# Bad URL | |
logger.debug('Bad URL') | |
except requests.exceptions.RequestException as e: | |
# Catastrophic error; bail | |
logger.debug('Catastrophic error') | |
raise SystemExit(e) | |
def closeOgAlert(data): | |
alertIdKey = parse('$.entity.id') | |
alertIdVal = (alertIdKey.find(data)[0].value) | |
logger.debug(alertIdVal) | |
configuration = opsgenie_sdk.Configuration() | |
# Configure API key authorization: GenieKey | |
configuration.api_key['Authorization'] = config['opsgenie_api']['auth'] | |
# create an instance of the API class | |
api_instance = opsgenie_sdk.AlertApi(opsgenie_sdk.ApiClient(configuration)) | |
identifier = alertIdVal # str | Identifier of alert which could be alert id, tiny id or alert alias | |
identifier_type = 'id' # str | Type of the identifier that is provided as an in-line parameter. Possible values are 'id', 'alias' or 'tiny' (optional) (d | |
efault to 'id') | |
close_alert_payload = opsgenie_sdk.CloseAlertPayload(note='Alert closed by OG2SNOW automation', source='python sdk') # CloseAlertPayload | Request payloa | |
d of closing alert action (optional) | |
try: | |
# Close Alert | |
api_response = api_instance.close_alert(identifier, identifier_type=identifier_type, close_alert_payload=close_alert_payload) | |
logger.debug(api_response) | |
except ApiException as e: | |
logger.debug("Exception when calling AlertApi->close_alert: %s\n" % e) | |
if __name__ == '__main__': | |
# read and parse config file | |
config = configparser.ConfigParser() | |
config.read(os.environ.get('OEC_ROOT_PATH') + '/conf/config.ini') | |
config.sections() | |
# logging | |
LOG = config['oec']['oec_root'] + config['oec']['debug_log'] | |
logging.basicConfig(filename=LOG, filemode="w", level=logging.DEBUG) | |
# console handler | |
console = logging.StreamHandler() | |
console.setLevel(logging.ERROR) | |
logging.getLogger("").addHandler(console) | |
logger = logging.getLogger(__name__) | |
logger.debug('hiho start debug logger') | |
data = json.loads(sys.argv[2]) | |
with open(config['oec']['oec_root'] + config['oec']['json_output'] , 'w') as f: | |
json.dump(data, f) | |
map2snow(data) | |
closeOgAlert(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment