function say_hello() { output_api_header(); echo "<response>\n"; echo " <status>0</status>\n"; echo " <message>OK</message>\n"; echo " <product>" . get_product_name() . "</product>\n"; echo " <version>" . get_product_version() . "</version>\n"; echo "</response>\n"; exit; }
function handle_api_error($msg) { output_api_header(); echo "<result>\n"; echo " <status>-1</status>\n"; echo " <message>" . xmlentities($msg) . "</message>\n"; echo "</result>\n"; exit; }
function nagioscorecmd_submit_nagios_command($raw = false) { global $cfg; // If commands are disallowed in the config... if ($cfg["disable_external_commands"] === TRUE) { handle_api_error(ERROR_DISABLED_COMMAND); return; } $command = grab_request_var("command"); // make sure we have a command if (!have_value($command)) { handle_api_error(ERROR_NO_COMMAND); } // make sure we can write to external command file if (!isset($cfg["command_file"])) { handle_api_error(ERROR_NO_COMMAND_FILE); } if (!file_exists($cfg["command_file"])) { handle_api_error(ERROR_BAD_COMMAND_FILE); } if (!is_writeable($cfg["command_file"])) { handle_api_error(ERROR_COMMAND_FILE_OPEN_WRITE); } // open external command file if (($handle = @fopen($cfg["command_file"], "w+")) === false) { handle_api_error(ERROR_COMMAND_FILE_OPEN); } // get current time $ts = time(); // write the external command(s) $error = false; if (!is_array($command)) { if ($raw == false) { fwrite($handle, "[" . $ts . "] "); } $result = fwrite($handle, $command . "\n"); //echo "WROTE: ".$request["command"]."<BR>\n"; } else { foreach ($command as $cmd) { if ($raw == false) { fwrite($handle, "[" . $ts . "] "); } $result = fwrite($handle, $cmd . "\n"); //echo "WROTE: ".$cmd."<BR>\n"; if ($result === false) { break; } } } // close the file fclose($handle); if ($result === false) { handle_api_error(ERROR_BAD_WRITE); } output_api_header(); echo "<result>\n"; echo " <status>0</status>\n"; echo " <message>OK</message>\n"; echo "</result>\n"; exit; }
function nagioscorepassivecheck_submit_check_data() { global $cfg; global $request; $debug = false; if ($debug) { echo "REQUEST:<BR>"; print_r($request); echo "<BR>"; } // check results are passed as XML data $xmldata = grab_request_var("XMLDATA"); if ($debug) { echo "XMLDATA:<BR>"; print_r($xmldata); echo "<BR>"; } // make sure we have data if (!have_value($xmldata)) { handle_api_error(ERROR_NO_DATA); } // convert to xml $xml = @simplexml_load_string($xmldata); if (!$xml) { print_r(libxml_get_errors()); handle_api_error(ERROR_BAD_XML); } if ($debug) { echo "OUR XML:<BR>"; print_r($xml); echo "<BR>"; } // make sure we can write to check results dir if (!isset($cfg["check_results_dir"])) { handle_api_error(ERROR_NO_CHECK_RESULTS_DIR); } if (!file_exists($cfg["check_results_dir"])) { handle_api_error(ERROR_BAD_CHECK_RESULTS_DIR); } $total_checks = 0; // process each result foreach ($xml->checkresult as $cr) { // get check result type $type = "host"; foreach ($cr->attributes() as $var => $val) { if ($var == "type") { $type = strval($val); } } // common elements $hostname = strval($cr->hostname); $state = intval($cr->state); $output = strval($cr->output); $output = str_replace("\n", "\\n", $output); // service checks if ($type == "service") { $servicename = strval($cr->servicename); } ////// WRITE THE CHECK RESULT ////// // create a temp file to write to $tmpname = tempnam($cfg["check_results_dir"], "c"); $fh = fopen($tmpname, "w"); fprintf($fh, "### NRDP Check ###\n"); fprintf($fh, "start_time=%d.0\n", time()); fprintf($fh, "# Time: %s\n", date('r')); fprintf($fh, "host_name=%s\n", $hostname); if ($type == "service") { fprintf($fh, "service_description=%s\n", $servicename); } fprintf($fh, "check_type=1\n"); // 0 for active, 1 for passive fprintf($fh, "early_timeout=1\n"); fprintf($fh, "exited_ok=1\n"); fprintf($fh, "return_code=%d\n", $state); fprintf($fh, "output=%s\\n\n", $output); // close the file fclose($fh); // change ownership and perms chgrp($tmpname, $cfg["nagios_command_group"]); chmod($tmpname, 0770); // create an ok-to-go, so Nagios Core picks it up $fh = fopen($tmpname . ".ok", "w+"); fclose($fh); $total_checks++; } output_api_header(); echo "<result>\n"; echo " <status>0</status>\n"; echo " <message>OK</message>\n"; echo " <meta>\n"; echo " <output>" . $total_checks . " checks processed.</output>\n"; echo " </meta>\n"; echo "</result>\n"; exit; }