Forked from nicholas-a-clark/gist:e239dc6ec2db489dce19ac2318d9525c
Last active
August 30, 2019 00:39
-
-
Save thomaswilburn/76a1a519a76f3e8ef897271bbdb8e6e4 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
var request = require("request"); | |
var dateFormat = require("dateformat"); | |
var today = new Date(); | |
/* Format the date to be todays date in mm-dd-yyy format for later use */ | |
today = dateFormat(today, "mm-dd-yyyy"); | |
onCallDateFormat = dateFormat(today, "yyyy-mm-dd"); | |
var jiraUrl = ""; | |
var jiraSubTasks = [ | |
{ | |
jiraTicketName: | |
"Bacula - verify all backup sets were backed up less than 24 hours ago", | |
jiraTicketMessage: | |
"Verify that all backup sets in bacula were last backed up less than 24 hours ago. Also make sure every client has a successful full in the last month. \\n " | |
}, | |
{ | |
jiraTicketName: "Jenkins/Methode health check for ${today}", | |
jiraTicketMessage: | |
"Look at methode jenkins instance and address any discovered problems. \\n Status page is at: " | |
}, | |
{ | |
jiraTicketName: "Jenkins/cron health check for ${today}", | |
jiraTicketMessage: | |
"Look at cron jenkins instance and address any discovered problems. \\n Status page is at: " | |
}, | |
{ | |
jiraTicketName: "Logic Monitor health check for ${today}", | |
jiraTicketMessage: | |
"Look at current alert list in logic monitor and address any discovered problems. \\n Status page is at: " | |
}, | |
{ | |
jiraTicketName: "Imperva - verify normal operation", | |
jiraTicketMessage: | |
"Make sure we are not flushing too much traffic. \\n Status page is at: " | |
}, | |
{ | |
jiraTicketName: | |
"Methode volumes - check tier-netapp1-b volume size and purge snapshots", | |
jiraTicketMessage: | |
"Check volume size and purge snapshots (if needed).\\n Information on purging: " | |
} | |
]; | |
whoIsOncall().then(createJiraTicket).then(createJiraChildTickets); | |
function whoIsOncall() { | |
return new Promise(function(ok, fail) { | |
//Get who is onCall | |
var http = require("https"); | |
var options = { | |
method: "GET", | |
hostname: "api.opsgenie.com", | |
port: null, | |
path: | |
"/v2/schedules/ServerOps_schedule/on-calls?scheduleIdentifierType=name&flat=true&date=" + | |
onCallDateFormat + | |
"T19%3A01%3A00-04%3A00", | |
headers: { | |
"content-length": "0", | |
authorization: "GenieKey " | |
} | |
}; | |
var req = http.request(options, function(res) { | |
var chunks = []; | |
res.on("data", function(chunk) { | |
chunks.push(chunk); | |
}); | |
res.on("end", function() { | |
var body = Buffer.concat(chunks); | |
var jsonOuput = JSON.parse(body); | |
console.log( | |
"JSON output: " + | |
JSON.stringify(jsonOuput.data.onCallRecipients) | |
.split("@")[0] | |
.replace("[", "") | |
.replace('"', "") | |
); | |
//Split up response to be simply Firstname.LastName intead of full email address | |
console.log( | |
JSON.stringify(jsonOuput.data.onCallRecipients) | |
.split("@")[0] | |
.replace("[", "") | |
.replace('"', "") | |
); | |
ok( | |
JSON.stringify(jsonOuput.data.onCallRecipients) | |
.split("@")[0] | |
.replace("[", "") | |
.replace('"', "") | |
); | |
}); | |
}); | |
req.end(); | |
}) | |
} | |
function createJiraTicket(emailAddress) { | |
return new Promise(function(ok, fail) { | |
console.log("Email address" + emailAddress); | |
var jiraPost = { | |
fields: { | |
project: { | |
key: "OPSEC" | |
}, | |
assignee: { | |
name: emailAddress | |
}, | |
summary: "Night time time health check for " + today, | |
description: | |
"Main ticket for Operations night time on call daily health check tasks", | |
issuetype: { | |
name: "Alerts" | |
}, | |
priority: { | |
name: "Medium" | |
} | |
} | |
}; | |
request( | |
{ | |
url: jiraUrl, | |
method: "POST", | |
json: true, | |
body: jiraPost | |
}, | |
function(error, response, body) { | |
if (!error && response.statusCode === 204) { | |
console.log("OK"); | |
} else if (error) { | |
console.log(error); | |
} else { | |
console.log("JIRA id is " + response.body.key); | |
console.log(JSON.stringify(response, null, 2)); | |
ok(response.body.key); | |
} | |
} | |
); | |
}); | |
} | |
function createJiraChildTickets(parentJiraKey) { | |
// Create subticket Jira Sub Ticket tasks | |
// Loop over all the items in the JSON array for the sub-tickets needed | |
for (var i = 0, length = Object.keys(jiraSubTasks).length; i < length; i++) { | |
var jiraPostSubTicket = { | |
fields: { | |
project: { | |
key: "OPSEC" | |
}, | |
assignee: { | |
name: emailAddress | |
}, | |
parent: { | |
parent: parentJiraKey | |
}, | |
summary: jiraSubTasks.jiraSubTicket.SubTask.jiraTicketName, | |
description: jiraSubTasks.jiraSubTicket.SubTask.jiraTicketMessage, | |
issuetype: { | |
name: "Alerts" | |
}, | |
priority: { | |
name: "Medium" | |
} | |
} //end fields | |
}; //end jiraPostSubTicket function | |
request( | |
{ | |
url: jiraUrl, | |
method: "POST", | |
json: true, | |
body: jiraPostSubTicket, | |
auth: { user: "otto", pass: "0tt0" } | |
}, | |
function(error, response, body) { | |
if (!error && response.statusCode === 204) { | |
console.log("OK"); | |
} else if (error) { | |
console.log(error); | |
} else { | |
console.log("JIRA id is " + response.body.key); | |
console.log(JSON.stringify(response, null, 2)); | |
} | |
} | |
); | |
} //end for loop | |
} //end createJiraChildTickets() function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment