Created
November 23, 2018 19:47
-
-
Save neufeldtech/acec00e50a2bc0a87b60b212b6462dc8 to your computer and use it in GitHub Desktop.
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
// ==UserScript== | |
// @name Prometheus Alert To JIRA (Karma Dashboard) | |
// @version 1.1 | |
// @match http://karma.example.com:8080/* | |
// @grant GM_xmlhttpRequest | |
// @require https://code.jquery.com/jquery-3.3.1.min.js | |
// ==/UserScript== | |
const JIRA_URL = "https://jira.example.com"; | |
function postToSlack(jiraKey, alert, reporter){ | |
var reqBody = {}; | |
var annotation = ""; | |
alert.annotations.forEach(function(a) { | |
annotation = a.value; | |
}); | |
reqBody.message = "*Prometheus Alert:* " + alert.env + " - " + alert.name + " - " + annotation + "\nhttps://jira.example.com/browse/" + jiraKey + " created by *" + reporter + "*"; | |
reqBody.room = "#ops"; | |
GM_xmlhttpRequest({ | |
method: "POST", | |
url: "http://my-hubot.example.com:8001/hubot/say", | |
headers: { | |
"content-type": "application/json" | |
}, | |
data: JSON.stringify(reqBody), | |
onload: function(response) { | |
} | |
}); | |
} | |
function getJiraTicketReporter(ticketUrl, callback) { | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: ticketUrl, | |
onload: function(response) { | |
console.log(response.status); | |
if (response.status === 200){ | |
callback(JSON.parse(response.responseText).fields.reporter.name); | |
} | |
} | |
}); | |
} | |
function createJiraTicket(alert) { | |
var issueObject = {}; | |
issueObject.fields = {'project':{key:'OPS'},'summary':'','description':'','issuetype':{'name':'Incident'},'customfield_10266':{'id':'10124'}}; | |
issueObject.fields.description +="h2. Prometheus Alert\n"; | |
issueObject.fields.description +="http://karma.example.com:8080/?q=@state=active\n"; | |
issueObject.fields.description += `h2. Time\n`; | |
issueObject.fields.description += `{code}${alert.time}{code}\n`; | |
alert.annotations.forEach(function(annotation) { | |
issueObject.fields.description += `h2. ${annotation.type.replace(/^\w/, c => c.toUpperCase())}\n`; | |
issueObject.fields.description += `{code}${annotation.value}{code}\n`; | |
issueObject.fields.summary = alert.env.toUpperCase() + " - " + alert.name + " - " + annotation.value; | |
}); | |
issueObject.fields.description += "h2. Labels\n"; | |
alert.labels.forEach(function(label) { | |
issueObject.fields.description += `- ${label}\n`; | |
}); | |
GM_xmlhttpRequest({ | |
method: "POST", | |
url: JIRA_URL+"/rest/api/latest/issue/", | |
headers: { | |
"User-Agent": "Mozilla/5.0", | |
"Accept": "application/json", | |
"content-type": "application/json", | |
"cache-control": "no-cache", | |
}, | |
data: JSON.stringify(issueObject), | |
onload: function(response) { | |
if (response.status === 201){ | |
var json = JSON.parse(response.responseText); | |
var jiraKey = json.key; | |
var selfUrl = json.self; | |
console.log(jiraKey); | |
getJiraTicketReporter(selfUrl, function(reporter) { | |
postToSlack(jiraKey, alert, reporter); | |
}); | |
} else { | |
alert('Ticket could not be created. Are you logged into JIRA right now?'); | |
} | |
} | |
}); | |
} | |
function createAlertObject(incidentGroupDiv) { | |
var alertGroup = JSON.parse(incidentGroupDiv.attr('data-group-json')) | |
console.log(JSON.stringify(alertGroup)) | |
var alert = { | |
annotations:[] | |
}; | |
alert.name = alertGroup.labels.alertname | |
alert.env = alertGroup.labels.environment || 'unknown' | |
alert.time = new Date(alertGroup.alerts[0].startsAt).toLocaleString('en-US', { timeZone: 'America/Los_Angeles' }) + " PT"; | |
alert.annotations = alertGroup.alerts[0].annotations.map((i) => { return {type: i.name, value: i.value } } ) | |
alert.annotations.sort(function(a, b){return a.type == "summary" ? -1 : 1;}); | |
var labels = alertGroup.shared.labels; | |
alert.labels = []; | |
for (var label in labels) { | |
alert.labels.push(`${label}: ${labels[label]}`) | |
} | |
console.log(alert) | |
return alert; | |
} | |
function observeAlertChanges() { | |
var doesNotContainAddedNodes = function(mutation) { | |
return !mutation.addedNodes || mutation.addedNodes.length === 0; | |
}; | |
var observer = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
if (doesNotContainAddedNodes(mutation)) return; | |
for (var i = 0; i < mutation.addedNodes.length; i++) { | |
// do things to your newly added nodes here | |
var node = mutation.addedNodes[i]; | |
$('.card-body', node).append(`<span class="jira-button components-label badge text-nowrap text-truncate mw-100 components-label-bright components-label-with-hover" style="color: blue;" type="button">+ JIRA</div>`); | |
} | |
}); | |
}); | |
observer.observe($('#root').get(0), { | |
childList: true, subtree: true, attributes: false, characterData: false | |
}); | |
} | |
(function() { | |
'use strict'; | |
$(document).ready(function(){ | |
observeAlertChanges(); | |
}); | |
})(); | |
$(document).on('click','.jira-button', function (event) { | |
var button = $(event.target); | |
var alert = createAlertObject(button.closest(".components-grid-alertgrid-alertgroup")); | |
createJiraTicket(alert); | |
button.prop('disabled', true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment