Page 1 of 1

ApplyConfig feedback from API

Posted: Mon Jan 18, 2021 10:28 am
by IPOInS
When submitting either the parameter applyconfig to any existing API function or the specific applyconfig command itself all you ever get back as a response is that the command has been successfully submitted.
However, it doesn't tell you if the apply was actually successful and that no errors were found. To do this you have to check the status via the browser.

I've been looking at possible work arounds such as creating a custom API endpoint to retrieve the 'configuration snapshot' list (using the same call your PHP script uses via CCM "get_nagioscore_config_snapshots()"). This works fine, and allows me to see if the latest snapshot is an error.
This can also be useful prior to 'applyconfig' to make sure it's in a fit state before committing any changes.

Is there some other API call I can make to query the result of the applyconfig or could this be another feature request?
Being able to query the configuration snapshots as a standard API call would also be nice :)

Thanks.

Re: ApplyConfig feedback from API

Posted: Tue Jan 19, 2021 4:34 pm
by cdienger
What version of XI are you using? I tested on a 5.8.1 machine, and the apply configuration command will return command_id as well which you can then use to check its status. It does look like command_id is missing when applyconfig is used as a parameter so there may be room for improvement here.

With applyconfig command for example:

Code: Select all

curl -XPOST "https://xi_ip/nagiosxi/api/v1/system/applyconfig?apikey=<apikey>" -k 
I get:

Code: Select all

{"success":"Apply config command has been sent to the backend.","command_id":26}
and can look up command_id 26 with:

Code: Select all

curl -XGET "https://xi_ip/nagiosxi/api/v1/system/command/26?apikey=<apikey>&pretty=1" -k
To return:

Code: Select all

        "command_id": "26",
        "submitter_id": "1",
        "command": "17",
        "submission_time": "2021-01-19 16:20:49",
        "event_time": "2021-01-19 16:20:49",
        "processing_time": "2021-01-19 16:20:50",
        "status_code": "2",
        "result_code": "0",
        "result": "--------------------------------------"
status_code 2 mean it is completed and result_code 0 means it completed successfully.

The custom endpoint to check the snapshots sounds useful. If you can attach it to this thread I can submit it along with a feature request to our dev team.

Re: ApplyConfig feedback from API

Posted: Wed Jan 20, 2021 4:49 am
by IPOInS
D'oh! Good point. I've checked this and found that powershell was hiding the output of command_Id because of how it displays object output when two or more outputs have different column names (submit the change then apply in this case), I was only seeing the Success output.
I can see the command_Id output being returned and I'll certainly use that now for validating updates. Thanks.


Regarding the snapshot custom API, here's the content of the nagiosxicustomendpoints.inc.php file:

Code: Select all

<?php

// include the xi component helper
require_once(dirname(__FILE__) . '/../componenthelper.inc.php');

// define/call our component initialize function
nagiosxicustomendpoints_component_init();
function nagiosxicustomendpoints_component_init()
{
	// information to pass to xi about our component
	$args = array(
		COMPONENT_NAME =>           "nagiosxi_custom_endpoints",
		COMPONENT_VERSION =>        "1.0.0",
		COMPONENT_AUTHOR =>         "IPOInS",
		COMPONENT_DESCRIPTION =>    "Nagios XI Custom API Endpoints",
		COMPONENT_TITLE =>          "Nagios XI Custom API Endpoints"
	);

	// register with xi
	register_component("nagiosxi_custom_endpoints", $args);

	// register our custom api handler
	register_custom_api_callback('custom', 'configsnapshot', 'nagiosxicustomendpoints_custom_nagioscoresnapshot');
}

// the function to be called when we reach our custom endpoint via api
function nagiosxicustomendpoints_custom_nagioscoresnapshot() {
	$snapshots = get_nagioscore_config_snapshots();
	return json_encode($snapshots);
}
?>
The get_nagioscore_config_snapshots() function is what's used via the CCM link Configuration Snapshots:
https://<NAGIOSSERVER>/nagiosxi/admin/coreconfigsnapshots.php

Re: ApplyConfig feedback from API

Posted: Wed Jan 20, 2021 5:32 pm
by cdienger
Thanks! I've submitted a feature request for an official snapshot endpoint for the API.

Re: ApplyConfig feedback from API

Posted: Thu Jan 28, 2021 10:20 am
by IPOInS
one thing though. The output from the command doesn't indicate if there are any warnings reported in the config.

Correct me if I'm wrong, but these can only be found when viewing the output from the snapshot. It would be nice to get this in result_code or a new column name.

Re: ApplyConfig feedback from API

Posted: Fri Jan 29, 2021 10:13 am
by cdienger
I will add that to the feature request. Thanks for the input!