Help with Nagios XI · Nagios XI Custom API Endpoints ! This document is for developers who want to add API endpoints to their Nagios XI system. This document shows how, using a custom component. Custom Endpoints We're going to cover how to create custom endpoints for use in your Nagios XI instance. These will automatically become available to any user that has access to the API once they're registered. Creating the necessary code Development of Nagios XI components is outside of the scope of this document, so we'll be making the assumption that you have at least a basic working knowledge of creating and testing custom components. We'll use nagiosxicustomendpoints as a working name for our example component. Create a directory in /usr/local/nagiosxi /html/includes/components/ called nagiosxicustomendpoints mkdir /usr/local/nagiosxi/html/includes/components/nagiosxicustomendpoints Now we need to populate the directory with only one file: nagiosxicustomendpoints.inc.php nagiosxicustomendpoints.inc.php 1 de 3 08/09/2022 17:10 Help with Nagios XI · Nagios XI <?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 => "nagiosxicustomendpoints", COMPONENT_VERSION => "1.0", COMPONENT_AUTHOR => "Nagios Enterprises, LLC", COMPONENT_DESCRIPTION => "Demonstrate Nagios XI Custom API Endpoints", COMPONENT_TITLE => "Nagios XI Custom API Endpoints Example" ); // register with xi register_component("nagiosxicustomendpoints", $args); // register our custom api handler register_custom_api_callback('awesome', 'example', 'nagiosxicustomendpoints_awesome_examp } // the function to be called when we reach our custom endpoint via api function nagiosxicustomendpoints_awesome_example($endpoint, $verb, $args) { $argslist = ''; foreach ($args as $key => $arg) { $argslist .= "<arg><num>{$key}</num><data>{$arg}</data></arg>"; } $xml = " <xml> <endpoint>{$endpoint}</endpoint> <verb>{$verb}</verb> <argslist>{$argslist}</argslist> </xml>"; $data = simplexml_load_string($xml); return json_encode($data); } ?> Once you've created this file, you can use the Example cURL Request located below to check out your new Custom API Endpoint. Example cURL Request: 2 de 3 08/09/2022 17:10 Help with Nagios XI · Nagios XI curl -XGET "https://nagiosxi.XXXX.com/nagiosxi/api/v1/awesome/example/data1/dat a2?apikey=DBHWkXHMPTQMam3UrLYBPUTfIM2Ated6OTjp7Zbd9hYjXido3rYRFRnEToamFGMK&pret " (https://nagiosxi.XXXX.com/nagiosxi/api/v1/awesome/example/data1 ty=1" /data2?apikey=DBHWkXHMPTQMam3UrLYBPUTfIM2Ated6OTjp7Zbd9hYjXido3rYRFRnEToamFGMK&pretty=1) Response JSON: { "xml": { "endpoint": "awesome", "verb": "example", "argslist": { "arg": [ { "num": "0", "data": "data1" }, { "num": "1", "data": "data2" }, ] } } } Explanation and Notes The function register_custom_api_callback takes 3 arguments. They are, in order: the endpoint to specify, the verb to specify, and the function to call when this endpoint has been reached. The callback function (nagiosxicustomendpoints_awesome_example in our example) should be defined with 3 arguments as well. They are, in order: the endpoint that was called, the verb that was called, and the array of args appended after the endpoint. Keep in mind the following regarding custom endpoints: • You can override existing endpoints and verbs. • There is no automatic conversion to JSON when using Custom API Endpoints. Whatever is returned from your callback function will be printed as the API Response. • You can pull additional request variables via the Nagios XI functions like grab_request_var() or the standard PHP globals like $_GET and $_POST. 3 de 3 08/09/2022 17:10