Skip to content

Commit

Permalink
Added rudimentary debugging to nrdp plugins (#27) -BH
Browse files Browse the repository at this point in the history
  • Loading branch information
hedenface committed Dec 20, 2017
1 parent 30fe5e2 commit f206988
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions server/config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 /////////

Expand Down
29 changes: 29 additions & 0 deletions server/includes/utils.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
21 changes: 18 additions & 3 deletions server/plugins/nagioscorecmd/nagioscorecmd.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 "<result>\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -28,6 +29,8 @@ function nagioscorepassivecheck_process_request($cbtype, $args)
default:
break;
}

_debug("nagioscorepassivecheck_process_request() had no registered callbacks, returning");
}


Expand All @@ -43,6 +46,12 @@ function nagioscorepassivecheck_submit_check_data()
print_r($request);
echo "<BR>";
}
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");
Expand All @@ -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);
}

Expand All @@ -70,12 +82,15 @@ function nagioscorepassivecheck_submit_check_data()
print_r($xml);
echo "<BR>";
}
_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);
}

Expand Down Expand Up @@ -116,6 +131,7 @@ function nagioscorepassivecheck_submit_check_data()
$total_checks++;
}

_debug('all nrdp checks have been written');
output_api_header();

echo "<result>\n";
Expand All @@ -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 //////
Expand All @@ -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);
}

Expand Down Expand Up @@ -171,13 +189,16 @@ 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");
}


// Writes the check output into the NDO database skipping Nagios Core
// 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'];
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit f206988

Please sign in to comment.