From f206988ebd9a54dcac37b20761f12d7b0b21ea06 Mon Sep 17 00:00:00 2001 From: Bryan Heden Date: Wed, 20 Dec 2017 17:29:56 -0600 Subject: [PATCH] Added rudimentary debugging to nrdp plugins (#27) -BH --- CHANGES.rst | 6 +++- server/config.inc.php | 6 ++++ server/includes/utils.inc.php | 29 +++++++++++++++++++ .../nagioscorecmd/nagioscorecmd.inc.php | 21 ++++++++++++-- .../nagioscorepassivecheck.inc.php | 25 +++++++++++++++- 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 74fd9a9..33b54bf 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,4 +1,8 @@ -1.4.0 - 01/06/2017 +1.4.1 - +------------------ +- Added rudimentary debugging to nrdp plugins (#27) -BH + +1.4.0 - 01/06/2017 ------------------ - Added option to callback function for prepending instead of appending to callback array (added by tmcnag) -JO - Updated send_nrdp.sh to the latest revision -JO diff --git a/server/config.inc.php b/server/config.inc.php index 398fd66..46e6166 100644 --- a/server/config.inc.php +++ b/server/config.inc.php @@ -45,6 +45,12 @@ // Allows Nagios XI to send old check results directly into NDO if configured $cfg["allow_old_results"] = false; +// Enable debug logging +$cfg["debug"] = false; + +// Where should the logs go? +$cfg["debug_log"] = "/usr/local/nrdp/server/debug.log"; + ///////// DONT MODIFY ANYTHING BELOW THIS LINE ///////// diff --git a/server/includes/utils.inc.php b/server/includes/utils.inc.php index 155d13f..370c2f4 100644 --- a/server/includes/utils.inc.php +++ b/server/includes/utils.inc.php @@ -340,3 +340,32 @@ function register_callback($cbtype, $func, $prepend=null) { } } } + +if (!function_exists('_debug')) { + function _debug($data) { + + global $cfg; + + if (!is_string($data)) + return; + + $debug = grab_array_var($cfg, "debug", false); + if (!$debug) + return; + + $file = grab_array_var($cfg, "debug_file", "/usr/local/nrdp/server/debug.log"); + $date = '[' . date('r') . '] '; + $datepad = str_pad(' ', strlen($date)); + + $lines = explode("\n", $data); + + foreach ($lines as $i => $line) { + if ($i == 0) { + file_put_contents($file, "{$date}{$line}\n", FILE_APPEND); + } + else { + file_put_contents($file, "{$datepad}{$line}\n", FILE_APPEND); + } + } + } +} diff --git a/server/plugins/nagioscorecmd/nagioscorecmd.inc.php b/server/plugins/nagioscorecmd/nagioscorecmd.inc.php index d29f08c..ad16ac8 100644 --- a/server/plugins/nagioscorecmd/nagioscorecmd.inc.php +++ b/server/plugins/nagioscorecmd/nagioscorecmd.inc.php @@ -16,6 +16,7 @@ function nagioscorecmd_process_request($cbtype, $args) { $cmd = grab_array_var($args, "cmd"); + _debug("nagioscorecmd_process_request(cbtype = {$cbtype}, args[cmd] = {$cmd}"); switch ($cmd) { @@ -33,15 +34,19 @@ function nagioscorecmd_process_request($cbtype, $args) default: break; } + + _debug("nagioscorecmd_process_request() had no registered callbacks, returning"); } function nagioscorecmd_submit_nagios_command($raw=false) { global $cfg; + _debug("nagioscorecmd_submit_nagios_command(raw=" . ($raw ? 'TRUE' : 'FALSE')); // If commands are disallowed in the config... if ($cfg["disable_external_commands"] === true) { + _debug('cfg[disable_external_commands] == true, bailing'); handle_api_error(ERROR_DISABLED_COMMAND); return; } @@ -50,19 +55,27 @@ function nagioscorecmd_submit_nagios_command($raw=false) // Make sure we have a command if (!have_value($command)) { + _debug('we have no command, bailing'); handle_api_error(ERROR_NO_COMMAND); } // Make sure we can write to external command file - if (!isset($cfg["command_file"])) + if (!isset($cfg["command_file"])) { + _debug('we have no cfg[command_file], bailing'); handle_api_error(ERROR_NO_COMMAND_FILE); - if (!file_exists($cfg["command_file"])) + } + if (!file_exists($cfg["command_file"])) { + _debug("cfg[command_file] ({$cfg['command_file']}) doesn't exist, bailing"); handle_api_error(ERROR_BAD_COMMAND_FILE); - if (!is_writeable($cfg["command_file"])) + } + if (!is_writeable($cfg["command_file"])) { + _debug("cfg[command_file] ({$cfg['command_file']}) isn't writable, bailing"); handle_api_error(ERROR_COMMAND_FILE_OPEN_WRITE); + } // Open external command file if (($handle = @fopen($cfg["command_file"],"w+")) === false) { + _debug("couldn't open cfg[command_file] ({$cfg['command_file']}), bailing"); handle_api_error(ERROR_COMMAND_FILE_OPEN); } @@ -92,9 +105,11 @@ function nagioscorecmd_submit_nagios_command($raw=false) fclose($handle); if ($result === false) { + _debug("fwrite() result was false, bailing"); handle_api_error(ERROR_BAD_WRITE); } + _debug("fwrite() was successful!"); output_api_header(); echo "\n"; diff --git a/server/plugins/nagioscorepassivecheck/nagioscorepassivecheck.inc.php b/server/plugins/nagioscorepassivecheck/nagioscorepassivecheck.inc.php index 3168f15..1bc51dc 100644 --- a/server/plugins/nagioscorepassivecheck/nagioscorepassivecheck.inc.php +++ b/server/plugins/nagioscorepassivecheck/nagioscorepassivecheck.inc.php @@ -16,6 +16,7 @@ function nagioscorepassivecheck_process_request($cbtype, $args) { $cmd = grab_array_var($args, "cmd"); + _debug("nagioscorepassivecheck_process_request(cbtype = {$cbtype}, args[cmd] = {$cmd}"); switch ($cmd) { @@ -28,6 +29,8 @@ function nagioscorepassivecheck_process_request($cbtype, $args) default: break; } + + _debug("nagioscorepassivecheck_process_request() had no registered callbacks, returning"); } @@ -43,6 +46,12 @@ function nagioscorepassivecheck_submit_check_data() print_r($request); echo "
"; } + foreach($request as $index => $req) { + if (is_array($req)) { + $req = print_r($req, true); + } + _debug("REQUEST: [{$index}] {$req}"); + } // Check results are passed as XML data $xmldata = grab_request_var("XMLDATA"); @@ -55,13 +64,16 @@ function nagioscorepassivecheck_submit_check_data() // Make sure we have data if (!have_value($xmldata)) { + _debug("no xmldata, bailing"); handle_api_error(ERROR_NO_DATA); } // Convert to xml $xml = @simplexml_load_string($xmldata); if (!$xml) { - print_r(libxml_get_errors()); + $xmlerr = print_r(libxml_get_errors(), true); + _debug("conversion to xml failed: {$xmlerr}"); + echo $xmlerr; handle_api_error(ERROR_BAD_XML); } @@ -70,12 +82,15 @@ function nagioscorepassivecheck_submit_check_data() print_r($xml); echo "
"; } + _debug("our xml: " . print_r($xml, true)); // Make sure we can write to check results dir if (!isset($cfg["check_results_dir"])) { + _debug('we have no cfg[check_results_dir], bailing'); handle_api_error(ERROR_NO_CHECK_RESULTS_DIR); } if (!file_exists($cfg["check_results_dir"])) { + _debug("cfg[check_results_dir] ({$cfg['check_results_dir']}) doesn't exist, bailing"); handle_api_error(ERROR_BAD_CHECK_RESULTS_DIR); } @@ -116,6 +131,7 @@ function nagioscorepassivecheck_submit_check_data() $total_checks++; } + _debug('all nrdp checks have been written'); output_api_header(); echo "\n"; @@ -133,6 +149,7 @@ function nagioscorepassivecheck_submit_check_data() // Write out the check result to Nagios Core function nrdp_write_check_output_to_cmd($hostname, $servicename, $state, $output, $type) { + _debug("nrdp_write_check_output_to_cmd(hostname={$hostname}, servicename={$servicename}, state={$state}, type={$type}, output={$output}"); global $cfg; ////// WRITE THE CHECK RESULT ////// @@ -143,6 +160,7 @@ function nrdp_write_check_output_to_cmd($hostname, $servicename, $state, $output // Check if the file is in the check_results_dir if (strpos($tmpname, $cfg["check_results_dir"]) === false) { unlink($tmpname); + _debug("tmpname({$tmpname}) not in cfg[check_results_dir] ({$cfg['check_results_dir']}), bailing"); handle_api_error(ERROR_BAD_CHECK_RESULTS_DIR); } @@ -171,6 +189,7 @@ function nrdp_write_check_output_to_cmd($hostname, $servicename, $state, $output // Create an ok-to-go, so Nagios Core picks it up $fh = fopen($tmpname.".ok", "w+"); fclose($fh); + _debug("nrdp_write_check_output_to_cmd() successful"); } @@ -178,6 +197,8 @@ function nrdp_write_check_output_to_cmd($hostname, $servicename, $state, $output // so that we can input old (past checks) data into the database function nrdp_write_check_output_to_ndo($hostname, $servicename, $state, $output, $type, $time) { + _debug("nrdp_write_check_output_to_ndo(hostname={$hostname}, servicename={$servicename}, state={$state}, type={$type}, output={$output}"); + // Connect to the NDOutils database with Nagios XI config options require("/usr/local/nagiosxi/html/config.inc.php"); $ndodb = $cfg['db_info']['ndoutils']; @@ -187,6 +208,7 @@ function nrdp_write_check_output_to_ndo($hostname, $servicename, $state, $output $db = new MySQLi($ndodb['dbserver'], $ndodb['user'], $ndodb['pwd'], $ndodb['db']); if ($db->connect_errno) { + _debug("Coudln't connect to database, bailing"); return false; } @@ -488,6 +510,7 @@ function nrdp_write_check_output_to_ndo($hostname, $servicename, $state, $output } $db->close(); + _debug("nrdp_write_check_output_to_ndo() successful"); return; }