Advertisement
Guest User

nagios_to_jira script

a guest
Feb 7th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.73 KB | None | 0 0
  1. <?php
  2. /* nagios_to_jira.php - A Nagios notification scripts that creates issues within a JIRA instance in the event of a host or service issue.
  3. * This script is designed to be executed on Linux systems running Nagios Core (or one of its derivative products).
  4. * 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
  5. * 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.
  6. * 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
  7. * ony runs under Linux).
  8. *
  9. * Copyright (C) 2011, Jarrod Spiga
  10. *
  11. * 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
  12. * Software Foundation, either version 3 of the License, or (at your option) any later version.
  13. *
  14. * 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
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * 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/.
  18. */
  19.  
  20. define('VERSION', '1.2.0');
  21. /* Version History:
  22. * For more detail and changelog, see http://wiki.spiga.id.au/display/NTJ/Home
  23. * v1.0.0-RC1 Initial release that creates JIRA issues on PROBLEM notifications and creates comments in previously create issues on ACKNOWLEDGEMENT and
  24. * RECOVERY notifications. The script also logs messages to it's own log file in order to assist with troubleshooting (as it's only useful
  25. * when called by Nagios).
  26. * 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
  27. * whatever logging facility is configured within PHP.
  28. * v1.1.0 Added HTTPS compatibility (see http://jira.spiga.id.au/browse/NTJ-3).
  29. * v1.1.1 Resolved bug that prevented issues from being created within JIRA if a mandatory field was not provided.
  30. * v1.1.2 Resolved bug where custom fields were not correctly forwarded to JIRA.
  31. * v1.2.0 Added support for service/host specific priorities to be set within JIRA via customer variables in Nagios.
  32. *
  33. * TODO:
  34. * NTJ-1 Add MySQL support to NTJ.
  35. */
  36.  
  37. // Change the working directory to where the script is located.
  38. chdir(substr($argv[0], 0, "nagios_to_jira.php" - 19));
  39. // Include pre-requisite files
  40. require_once("nagios_to_jira_config.php");
  41. require_once("../COR/error_handling.php");
  42. require_once("../COR/parse_arguments.php");
  43.  
  44. // Process arguments passed to the script.
  45. $args = parse_arguments($argv, FALSE);
  46. // The only thing that we expect is a debug flag. If it's there, set $debug to TRUE.
  47. if (isset($args["debug"])) {
  48. $debug = TRUE;
  49. } else {
  50. $debug = FALSE;
  51. }
  52.  
  53. error_reporting(E_ALL);
  54.  
  55. function generate_description($type) {
  56. // Local function that generates the description string that appears in the comment section of the JIRA issue
  57. // 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
  58. // of the comment.
  59. switch(getenv("NAGIOS_NOTIFICATIONTYPE")) {
  60. case "PROBLEM":
  61. $description = "{color:#3b0b0b}*Nagios Problem Alert*{color}\n\n";
  62. break;
  63. case "RECOVERY":
  64. $description = "{color:#0b3b0b}*Nagios Recovery Alert*{color}\n\n";
  65. break;
  66. case "ACKNOWLEDGEMENT":
  67. $description = "{color:#0f5d94}*Nagios Acknowledgement*{color}\n\n";
  68. break;
  69. default:
  70. $description = "{color:#585858}*Unknown Alert*{color}\n\n";
  71. break;
  72. }
  73. // Add content to the description - all notifications should have the date and time Nagios sent the alert.
  74. $description .= "The following information was provided by Nagios:\n";
  75. $description .= "* Date & Time: " . getenv("NAGIOS_SHORTDATETIME") . "\n";
  76. if (getenv("NAGIOS_SERVICEDESC")) {
  77. // If there is a service description environment variable present, display service-related information.
  78. $description .= "* Status Information: " . getenv("NAGIOS_SERVICEOUTPUT") . "\n";
  79. $description .= "* Current Host State: " . getenv("NAGIOS_HOSTSTATE") . "\n";
  80. $description .= "* Current Service State: " . getenv("NAGIOS_SERVICESTATE") . "\n";
  81. } else {
  82. // If no service description environment variable is present, display host-related information.
  83. $description .= "* Status Information: " . getenv("NAGIOS_HOSTOUTPUT") . "\n";
  84. $description .= "* Current Host State: " . getenv("NAGIOS_HOSTSTATE") . "\n";
  85. }
  86. if (getenv("NAGIOS_NOTIFICATIONTYPE") == "ACKNOWLEDGEMENT") {
  87. // If this is an Acknowledgement notification, add details about the author and the acknowledgement comment.
  88. $description .= "* Notification Author: " . getenv("NAGIOS_NOTIFICATIONAUTHOR") . "\n";
  89. $description .= "* Notification Comment: " . getenv("NAGIOS_NOTIFICATIONCOMMENT") . "\n";
  90. }
  91. // All notifications should also have the host IP address details.
  92. $description .= "* Host Address: " . getenv("NAGIOS_HOSTADDRESS") . "\n";
  93. // Once the generation of the description is complete, resturn it.
  94. return $description;
  95. }
  96.  
  97. function generate_summary() {
  98. // Local function that generates the issue summary when an issue is created in JIRA.
  99. if (getenv("NAGIOS_SERVICEDESC")) {
  100. // If the service description environment variable is present, include the details of the service that has the problem in the summary.
  101. $summary = "NAGIOS: " . getenv("NAGIOS_SERVICEDESC") . " on " . getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_SERVICESTATE");
  102. } else {
  103. // If the service description environment variable is not present, details about the host is all that is required in the summary.
  104. $summary = "NAGIOS: " . getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_HOSTSTATE");
  105. }
  106. return $summary;
  107. }
  108.  
  109. function issue_comment($issue_id, $recovery) {
  110. global $svcissue;
  111. global $debug;
  112. // Local function that adds a comment to an issue that was previously created in JIRA by this script.
  113. if ($debug == TRUE) {
  114. trigger_error("Start of issue_comment function.", E_USER_NOTICE);
  115. }
  116.  
  117. // Initialise a cURL session, login to JIRA and retrieve the XSRF token.
  118. if ($debug == TRUE) {
  119. trigger_error("Initiating CURL session.", E_USER_NOTICE);
  120. }
  121. $curl = curl_init();
  122. // 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
  123. // 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
  124. // a host header in the request.
  125. $headers = array("Host: " . JIRA_HOST_HEADER, "X-Atlassian-Token: nocheck");
  126. // Set the URL that should be accessed in order to log on to JIRA.
  127. $url = JIRA_BASE . "/login.jsp?os_username=" . JIRA_USERNAME . "&os_password=" . JIRA_PASSWORD . "&os_cookie=true";
  128. // Set cURL options.
  129. curl_setopt($curl, CURLOPT_URL, $url);
  130. curl_setopt($curl, CURLOPT_PORT, JIRA_PORT);
  131. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  132. curl_setopt($curl, CURLOPT_POST, TRUE);
  133. curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/nag2jira_cookie");
  134. curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/nag2jira_cookiejar");
  135. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  136. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
  137. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  138. // Execute the cURL request.
  139. $login = curl_exec($curl);
  140. // Ensure that the cURL request was successful.
  141. if ($login === FALSE) {
  142. $errstr = "Unable to initialise cURL session when attempting to log in to JIRA! Aborting.";
  143. trigger_error($errstr, E_USER_ERROR);
  144. exit(1);
  145. }
  146. // Ensure that the login was successful.
  147. if (strpos($login, "Sorry, your username and password are incorrect - please try again.") !== FALSE) {
  148. $errstr = "Unable to log into JIRA. Please verify the user name and password. Aborting.";
  149. trigger_error($errstr, E_USER_ERROR);
  150. exit(1);
  151. }
  152. if ($debug == TRUE) {
  153. trigger_error("Successfully logged on to JIRA instance.", E_USER_NOTICE);
  154. }
  155.  
  156. // Set the URL that should be used to retrieve the XSRF token.
  157. $url = JIRA_BASE . "/secure/CreateIssue.jspa?pid=" . JIRA_PROJECT_ID . "&issuetype=" . JIRA_ISSUE_TYPE;
  158. // Set cURL options.
  159. curl_setopt($curl, CURLOPT_URL, $url);
  160. // Execute the cURL request.
  161. $xsrf_request = curl_exec($curl);
  162. // Ensure that the cURL request was successful.
  163. if ($xsrf_request === FALSE) {
  164. $errstr = "Unable to initialise cURL session when attempting to retrieve XSRF token! Aborting.";
  165. trigger_error($errstr, E_USER_ERROR);
  166. exit(1);
  167. }
  168. // Extract the XSRF token
  169. $xsrf_result = preg_match('/atlassian-token" content="([^"]+)"/ism', $xsrf_request, $xsrf_matches);
  170. // Ensure that a token was found
  171. if (sizeof($xsrf_matches) == 0) {
  172. $errstr = "Unable to determine XSRF token! Aborting.";
  173. trigger_error($errstr, E_USER_ERROR);
  174. exit(1);
  175. }
  176. $xsrf_token = $xsrf_matches[1];
  177. if ($debug == TRUE) {
  178. trigger_error("Successfully retrieved XSRF token.", E_USER_NOTICE);
  179. }
  180.  
  181. // Generate the comment description .
  182. $description = generate_description("comment");
  183. // Set the URL that should be accessed in order to create a new comment within JIRA.
  184. $url = JIRA_BASE . "/secure/AddComment.jspa?atl_token=" . $xsrf_token . "&id=" . $issue_id . "&comment=" . urlencode($description);
  185. if (COMMENT_FIELDS != "") {
  186. if ($debug == TRUE) {
  187. trigger_error("Adding ' &" . COMMENT_FIELDS . "' to issue comment creation URL.", E_USER_NOTICE);
  188. }
  189. $url .= "&" . COMMENT_FIELDS;
  190. }
  191. // Set cURL options.
  192. curl_setopt($curl, CURLOPT_URL, $url);
  193. // Execute the cURL request.
  194. $issue_comment = curl_exec($curl);
  195. // Ensure that the cURL request was successful.
  196. if ($issue_comment === FALSE) {
  197. $errstr = "Unable to initialise cURL session when attempting to add a comment to issue with ID " . $issue_id . ". Aborting.";
  198. trigger_error($errstr, E_USER_ERROR);
  199. exit(1);
  200. }
  201. if ($debug == TRUE) {
  202. trigger_error("Successfully added comment to issue.", E_USER_NOTICE);
  203. }
  204.  
  205. // 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.
  206. if ($recovery) {
  207. if ($debug == TRUE) {
  208. trigger_error("As this notification is a recovery notification, now removing data from the history file.", E_USER_NOTICE);
  209. }
  210.  
  211. // Obtain the problem ID generated by Nagios.
  212. if ($svcissue) {
  213. $problem_id = getenv("NAGIOS_SERVICEPROBLEMID");
  214. } else {
  215. $problem_id = getenv("NAGIOS_HOSTPROBLEMID");
  216. }
  217. // Open the history file and re-read its contents.
  218. $file_handle = fopen(HISTORY_FILE, "r+");
  219. if (!$file_handle) {
  220. // If the attempt to load the history file fails, log a warning.
  221. $errstr = "Could not open history file (" . HISTORY_FILE . ") for reading and writing. Unable to remove problem/issue ID pairing for this recovery.";
  222. trigger_error($errstr, E_USER_WARNING);
  223. exit(1);
  224. }
  225. $history_file = fread($file_handle, filesize(HISTORY_FILE));
  226. // Move the file pointer back to the beginning so that we may over-write the previous contents.
  227. rewind($file_handle);
  228. // Strip out the problem ID and issue ID that we no longer need to keep track of.
  229. $history_file = substr($history_file, 0, strpos($history_file, $problem_id . ","));
  230. $history_file = substr($history_file, strpos($history_file, "|") + 1);
  231. // Write the new history to the history file.
  232. $history_write = fwrite($file_handle, $history_file);
  233. if ($history_write === FALSE) {
  234. // If the attempt to write to the history file fails, log a warning.
  235. $errstr = "Could not write to history file (" . HISTORY_FILE . "). Unable to remove problem/issue ID pairing for this recovery.";
  236. trigger_error($errstr, E_USER_WARNING);
  237. fclose($file_handle);
  238. exit(1);
  239. }
  240. // Truncate the file so it contains only what was just written.
  241. ftruncate($file_handle, strlen($history_file));
  242. // Close the file handle and exit gracefully.
  243. fclose($file_handle);
  244. if ($debug == TRUE) {
  245. trigger_error("Successfully updated the history file.", E_USER_NOTICE);
  246. }
  247. exit(0);
  248. }
  249. exit(0);
  250.  
  251. }
  252.  
  253. function issue_create($history) {
  254. global $svcissue;
  255. global $debug;
  256. if ($debug == TRUE) {
  257. trigger_error("Start of issue_create function.", E_USER_NOTICE);
  258. }
  259.  
  260. // Local function that creates a new issue within the JIRA instance when a PROBLEM notification is received.
  261. // Initialise a cURL session, login to JIRA and retrieve the XSRF token.
  262. if ($debug == TRUE) {
  263. trigger_error("Initiating CURL session.", E_USER_NOTICE);
  264. }
  265. $curl = curl_init();
  266. // 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
  267. // 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
  268. // a host header in the request.
  269. $headers = array("Host: " . JIRA_HOST_HEADER, "X-Atlassian-Token: nocheck");
  270. // Set the URL that should be accessed in order to log on to JIRA.
  271. $url = JIRA_BASE . "/login.jsp?os_username=" . JIRA_USERNAME . "&os_password=" . JIRA_PASSWORD . "&os_cookie=true";
  272. // Set cURL options.
  273. curl_setopt($curl, CURLOPT_URL, $url);
  274. curl_setopt($curl, CURLOPT_PORT, JIRA_PORT);
  275. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  276. curl_setopt($curl, CURLOPT_POST, TRUE);
  277. curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/nag2jira_cookie");
  278. curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/nag2jira_cookiejar");
  279. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  280. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
  281. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  282. // Execute the cURL request.
  283. $login = curl_exec($curl);
  284. // Ensure that the cURL request was successful.
  285. if ($login === FALSE) {
  286. $errstr = "Unable to initialise cURL session when attempting to log in to JIRA! Aborting.";
  287. trigger_error($errstr, E_USER_ERROR);
  288. exit(1);
  289. }
  290. // Ensure that the login was successful.
  291. if (strpos($login, "Sorry, your username and password are incorrect - please try again.") !== FALSE) {
  292. $errstr = "Unable to log into JIRA. Please verify the user name and password. Aborting.";
  293. trigger_error($errstr, E_USER_ERROR);
  294. exit(1);
  295. }
  296. if ($debug == TRUE) {
  297. trigger_error("Successfully logged on to JIRA instance.", E_USER_NOTICE);
  298. }
  299.  
  300. // Set the URL that should be used to retrieve the XSRF token.
  301. $url = JIRA_BASE . "/secure/CreateIssue.jspa?pid=" . JIRA_PROJECT_ID . "&issuetype=" . JIRA_ISSUE_TYPE;
  302. // Set cURL options.
  303. curl_setopt($curl, CURLOPT_URL, $url);
  304. // Execute the cURL request.
  305. $xsrf_request = curl_exec($curl);
  306. // Ensure that the cURL request was successful.
  307. if ($xsrf_request === FALSE) {
  308. $errstr = "Unable to initialise cURL session when attempting to retrieve XSRF token! Aborting.";
  309. trigger_error($errstr, E_USER_ERROR);
  310. exit(1);
  311. }
  312. // Extract the XSRF token
  313. $xsrf_result = preg_match('/atlassian-token" content="([^"]+)"/ism', $xsrf_request, $xsrf_matches);
  314. // Ensure that a token was found
  315. if (sizeof($xsrf_matches) == 0) {
  316. $errstr = "Unable to determine XSRF token! Aborting.";
  317. trigger_error($errstr, E_USER_ERROR);
  318. exit(1);
  319. }
  320. $xsrf_token = $xsrf_matches[1];
  321. if ($debug == TRUE) {
  322. trigger_error("Successfully retrieved XSRF token.", E_USER_NOTICE);
  323. }
  324.  
  325. $summary = generate_summary();
  326. $description = generate_description("create");
  327. // Set the URL that should be accessed in order to create a new issue within JIRA.
  328. $url = JIRA_BASE . "/secure/CreateIssueDetails.jspa?atl_token=" . $xsrf_token . "&pid=" . JIRA_PROJECT_ID . "&issuetype=" . JIRA_ISSUE_TYPE . "&components=" . JIRA_COMPONENTS;
  329. $url .= "&summary=" . urlencode($summary) . "&description=" . urlencode($description);
  330.  
  331. if ($svcissue == TRUE) {
  332. if (getenv("NAGIOS__SERVICEJIRA_PRIORITY_ID") !== FALSE) {
  333. if ($debug == TRUE) {
  334. trigger_error("Using custom priority of '" . getenv("NAGIOS__SERVICEJIRA_PRIORITY_ID") . "' when creating issue.", E_USER_NOTICE);
  335. }
  336. $url .= "&priority=" . getenv("NAGIOS__SERVICEJIRA_PRIORITY_ID");
  337. } else {
  338. $url .= "&priority=2";
  339. }
  340. } else {
  341. if (getenv("NAGIOS__HOSTJIRA_PRIORITY_ID") !== FALSE) {
  342. if ($debug == TRUE) {
  343. trigger_error("Using custom priority of '" . getenv("NAGIOS__HOSTJIRA_PRIORITY_ID") . "' when creating issue.", E_USER_NOTICE);
  344. }
  345. $url .= "&priority=" . getenv("NAGIOS__HOSTJIRA_PRIORITY_ID");
  346. } else {
  347. $url .= "&priority=2";
  348. }
  349. }
  350.  
  351. if (ISSUE_FIELDS != "") {
  352. if ($debug == TRUE) {
  353. trigger_error("Adding '&" . ISSUE_FIELDS . "' to issue creation URL.", E_USER_NOTICE);
  354. }
  355. $url .= "&" . ISSUE_FIELDS;
  356. }
  357. // Set cURL options - including CURLOPT_HEADER:
  358. // Upon successful creation of an issue, JIRA redirects browsers to the URL of the newly created issue. Following the redirection causes issues under
  359. // some circumstances, so we'll manually follow the redirect.
  360. curl_setopt($curl, CURLOPT_URL, $url);
  361. curl_setopt($curl, CURLOPT_HEADER, TRUE);
  362. // Execute the cURL request.
  363. $issue_create = curl_exec($curl);
  364. // Ensure that the cURL request was successful.
  365. if ($issue_create === FALSE) {
  366. $errstr = "Unable to initialise cURL session when attempting to create an issue in JIRA! Aborting.";
  367. trigger_error($errstr, E_USER_ERROR);
  368. exit(1);
  369. }
  370. // Determine if a HTTP redirect was provided.
  371. if (strpos($issue_create, "HTTP/1.1 302 Moved Temporarily") === FALSE) {
  372. $errstr = "Failed to create an issue in JIRA! Errors listed by JIRA are: ";
  373. $jira_errors = substr_count($issue_create, "<div class=\"error\">");
  374. for ($i = 1; $i <= $jira_errors; $i++) {
  375. // Cut the first character off the string so that previous iterations of this loop aren't redetected.
  376. $issue_create = substr($issue_create, 1);
  377. // Cut everything off the string up until the first error div class.
  378. $issue_create = strstr($issue_create, "<div class=\"error\">");
  379. // The error sting that should be reported is contained within this div tag.
  380. $errstr .= substr($issue_create, 19, strpos($issue_create, "</div>") - 20);
  381. // If this loop reiterates, add a comment to the error string to ensure nice formatting.
  382. if ($i != $jira_errors) {
  383. $errstr .= ", ";
  384. }
  385. }
  386. $errstr .= ". Aborting.";
  387. trigger_error($errstr, E_USER_ERROR);
  388. exit(1);
  389. }
  390.  
  391. // Ensure that the redirect was supplied.
  392. $position = strpos($issue_create, "/browse/");
  393. if ($position === FALSE) {
  394. $errstr = "Failed to create issue in JIRA, or invalid redirect was supplied by JIRA! Aborting.";
  395. trigger_error($errstr, E_USER_ERROR);
  396. exit(1);
  397. }
  398.  
  399. if ($debug == TRUE) {
  400. trigger_error("Successfully created a new issue.", E_USER_NOTICE);
  401. }
  402. // Set the URL that should be used to load up the newly created issue so that we can determine what the issue ID is.
  403. $url = JIRA_BASE . substr($issue_create, $position);
  404. $url = substr($url, 0, strpos($url, "\n") - 1);
  405. // Set cURL options - including resetting CURLOPT_HEADER and CURLOPT_HTTPGET.
  406. curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
  407. curl_setopt($curl, CURLOPT_HEADER, FALSE);
  408. curl_setopt($curl, CURLOPT_URL, $url);
  409. // Execute the cURL request.
  410. $issue_display = curl_exec($curl);
  411. // Ensure that the cURL request was successful.
  412. if ($issue_display === FALSE) {
  413. $errstr = "Unable to initialise cURL session when attempting to load newly created JIRA issue! Unable to determine issue ID as a result.";
  414. trigger_error($errstr, E_USER_WARNING);
  415. exit(1);
  416. }
  417. if ($debug == TRUE) {
  418. trigger_error("Successfully received HTTP redirect upon creation of the new issue.", E_USER_NOTICE);
  419. }
  420.  
  421. // Ensure that we can obtain the issue ID from the created issue.
  422. $position = strpos($issue_display, "/secure/AddComment!default.jspa?id=");
  423. if ($position === FALSE) {
  424. $errstr = "Could not obtain issue ID, though the issue was created within JIRA.";
  425. trigger_error($errstr, E_USER_WARNING);
  426. exit(1);
  427. }
  428. $issue_id = substr($issue_display, $position + 35);
  429. $issue_id = substr($issue_id, 0, strpos($issue_id, "\""));
  430. // Append the problem ID and issue ID to the history file.
  431. /*if ($svcissue) {
  432. $history .= getenv("NAGIOS_SERVICEPROBLEMID");
  433.  
  434. } else {
  435. $history .= getenv("NAGIOS_HOSTPROBLEMID");
  436. }
  437. $history .= "," . $issue_id . "|";
  438. $file_handle = fopen(HISTORY_FILE, "w+");
  439. if (!$file_handle) {
  440. // If the attempt to open/create the history file fails, log a warning.
  441. $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.";
  442. exit(0);
  443. }
  444. $bytes = fwrite($file_handle, $history);
  445. fclose($file_handle);
  446. // If the attempt to write to the history file fails, log a warning.
  447. if ($bytes === FALSE) {
  448. $errstr = "Could not write to history file (" . HISTORY_FILE . "). Issue and Problem ID pairing could not be logged.";
  449. trigger_error($errstr, E_USER_WARNING);
  450. exit(0);
  451. }
  452. if ($debug == TRUE) {
  453. trigger_error("Successfully updated the history file.", E_USER_NOTICE);
  454. }
  455.  
  456. // If the history file is greater than 1MB, log a notice (it may need to be manually cleaned up).
  457. if ($bytes > 1048576) {
  458. $errstr = "History file is over 1MB in size. It is recommended to keep this file smaller than this threshold.";
  459. trigger_error($errstr, E_USER_NOTICE);
  460. }*/
  461. exit(0);
  462. }
  463.  
  464. function search_history($history, $problem_id) {
  465. // Determine if the problem ID appears in the history.
  466. $position = strpos($history, $problem_id . ",");
  467. if ($position === FALSE) {
  468. // If the probelm ID is not in the history, return a negative value.
  469. return(-1);
  470. } else {
  471. // If the problem ID was found, return the issue ID that corresponds to the problem ID.
  472. $issue_id = substr($history, $position + strlen($problem_id) + 1);
  473. $issue_id = substr($issue_id, 0, strpos($issue_id, "|"));
  474. return(intval($issue_id));
  475. }
  476. }
  477.  
  478. if ($debug === TRUE) {
  479. trigger_error("NTJ has been called.", E_USER_NOTICE);
  480. }
  481. // Determine if we have a host issue or a service issue.
  482. if (getenv("NAGIOS_SERVICEDESC")) {
  483. // We have a service issue.
  484. $summary = getenv("NAGIOS_SERVICEDESC") . " on " . getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_SERVICESTATE");
  485. if ($debug === TRUE) {
  486. trigger_error("Service issue detected: " . $summary . ".", E_USER_NOTICE);
  487. }
  488. $summary = "NAGIOS: " . $summary;
  489. $svcissue = TRUE;
  490. } else if (getenv("NAGIOS_HOSTNAME")) {
  491. // We have a host issue.
  492. $summary = getenv("NAGIOS_HOSTNAME") . " is " . getenv("NAGIOS_HOSTSTATE");
  493. if ($debug === TRUE) {
  494. trigger_error("Host issue detected: " . $summary . ".", E_USER_NOTICE);
  495. }
  496. $summary = "NAGIOS: " . $summary;
  497. } else {
  498. // Neither the NAGIOS_SERVICEDESC or NAGIOS_HOSTNAME environment variables were set - which indicates that this scripty wasn't called from Nagios
  499. $errstr = "Script was called, but there were no Nagios environment variables present. Script was most likely not called from Nagios. Aborting.";
  500. trigger_error($errstr, E_USER_ERROR);
  501. exit(1);
  502. }
  503.  
  504. if ($debug == TRUE) {
  505. trigger_error("Notification type is: " . getenv("NAGIOS_NOTIFICATIONTYPE") . ".", E_USER_NOTICE);
  506. }
  507.  
  508. // Determine what type of notification has come through.
  509. switch(getenv("NAGIOS_NOTIFICATIONTYPE")) {
  510. case "ACKNOWLEDGEMENT":
  511. // If a problem notification is received, we should check if an issue already exists with the same problem ID:
  512. // If it does, we add a comment to the existing issue.
  513. // If it does not, we create a new issue.
  514. issue_create("");
  515. // Check if the history file exists.
  516. /*if (!file_exists(HISTORY_FILE)) {
  517. // 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.
  518. $errstr = "History file (" . HISTORY_FILE . ") does not exist! Continuing with JIRA issue creation and will attempt to create a new history file.";
  519. trigger_error($errstr, E_USER_NOTICE);
  520. issue_create("");
  521. } else {
  522. if ($debug == TRUE) {
  523. trigger_error("History file found at: " . HISTORY_FILE . ".", E_USER_NOTICE);
  524. }
  525.  
  526. // If the history file does exist, check that it isn't empty.
  527. if (!filesize(HISTORY_FILE) > 0) {
  528. // If the history file is empty, create a JIRA issue.
  529. if ($debug == TRUE) {
  530. trigger_error("History file is empty. Proceeding to create a new issue.", E_USER_NOTICE);
  531. }
  532. issue_create("");
  533. }
  534. // Open the history file and load its contents.
  535. $file_handle = fopen(HISTORY_FILE, "r");
  536. if (!$file_handle) {
  537. // If the attempt to load the history file fails, log a notice and create a JIRA issue.
  538. $errstr = "Could not open history file (" . HISTORY_FILE . ") for reading. Continuing with JIRA issue creation.";
  539. trigger_error($errstr, E_USER_NOTICE);
  540. issue_create("");
  541. }
  542. $history_file = fread($file_handle, filesize(HISTORY_FILE));
  543. fclose($file_handle);
  544. if ($debug == TRUE) {
  545. trigger_error("Data from history file has been loaded and file is now closed.", E_USER_NOTICE);
  546. }
  547.  
  548. // Search through the history file to see there is already a JIRA issue for this problem ID.
  549. if ($svcissue) {
  550. $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_SERVICEPROBLEMID"))));
  551. } else {
  552. $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_HOSTPROBLEMID"))));
  553. }
  554. // $history_match should be negative (-1) if no match was found - otherwise, it should be the JIRA issue ID.
  555. if ($history_match < 0) {
  556. if ($debug == TRUE) {
  557. trigger_error("No match was found in the history file. Continuing with JIRA issue creation.", E_USER_NOTICE);
  558. }
  559. issue_create($history_file);
  560. } else {
  561. if ($debug == TRUE) {
  562. trigger_error("Historical match found (" . $history_match . "). Continuing with JIRA comment creation.", E_USER_NOTICE);
  563. }
  564. issue_comment(intval($history_match), FALSE);
  565. }
  566. }*/
  567. exit(0);
  568. break;
  569. case "RECOVERY":
  570. exit(0);
  571. case "PROBLEM":
  572. // 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
  573. // that issue. If no JIRA issue exists, we log the event and ignore the notification.
  574.  
  575. // Check if the history file exists.
  576. /*if (!file_exists(HISTORY_FILE)) {
  577. // 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).
  578. $errstr = "History file (" . HISTORY_FILE . ") does not exist! Assuming that there is no JIRA issue for this notification. Aborting.";
  579. trigger_error($errstr, E_USER_NOTICE);
  580. } else {
  581. if ($debug == TRUE) {
  582. trigger_error("History file found at: " . HISTORY_FILE . ".", E_USER_NOTICE);
  583. }
  584.  
  585. // If the history file does exist, check that it isn't empty.
  586. if (!filesize(HISTORY_FILE) > 0) {
  587. // If the history file is empty, log a notice and ignore the notification.
  588. $errstr = "History file (" . HISTORY_FILE . ") is empty. Assuming that there is no JIRA issue for this notification. Aborting.";
  589. trigger_error($errstr, E_USER_NOTICE);
  590. exit(0);
  591. }
  592. // Open the history file and load its contents.
  593. $file_handle = fopen(HISTORY_FILE, "r");
  594. if (!$file_handle) {
  595. // If the attempt to load the history file fails, log a notice and ignore the notification.
  596. $errstr = "Could not open history file (" . HISTORY_FILE . ") for reading. Assuming that there is no JIRA issue for this notification. Aborting.";
  597. trigger_error($errstr, E_USER_NOTICE);
  598. exit(0);
  599. }
  600. $history_file = fread($file_handle, filesize(HISTORY_FILE));
  601. fclose($file_handle);
  602. if ($debug == TRUE) {
  603. trigger_error("Data from history file has been loaded and file is now closed.", E_USER_NOTICE);
  604. }
  605.  
  606. // Search through the history file to see there is already a JIRA issue for this problem ID.
  607. if ($svcissue) {
  608. if (intval(getenv("NAGIOS_SERVICEPROBLEMID")) == 0) {
  609. $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_LASTSERVICEPROBLEMID"))));
  610. $recovery = TRUE;
  611. } else {
  612. $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_SERVICEPROBLEMID"))));
  613. $recovery = FALSE;
  614. }
  615. } else {
  616. if (intval(getenv("NAGIOS_HOSTPROBLEMID")) == 0) {
  617. $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_LASTHOSTPROBLEMID"))));
  618. $recovery = TRUE;
  619. } else {
  620. $history_match = intval(search_history($history_file, intval(getenv("NAGIOS_HOSTPROBLEMID"))));
  621. $recovery = FALSE;
  622. }
  623. }
  624.  
  625. // $history_match should be negative (-1) if no match was found - otherwise, it should be the JIRA issue ID.
  626. if ($history_match < 0) {
  627. $errstr = "Did not find a matching problem ID in the history file (" . HISTORY_FILE . "). Aborting.";
  628. trigger_error($errstr, E_USER_NOTICE);
  629. exit(0);
  630. } else {
  631. if ($debug == TRUE) {
  632. trigger_error("Historical match found (" . $history_match . "). Continuing with JIRA comment creation.", E_USER_NOTICE);
  633. if ($recovery === TRUE) {
  634. trigger_error("This notification is for a problem recovery.", E_USER_NOTICE);
  635. } else {
  636. trigger_error("This notification is NOT for a problem recovery.", E_USER_NOTICE);
  637. }
  638. }
  639.  
  640. issue_comment(intval($history_match), $recovery);
  641. }
  642. }*/
  643. exit(0);
  644. break;
  645. default:
  646. // If any other notification is received (flapping or downtime related), we log the event and ignore the notification.
  647. $errstr = "Received notification of type " . getenv("NAGIOS_NOTIFICATIONTYPE") . ". Aborting.";
  648. trigger_error($errstr, E_USER_NOTICE);
  649. exit(0);
  650. break;
  651. }
  652.  
  653. $errstr = "Unknown error!";
  654. trigger_error($errstr, E_USER_ERROR);
  655. exit(1);
  656.  
  657. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement