Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* nagios_to_jira.php - A Nagios notification scripts that creates issues within a JIRA instance in the event of a host or service issue.
- * This script is designed to be executed on Linux systems running Nagios Core (or one of its derivative products).
- * This script requires PHP cURL functions, which are present in PHP v4.0.2 and later. It is recommended that you be using PHP v5.0.0 or later with this
- * script. Linux systems should have PHP compiled with the --with-curl[=DIR] argument, with DIR being the path to the libcurl 7.10.5 or later liraries.
- * This scripts is not supported in Windows environments (in any case, it makes most sense being installed on a host running Nagios Core, which itself
- * ony runs under Linux).
- *
- * Copyright (C) 2011, Jarrod Spiga
- *
- * This PHP code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
- * Software Foundation, either version 3 of the License, or (at your option) any later version.
- *
- * This PHP code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with this PHP code. If not, see http://www.gnu.org/licenses/.
- */
- define('VERSION', '1.2.0');
- /* Version History:
- * For more detail and changelog, see http://wiki.spiga.id.au/display/NTJ/Home
- * v1.0.0-RC1 Initial release that creates JIRA issues on PROBLEM notifications and creates comments in previously create issues on ACKNOWLEDGEMENT and
- * RECOVERY notifications. The script also logs messages to it's own log file in order to assist with troubleshooting (as it's only useful
- * when called by Nagios).
- * v1.0.0-RC2 Added debugging features. When the --debug argument is passed to the script, debugging information is logged to the internal log file and
- * whatever logging facility is configured within PHP.
- * v1.1.0 Added HTTPS compatibility (see http://jira.spiga.id.au/browse/NTJ-3).
- * v1.1.1 Resolved bug that prevented issues from being created within JIRA if a mandatory field was not provided.
- * v1.1.2 Resolved bug where custom fields were not correctly forwarded to JIRA.
- * v1.2.0 Added support for service/host specific priorities to be set within JIRA via customer variables in Nagios.
- *
- * TODO:
- * NTJ-1 Add MySQL support to NTJ.
- */
- // Change the working directory to where the script is located.
- chdir(substr($argv[0], 0, "nagios_to_jira.php" - 19));
- // Include pre-requisite files
- require_once("nagios_to_jira_config.php");
- require_once("../COR/error_handling.php");
- require_once("../COR/parse_arguments.php");
- // Process arguments passed to the script.
- $args = parse_arguments($argv, FALSE);
- // The only thing that we expect is a debug flag. If it's there, set $debug to TRUE.
- if (isset($args["debug"])) {
- $debug = TRUE;
- } else {
- $debug = FALSE;
- }
- error_reporting(E_ALL);
- function generate_description($type) {
- // Local function that generates the description string that appears in the comment section of the JIRA issue
- // The first line should indicate what type of notification was received. This line should be bold face and coloured so that it stands out from the rest
- // of the comment.
- switch(getenv("NAGIOS_NOTIFICATIONTYPE")) {
- case "PROBLEM":
- $description = "{color:#3b0b0b}*Nagios Problem Alert*{color}\n\n";
- break;
- case "RECOVERY":
- $description = "{color:#0b3b0b}*Nagios Recovery Alert*{color}\n\n";
- break;
- case "ACKNOWLEDGEMENT":
- $description = "{color:#0f5d94}*Nagios Acknowledgement*{color}\n\n";
- break;
- default:
- $description = "{color:#585858}*Unknown Alert*{color}\n\n";
- break;
- }
- // Add content to the description - all notifications should have the date and time Nagios sent the alert.
- $description .= "The following information was provided by Nagios:\n";
- $description .= "* Date & Time: " . getenv("NAGIOS_SHORTDATETIME") . "\n";
- if (getenv("NAGIOS_SERVICEDESC")) {
- // If there is a service description environment variable present, display service-related information.
- $description .= "* Status Information: " . getenv("NAGIOS_SERVICEOUTPUT") . "\n";
- $description .= "* Current Host State: " . getenv("NAGIOS_HOSTSTATE") . "\n";
- $description .= "* Current Service State: " . getenv("NAGIOS_SERVICESTATE") . "\n";
- } else {
- // If no service description environment variable is present, display host-related information.
- $description .= "* Status Information: " . getenv("NAGIOS_HOSTOUTPUT") . "\n";
- $description .= "* Current Host State: " . getenv("NAGIOS_HOSTSTATE") . "\n";
- }
- if (getenv("NAGIOS_NOTIFICATIONTYPE") == "ACKNOWLEDGEMENT") {
- // If this is an Acknowledgement notification, add details about the author and the acknowledgement comment.
- $description .= "* Notification Author: " . getenv("NAGIOS_NOTIFICATIONAUTHOR") . "\n";
- $description .= "* Notification Comment: " . getenv("NAGIOS_NOTIFICATIONCOMMENT") . "\n";
- }
- // All notifications should also have the host IP address details.
- $description .= "* Host Address: " . getenv("NAGIOS_HOSTADDRESS") . "\n";
- // Once the generation of the description is complete, resturn it.
- return $description;
- }
- function generate_summary() {
- // Local function that generates the issue summary when an issue is created in JIRA.
- if (getenv("NAGIOS_SERVICEDESC")) {
- // If the service description environment variable is present, include the details of the service that has the problem in the summary.
- $summary = "NAGIOS: " . getenv("NAGIOS_SERVICEDESC") . " on " . getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_SERVICESTATE");
- } else {
- // If the service description environment variable is not present, details about the host is all that is required in the summary.
- $summary = "NAGIOS: " . getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_HOSTSTATE");
- }
- return $summary;
- }
- function issue_comment($issue_id, $recovery) {
- global $svcissue;
- global $debug;
- // Local function that adds a comment to an issue that was previously created in JIRA by this script.
- if ($debug == TRUE) {
- trigger_error("Start of issue_comment function.", E_USER_NOTICE);
- }
- // Initialise a cURL session, login to JIRA and retrieve the XSRF token.
- if ($debug == TRUE) {
- trigger_error("Initiating CURL session.", E_USER_NOTICE);
- }
- $curl = curl_init();
- // In some cases, JIRA may be accessible to the monitoring server on a different IP address than what the base path resolves to (for example, if JIRA
- // resides on a server tha sits behind load balancers and the monitoring server sits in a local subnet). In order to support such configurations, define
- // a host header in the request.
- $headers = array("Host: " . JIRA_HOST_HEADER, "X-Atlassian-Token: nocheck");
- // Set the URL that should be accessed in order to log on to JIRA.
- $url = JIRA_BASE . "/login.jsp?os_username=" . JIRA_USERNAME . "&os_password=" . JIRA_PASSWORD . "&os_cookie=true";
- // Set cURL options.
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_PORT, JIRA_PORT);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/nag2jira_cookie");
- curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/nag2jira_cookiejar");
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- // Execute the cURL request.
- $login = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($login === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to log in to JIRA! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- // Ensure that the login was successful.
- if (strpos($login, "Sorry, your username and password are incorrect - please try again.") !== FALSE) {
- $errstr = "Unable to log into JIRA. Please verify the user name and password. Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- if ($debug == TRUE) {
- trigger_error("Successfully logged on to JIRA instance.", E_USER_NOTICE);
- }
- // Set the URL that should be used to retrieve the XSRF token.
- $url = JIRA_BASE . "/secure/CreateIssue.jspa?pid=" . JIRA_PROJECT_ID . "&issuetype=" . JIRA_ISSUE_TYPE;
- // Set cURL options.
- curl_setopt($curl, CURLOPT_URL, $url);
- // Execute the cURL request.
- $xsrf_request = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($xsrf_request === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to retrieve XSRF token! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- // Extract the XSRF token
- $xsrf_result = preg_match('/atlassian-token" content="([^"]+)"/ism', $xsrf_request, $xsrf_matches);
- // Ensure that a token was found
- if (sizeof($xsrf_matches) == 0) {
- $errstr = "Unable to determine XSRF token! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- $xsrf_token = $xsrf_matches[1];
- if ($debug == TRUE) {
- trigger_error("Successfully retrieved XSRF token.", E_USER_NOTICE);
- }
- // Generate the comment description .
- $description = generate_description("comment");
- // Set the URL that should be accessed in order to create a new comment within JIRA.
- $url = JIRA_BASE . "/secure/AddComment.jspa?atl_token=" . $xsrf_token . "&id=" . $issue_id . "&comment=" . urlencode($description);
- if (COMMENT_FIELDS != "") {
- if ($debug == TRUE) {
- trigger_error("Adding ' &" . COMMENT_FIELDS . "' to issue comment creation URL.", E_USER_NOTICE);
- }
- $url .= "&" . COMMENT_FIELDS;
- }
- // Set cURL options.
- curl_setopt($curl, CURLOPT_URL, $url);
- // Execute the cURL request.
- $issue_comment = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($issue_comment === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to add a comment to issue with ID " . $issue_id . ". Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- if ($debug == TRUE) {
- trigger_error("Successfully added comment to issue.", E_USER_NOTICE);
- }
- // If the $recovery variable is true, then the problem has been cleared in Nagios and the problem & issue ID pair can be removed from the history.
- if ($recovery) {
- if ($debug == TRUE) {
- trigger_error("As this notification is a recovery notification, now removing data from the history file.", E_USER_NOTICE);
- }
- // Obtain the problem ID generated by Nagios.
- if ($svcissue) {
- $problem_id = getenv("NAGIOS_SERVICEPROBLEMID");
- } else {
- $problem_id = getenv("NAGIOS_HOSTPROBLEMID");
- }
- // Open the history file and re-read its contents.
- $file_handle = fopen(HISTORY_FILE, "r+");
- if (!$file_handle) {
- // If the attempt to load the history file fails, log a warning.
- $errstr = "Could not open history file (" . HISTORY_FILE . ") for reading and writing. Unable to remove problem/issue ID pairing for this recovery.";
- trigger_error($errstr, E_USER_WARNING);
- exit(1);
- }
- $history_file = fread($file_handle, filesize(HISTORY_FILE));
- // Move the file pointer back to the beginning so that we may over-write the previous contents.
- rewind($file_handle);
- // Strip out the problem ID and issue ID that we no longer need to keep track of.
- $history_file = substr($history_file, 0, strpos($history_file, $problem_id . ","));
- $history_file = substr($history_file, strpos($history_file, "|") + 1);
- // Write the new history to the history file.
- $history_write = fwrite($file_handle, $history_file);
- if ($history_write === FALSE) {
- // If the attempt to write to the history file fails, log a warning.
- $errstr = "Could not write to history file (" . HISTORY_FILE . "). Unable to remove problem/issue ID pairing for this recovery.";
- trigger_error($errstr, E_USER_WARNING);
- fclose($file_handle);
- exit(1);
- }
- // Truncate the file so it contains only what was just written.
- ftruncate($file_handle, strlen($history_file));
- // Close the file handle and exit gracefully.
- fclose($file_handle);
- if ($debug == TRUE) {
- trigger_error("Successfully updated the history file.", E_USER_NOTICE);
- }
- exit(0);
- }
- exit(0);
- }
- function issue_create($history) {
- global $svcissue;
- global $debug;
- if ($debug == TRUE) {
- trigger_error("Start of issue_create function.", E_USER_NOTICE);
- }
- // Local function that creates a new issue within the JIRA instance when a PROBLEM notification is received.
- // Initialise a cURL session, login to JIRA and retrieve the XSRF token.
- if ($debug == TRUE) {
- trigger_error("Initiating CURL session.", E_USER_NOTICE);
- }
- $curl = curl_init();
- // In some cases, JIRA may be accessible to the monitoring server on a different IP address than what the base path resolves to (for example, if JIRA
- // resides on a server tha sits behind load balancers and the monitoring server sits in a local subnet). In order to support such configurations, define
- // a host header in the request.
- $headers = array("Host: " . JIRA_HOST_HEADER, "X-Atlassian-Token: nocheck");
- // Set the URL that should be accessed in order to log on to JIRA.
- $url = JIRA_BASE . "/login.jsp?os_username=" . JIRA_USERNAME . "&os_password=" . JIRA_PASSWORD . "&os_cookie=true";
- // Set cURL options.
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_PORT, JIRA_PORT);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/nag2jira_cookie");
- curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/nag2jira_cookiejar");
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- // Execute the cURL request.
- $login = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($login === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to log in to JIRA! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- // Ensure that the login was successful.
- if (strpos($login, "Sorry, your username and password are incorrect - please try again.") !== FALSE) {
- $errstr = "Unable to log into JIRA. Please verify the user name and password. Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- if ($debug == TRUE) {
- trigger_error("Successfully logged on to JIRA instance.", E_USER_NOTICE);
- }
- // Set the URL that should be used to retrieve the XSRF token.
- $url = JIRA_BASE . "/secure/CreateIssue.jspa?pid=" . JIRA_PROJECT_ID . "&issuetype=" . JIRA_ISSUE_TYPE;
- // Set cURL options.
- curl_setopt($curl, CURLOPT_URL, $url);
- // Execute the cURL request.
- $xsrf_request = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($xsrf_request === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to retrieve XSRF token! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- // Extract the XSRF token
- $xsrf_result = preg_match('/atlassian-token" content="([^"]+)"/ism', $xsrf_request, $xsrf_matches);
- // Ensure that a token was found
- if (sizeof($xsrf_matches) == 0) {
- $errstr = "Unable to determine XSRF token! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- $xsrf_token = $xsrf_matches[1];
- if ($debug == TRUE) {
- trigger_error("Successfully retrieved XSRF token.", E_USER_NOTICE);
- }
- $summary = generate_summary();
- $description = generate_description("create");
- // Set the URL that should be accessed in order to create a new issue within JIRA.
- $url = JIRA_BASE . "/secure/CreateIssueDetails.jspa?atl_token=" . $xsrf_token . "&pid=" . JIRA_PROJECT_ID . "&issuetype=" . JIRA_ISSUE_TYPE . "&components=" . JIRA_COMPONENTS;
- $url .= "&summary=" . urlencode($summary) . "&description=" . urlencode($description);
- if ($svcissue == TRUE) {
- if (getenv("NAGIOS__SERVICEJIRA_PRIORITY_ID") !== FALSE) {
- if ($debug == TRUE) {
- trigger_error("Using custom priority of '" . getenv("NAGIOS__SERVICEJIRA_PRIORITY_ID") . "' when creating issue.", E_USER_NOTICE);
- }
- $url .= "&priority=" . getenv("NAGIOS__SERVICEJIRA_PRIORITY_ID");
- } else {
- $url .= "&priority=2";
- }
- } else {
- if (getenv("NAGIOS__HOSTJIRA_PRIORITY_ID") !== FALSE) {
- if ($debug == TRUE) {
- trigger_error("Using custom priority of '" . getenv("NAGIOS__HOSTJIRA_PRIORITY_ID") . "' when creating issue.", E_USER_NOTICE);
- }
- $url .= "&priority=" . getenv("NAGIOS__HOSTJIRA_PRIORITY_ID");
- } else {
- $url .= "&priority=2";
- }
- }
- if (ISSUE_FIELDS != "") {
- if ($debug == TRUE) {
- trigger_error("Adding '&" . ISSUE_FIELDS . "' to issue creation URL.", E_USER_NOTICE);
- }
- $url .= "&" . ISSUE_FIELDS;
- }
- // Set cURL options - including CURLOPT_HEADER:
- // Upon successful creation of an issue, JIRA redirects browsers to the URL of the newly created issue. Following the redirection causes issues under
- // some circumstances, so we'll manually follow the redirect.
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, TRUE);
- // Execute the cURL request.
- $issue_create = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($issue_create === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to create an issue in JIRA! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- // Determine if a HTTP redirect was provided.
- if (strpos($issue_create, "HTTP/1.1 302 Moved Temporarily") === FALSE) {
- $errstr = "Failed to create an issue in JIRA! Errors listed by JIRA are: ";
- $jira_errors = substr_count($issue_create, "<div class=\"error\">");
- for ($i = 1; $i <= $jira_errors; $i++) {
- // Cut the first character off the string so that previous iterations of this loop aren't redetected.
- $issue_create = substr($issue_create, 1);
- // Cut everything off the string up until the first error div class.
- $issue_create = strstr($issue_create, "<div class=\"error\">");
- // The error sting that should be reported is contained within this div tag.
- $errstr .= substr($issue_create, 19, strpos($issue_create, "</div>") - 20);
- // If this loop reiterates, add a comment to the error string to ensure nice formatting.
- if ($i != $jira_errors) {
- $errstr .= ", ";
- }
- }
- $errstr .= ". Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- // Ensure that the redirect was supplied.
- $position = strpos($issue_create, "/browse/");
- if ($position === FALSE) {
- $errstr = "Failed to create issue in JIRA, or invalid redirect was supplied by JIRA! Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- if ($debug == TRUE) {
- trigger_error("Successfully created a new issue.", E_USER_NOTICE);
- }
- // Set the URL that should be used to load up the newly created issue so that we can determine what the issue ID is.
- $url = JIRA_BASE . substr($issue_create, $position);
- $url = substr($url, 0, strpos($url, "\n") - 1);
- // Set cURL options - including resetting CURLOPT_HEADER and CURLOPT_HTTPGET.
- curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
- curl_setopt($curl, CURLOPT_HEADER, FALSE);
- curl_setopt($curl, CURLOPT_URL, $url);
- // Execute the cURL request.
- $issue_display = curl_exec($curl);
- // Ensure that the cURL request was successful.
- if ($issue_display === FALSE) {
- $errstr = "Unable to initialise cURL session when attempting to load newly created JIRA issue! Unable to determine issue ID as a result.";
- trigger_error($errstr, E_USER_WARNING);
- exit(1);
- }
- if ($debug == TRUE) {
- trigger_error("Successfully received HTTP redirect upon creation of the new issue.", E_USER_NOTICE);
- }
- // Ensure that we can obtain the issue ID from the created issue.
- $position = strpos($issue_display, "/secure/AddComment!default.jspa?id=");
- if ($position === FALSE) {
- $errstr = "Could not obtain issue ID, though the issue was created within JIRA.";
- trigger_error($errstr, E_USER_WARNING);
- exit(1);
- }
- $issue_id = substr($issue_display, $position + 35);
- $issue_id = substr($issue_id, 0, strpos($issue_id, "\""));
- // Append the problem ID and issue ID to the history file.
- /*if ($svcissue) {
- $history .= getenv("NAGIOS_SERVICEPROBLEMID");
- } else {
- $history .= getenv("NAGIOS_HOSTPROBLEMID");
- }
- $history .= "," . $issue_id . "|";
- $file_handle = fopen(HISTORY_FILE, "w+");
- if (!$file_handle) {
- // If the attempt to open/create the history file fails, log a warning.
- $errstr = "Could not open history file (" . HISTORY_FILE . ") for writing or could not create history file. Issue and Problem ID pairing could not be logged.";
- exit(0);
- }
- $bytes = fwrite($file_handle, $history);
- fclose($file_handle);
- // If the attempt to write to the history file fails, log a warning.
- if ($bytes === FALSE) {
- $errstr = "Could not write to history file (" . HISTORY_FILE . "). Issue and Problem ID pairing could not be logged.";
- trigger_error($errstr, E_USER_WARNING);
- exit(0);
- }
- if ($debug == TRUE) {
- trigger_error("Successfully updated the history file.", E_USER_NOTICE);
- }
- // If the history file is greater than 1MB, log a notice (it may need to be manually cleaned up).
- if ($bytes > 1048576) {
- $errstr = "History file is over 1MB in size. It is recommended to keep this file smaller than this threshold.";
- trigger_error($errstr, E_USER_NOTICE);
- }*/
- exit(0);
- }
- function search_history($history, $problem_id) {
- // Determine if the problem ID appears in the history.
- $position = strpos($history, $problem_id . ",");
- if ($position === FALSE) {
- // If the probelm ID is not in the history, return a negative value.
- return(-1);
- } else {
- // If the problem ID was found, return the issue ID that corresponds to the problem ID.
- $issue_id = substr($history, $position + strlen($problem_id) + 1);
- $issue_id = substr($issue_id, 0, strpos($issue_id, "|"));
- return(intval($issue_id));
- }
- }
- if ($debug === TRUE) {
- trigger_error("NTJ has been called.", E_USER_NOTICE);
- }
- // Determine if we have a host issue or a service issue.
- if (getenv("NAGIOS_SERVICEDESC")) {
- // We have a service issue.
- $summary = getenv("NAGIOS_SERVICEDESC") . " on " . getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_SERVICESTATE");
- if ($debug === TRUE) {
- trigger_error("Service issue detected: " . $summary . ".", E_USER_NOTICE);
- }
- $summary = "NAGIOS: " . $summary;
- $svcissue = TRUE;
- } else if (getenv("NAGIOS_HOSTNAME")) {
- // We have a host issue.
- $summary = getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_HOSTSTATE");
- if ($debug === TRUE) {
- trigger_error("Host issue detected: " . $summary . ".", E_USER_NOTICE);
- }
- $summary = "NAGIOS: " . $summary;
- } else {
- // Neither the NAGIOS_SERVICEDESC or NAGIOS_HOSTNAME environment variables were set - which indicates that this scripty wasn't called from Nagios
- $errstr = "Script was called, but there were no Nagios environment variables present. Script was most likely not called from Nagios. Aborting.";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- }
- if ($debug == TRUE) {
- trigger_error("Notification type is: " . getenv("NAGIOS_NOTIFICATIONTYPE") . ".", E_USER_NOTICE);
- }
- // Determine what type of notification has come through.
- switch(getenv("NAGIOS_NOTIFICATIONTYPE")) {
- case "ACKNOWLEDGEMENT":
- // If a problem notification is received, we should check if an issue already exists with the same problem ID:
- // If it does, we add a comment to the existing issue.
- // If it does not, we create a new issue.
- issue_create("");
- // Check if the history file exists.
- /*if (!file_exists(HISTORY_FILE)) {
- // If the file doesn't exist, log a notice and create a JIRA issue. The issue_create function will attempt to create the history file.
- $errstr = "History file (" . HISTORY_FILE . ") does not exist! Continuing with JIRA issue creation and will attempt to create a new history file.";
- trigger_error($errstr, E_USER_NOTICE);
- issue_create("");
- } else {
- if ($debug == TRUE) {
- trigger_error("History file found at: " . HISTORY_FILE . ".", E_USER_NOTICE);
- }
- // If the history file does exist, check that it isn't empty.
- if (!filesize(HISTORY_FILE) > 0) {
- // If the history file is empty, create a JIRA issue.
- if ($debug == TRUE) {
- trigger_error("History file is empty. Proceeding to create a new issue.", E_USER_NOTICE);
- }
- issue_create("");
- }
- // Open the history file and load its contents.
- $file_handle = fopen(HISTORY_FILE, "r");
- if (!$file_handle) {
- // If the attempt to load the history file fails, log a notice and create a JIRA issue.
- $errstr = "Could not open history file (" . HISTORY_FILE . ") for reading. Continuing with JIRA issue creation.";
- trigger_error($errstr, E_USER_NOTICE);
- issue_create("");
- }
- $history_file = fread($file_handle, filesize(HISTORY_FILE));
- fclose($file_handle);
- if ($debug == TRUE) {
- trigger_error("Data from history file has been loaded and file is now closed.", E_USER_NOTICE);
- }
- // Search through the history file to see there is already a JIRA issue for this problem ID.
- if ($svcissue) {
- $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_SERVICEPROBLEMID"))));
- } else {
- $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_HOSTPROBLEMID"))));
- }
- // $history_match should be negative (-1) if no match was found - otherwise, it should be the JIRA issue ID.
- if ($history_match < 0) {
- if ($debug == TRUE) {
- trigger_error("No match was found in the history file. Continuing with JIRA issue creation.", E_USER_NOTICE);
- }
- issue_create($history_file);
- } else {
- if ($debug == TRUE) {
- trigger_error("Historical match found (" . $history_match . "). Continuing with JIRA comment creation.", E_USER_NOTICE);
- }
- issue_comment(intval($history_match), FALSE);
- }
- }*/
- exit(0);
- break;
- case "RECOVERY":
- exit(0);
- case "PROBLEM":
- // If a recovery or problem notification is received, we should check if an issue already exists with the same problem ID and add a comment to
- // that issue. If no JIRA issue exists, we log the event and ignore the notification.
- // Check if the history file exists.
- /*if (!file_exists(HISTORY_FILE)) {
- // If the file doesn't exist, log a notice and ignore the notification (as we assume that there is no JIRA issue for this notification).
- $errstr = "History file (" . HISTORY_FILE . ") does not exist! Assuming that there is no JIRA issue for this notification. Aborting.";
- trigger_error($errstr, E_USER_NOTICE);
- } else {
- if ($debug == TRUE) {
- trigger_error("History file found at: " . HISTORY_FILE . ".", E_USER_NOTICE);
- }
- // If the history file does exist, check that it isn't empty.
- if (!filesize(HISTORY_FILE) > 0) {
- // If the history file is empty, log a notice and ignore the notification.
- $errstr = "History file (" . HISTORY_FILE . ") is empty. Assuming that there is no JIRA issue for this notification. Aborting.";
- trigger_error($errstr, E_USER_NOTICE);
- exit(0);
- }
- // Open the history file and load its contents.
- $file_handle = fopen(HISTORY_FILE, "r");
- if (!$file_handle) {
- // If the attempt to load the history file fails, log a notice and ignore the notification.
- $errstr = "Could not open history file (" . HISTORY_FILE . ") for reading. Assuming that there is no JIRA issue for this notification. Aborting.";
- trigger_error($errstr, E_USER_NOTICE);
- exit(0);
- }
- $history_file = fread($file_handle, filesize(HISTORY_FILE));
- fclose($file_handle);
- if ($debug == TRUE) {
- trigger_error("Data from history file has been loaded and file is now closed.", E_USER_NOTICE);
- }
- // Search through the history file to see there is already a JIRA issue for this problem ID.
- if ($svcissue) {
- if (intval(getenv("NAGIOS_SERVICEPROBLEMID")) == 0) {
- $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_LASTSERVICEPROBLEMID"))));
- $recovery = TRUE;
- } else {
- $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_SERVICEPROBLEMID"))));
- $recovery = FALSE;
- }
- } else {
- if (intval(getenv("NAGIOS_HOSTPROBLEMID")) == 0) {
- $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_LASTHOSTPROBLEMID"))));
- $recovery = TRUE;
- } else {
- $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_HOSTPROBLEMID"))));
- $recovery = FALSE;
- }
- }
- // $history_match should be negative (-1) if no match was found - otherwise, it should be the JIRA issue ID.
- if ($history_match < 0) {
- $errstr = "Did not find a matching problem ID in the history file (" . HISTORY_FILE . "). Aborting.";
- trigger_error($errstr, E_USER_NOTICE);
- exit(0);
- } else {
- if ($debug == TRUE) {
- trigger_error("Historical match found (" . $history_match . "). Continuing with JIRA comment creation.", E_USER_NOTICE);
- if ($recovery === TRUE) {
- trigger_error("This notification is for a problem recovery.", E_USER_NOTICE);
- } else {
- trigger_error("This notification is NOT for a problem recovery.", E_USER_NOTICE);
- }
- }
- issue_comment(intval($history_match), $recovery);
- }
- }*/
- exit(0);
- break;
- default:
- // If any other notification is received (flapping or downtime related), we log the event and ignore the notification.
- $errstr = "Received notification of type " . getenv("NAGIOS_NOTIFICATIONTYPE") . ". Aborting.";
- trigger_error($errstr, E_USER_NOTICE);
- exit(0);
- break;
- }
- $errstr = "Unknown error!";
- trigger_error($errstr, E_USER_ERROR);
- exit(1);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement