Re: API not functioning correctly
Posted: Tue May 16, 2017 12:11 pm
I have finally resolved this issue. The root cause was the MySQL db had strict mode enabled.
I found this out by digging through the code located in nagiosxi/html/api/includes/utils-api.inc.php and found a flaw.
In the function create_cfg_service() the import of the new service is never checked and if the new service import fails for any reason then the api still returns a successful message.
The '$myImportClass->fileImport' returns 0 if successful and 1 if it failed. In my case it kept returning 1 as a failed import because of a db issue, but the api never returned an error message of any kind, nor is it even checked in the code.
I would recommend that the above code at least be changed to something similar to:
Once I had discovered the '$myImportClass->strMessage' error messge I easily could determine that I had a database issue.
I found this out by digging through the code located in nagiosxi/html/api/includes/utils-api.inc.php and found a flaw.
In the function create_cfg_service() the import of the new service is never checked and if the new service import fails for any reason then the api still returns a successful message.
Code: Select all
...
// Commit the data to a file
$objs = array($obj);
$str = get_cfg_objects_str($objs, $firsthost);
$myImportClass->fileImport($str, 1, true);
if ($applyconfig) {
submit_command(COMMAND_NAGIOSCORE_APPLYCONFIG);
$msg = ' ' . _('Config applied, Nagios Core was restarted.');
} else {
$msg = ' ' . _('Config imported but not yet applied.');
}
return array('success' => _('Successfully added') . ' ' . $args['host_name'] . ' :: ' . $args['service_description'] . ' ' . _('to the system.') . $msg);
...
I would recommend that the above code at least be changed to something similar to:
Code: Select all
...
// Commit the data to a file
$objs = array($obj);
$str = get_cfg_objects_str($objs, $firsthost);
$importCode = $myImportClass->fileImport($str, 1, true);
if($importCode==0){
if ($applyconfig) {
submit_command(COMMAND_NAGIOSCORE_APPLYCONFIG);
$msg = ' ' . _('Config applied, Nagios Core was restarted.');
} else {
$msg = ' ' . _('Config imported but not yet applied.');
}
return array('success' => _('Successfully added') . ' ' . $args['host_name'] . ' :: ' . $args['service_description'] . ' ' . _('to the system.') . $msg);
} else {
$msg = $myImportClass->strMessage;
return array('error' => _('Import failed for') . ' ' . $args['host_name'] . ' :: ' . $args['service_description'] . ': ' . $msg);
}
...
Once I had discovered the '$myImportClass->strMessage' error messge I easily could determine that I had a database issue.