API not functioning correctly

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
daniel.ledford
Posts: 19
Joined: Wed Jan 20, 2016 9:03 am

Re: API not functioning correctly

Post by daniel.ledford »

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.

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);
        ...
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:

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.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: API not functioning correctly

Post by dwhitfield »

daniel.ledford wrote: I easily could determine that I had a database issue.
Do you need help with the database issue or are we ready to lock this up?
daniel.ledford
Posts: 19
Joined: Wed Jan 20, 2016 9:03 am

Re: API not functioning correctly

Post by daniel.ledford »

We can lock the thread. I have resolved the database issue.

Thanks for all of your help!
Locked