I have a requirement to put the schedule downtime using the rest API and the nagios version (5.4.7) I have got doesn't have this rest API available so I tried to create a custom API for that. I have created following php file. '/usr/local/nagiosxi/html/includes/components/nagiosxicustomendpoints/nagiosxicustomendpoints.inc.php '
Here is the section to register the custom api.
Code: Select all
register_custom_api_callback('downtime', 'hostgroup', 'nagiosxicustomendpoints_downtime_hostgroup');Code: Select all
function nagiosxicustomendpoints_downtime_hostgroup($endpoint, $verb, $args) {
$argslist = '';
$schParams = array();
$nagiosCmdFile = '/usr/local/nagios/var/rw/nagios.cmd';
if (isset($_GET['hostgroup_name'])) {
$schParams['hostgroup_name'] = $_GET['hostgroup_name'];
}
if (isset($_GET['start_time'])) {
$schParams['start_time'] = $_GET['start_time'];
}
if (isset($_GET['end_time'])) {
$schParams['end_time'] = $_GET['end_time'];
}
if (isset($_GET['fixed'])) {
$schParams['fixed'] = $_GET['fixed'];
}
if (isset($_GET['trigger_id'])) {
$schParams['trigger_id'] = $_GET['trigger_id'];
}
if (isset($_GET['duration'])) {
$schParams['duration'] = $_GET['duration'];
}
if (isset($_GET['author'])) {
$schParams['author'] = $_GET['author'];
}
if (isset($_GET['comment'])) {
$schParams['comment'] = $_GET['comment'];
}
foreach ($args as $key => $arg) {
$argslist .= "<arg><num>{$key}</num><data>{$arg}</data></arg>";
}
$xml = " <xml>
<endpoint>{$endpoint}</endpoint>
<verb>{$verb}</verb>
<hostgroup>{$schParams['hostgroup_name']},{$schParams['start_time']},{$schParams['end_time']},{$schParams['fixed']},{$schParams['trigger_id']},{$schParams['duration']},{$schParams['author']},{$schParams['comment']}</hostgroup>
<argslist>{$argslist}</argslist>
</xml>";
//echo file_put_contents($nagiosCmdFile, "$schParams['hostgroup_name'],$schParams['start_time'],$schParams['end_time'],$schParams['fixed'],$schParams['trigger_id'],$schParams['duration'],$schParams['author'],$schParams['comment']", FILE_APPEND);
$file = '/tmp/test_with_php.txt';
// Open the file to get existing content
//$current = file_get_contents($file);
echo "hi";
// Append a new person to the file
$current = "John Smith\n";
// Write the contents back to the file
$output = file_put_contents($file, $current);
echo "output is $output";
return xml2json::transformXmlStringToJson($xml);
}If i am trying with a test php file, its working and writing the file. But not when using curl to use it via rest API.
Code: Select all
<?php
$file = '/tmp/test_with_php.txt';
// Open the file to get existing content
#$current = file_get_contents($file);
echo "hi";
// Append a new person to the file
$current = "John Smith\n";
// Write the contents back to the file
echo file_put_contents($file, $current);
?>Can you please help me on this?