Skip to content

Instantly share code, notes, and snippets.

@phobos182
Created October 29, 2012 20:29
Show Gist options
  • Save phobos182/3976326 to your computer and use it in GitHub Desktop.
Save phobos182/3976326 to your computer and use it in GitHub Desktop.
Server alert api
#!/usr/bin/env python
# Sample
from jira.client import JIRA
from optparse import OptionParser
from pagerduty import *
import hashlib
import hipchat
import simplejson as json
import socket
import sys
# Constants
JIRA_SERVER = 'http://jira'
JIRA_USER = ''
JIRA_PASS = ''
HIPCHAT_TOKEN = ""
HIPCHAT_ROOM = 00000
PAGERDUTY_KEY = ""
HOSTNAME = socket.gethostname()
# Variables
issue_dict = {}
# Option Parser
parser = OptionParser()
parser.add_option("-t", "--type", dest="type", default="jira", help="Type of job (jira|pagerduty|hipchat)")
parser.add_option("-p", "--project", dest="project", default="OPS", help="Jira project (default: OPS)")
parser.add_option("-s", "--summary", dest="summary", default=None, help="Event summary (subject)")
parser.add_option("-c", "--component", dest="component", default=None, help="Jira component")
parser.add_option("-l", "--labels a,b,c", dest="labels", default=None, help="Jira labels")
parser.add_option("-d", "--description", dest="description", default=None, help="Event description (body)")
parser.add_option("-r", "--priority", dest="priority", default=None, help="Jira priority")
parser.add_option("-a", "--action", dest="action", default="trigger", help="PagerDuty Action: trigger / resolve (default: trigger)")
parser.add_option("-i", "--issue", dest="issue", default="Bug", help="Jira issue type (default: Bug)")
(options, args) = parser.parse_args()
#_ JIRA _#
if options.type == 'jira':
# Jira connection options
jira_options = {
"server": "%s" % (JIRA_SERVER)
}
# Test for null values
if options.summary is None or options.description is None:
print "Please specify a summary and / or description"
sys.exit(1)
# Connect to Jira
try:
jira = JIRA(jira_options, basic_auth=(JIRA_USER, JIRA_PASS))
except Exception, e:
print "Unable to connect to Jira: HTTP Code %s" % (e.status_code)
sys.exit(1)
# Required parameters
issue_dict['project'] = {}
issue_dict['project']['key'] = options.project
issue_dict['issuetype'] = {}
issue_dict['issuetype']['name'] = options.issue
issue_dict['summary'] = options.summary
issue_dict['description'] = options.description
# Optional params
if options.priority:
priority = None
if "p0" in options.priority:
priority = "1"
if "p1" in options.priority:
priority = "3"
if "p2" in options.priority:
priority = "4"
if "p3" in options.priority:
priority = "5"
if priority is not None:
issue_dict['priority'] = {}
issue_dict['priority']['id'] = priority
if options.component:
issue_dict['components'] = []
issue_dict['components'].append({ "name": "%s" % (options.component) })
if options.labels:
try:
l = options.labels.split(",")
issue_dict['labels'] = l
except Exception:
pass
# Submit Jira ticket
try:
new_issue = jira.create_issue(fields=issue_dict)
except Exception, e:
print "Could not create Jira ticket - %s" % (e)
#_ HIPCHAT _#
if options.type == 'hipchat':
chat = hipchat.HipChat(token=HIPCHAT_TOKEN)
if options.summary is None or options.description is None:
print "Must provide a summary and description"
sys.exit(1)
try:
chat.message_room(room_id=HIPCHAT_ROOM, message_from=HOSTNAME, message="%s - %s" % (options.summary, options.description), color="red")
except Exception, e:
print "Exception: %s, %s" % (e.errno, e.hdrs)
#_ PAGERDUTY _#
if options.type == 'pagerduty':
# PagerDuty will append 'incidents' under one incident key.
# Natural way of de-duplicating alerts per host.
# Just MD5 the hostname for key
incident_key = hashlib.md5(HOSTNAME).hexdigest()
if not "trigger" in options.action:
if not "resolve" in options.action:
print "Must specify trigger / resolve as an action"
sys.exit(1)
if options.summary is None or options.description is None:
print "Must specify summary and description"
sys.exit(1)
# Create PagerDuty object
pd = PagerDuty(PAGERDUTY_KEY)
try:
result = getattr(pd, options.action)(
description = options.summary,
incident_key = incident_key,
details = options.description
)
except PagerDutyException, e:
print "PagerDuty Exception - %s" % (e)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment