/** * get the correct state data based on the api type * * Parameters: * $hostname - hostname of the nagios instance * $port - port the nagios api instance is listening on * $protocol - the protocol to use for the transport (http/s) * $api_type - the type of API to use (nagiosapi, livestatus, ...) * * Returns an array of [$state, $mapping, $curl_stats] */ static function fetch_state($hostname, $port, $protocol, $url, $api_type) { $nagios_api = NagdashHelpers::get_nagios_api_object($api_type, $hostname, $port, $protocol, $url); // TODO: fix up the API implementations so they return the same // formatted data. There is no real need to have this switch case here switch ($api_type) { case "livestatus": $ret = $nagios_api->getState(); $state = $ret["details"]; $curl_stats = $ret["curl_stats"]; $mapping = $nagios_api->getColumnMapping(); break; case "nagios-api": $ret = $nagios_api->getState(); if ($ret["errors"] == true) { $state = $ret["details"]; } else { $state = $ret["details"]["content"]; } $curl_stats = $ret["curl_stats"]; $mapping = $nagios_api->getColumnMapping(); break; } return [$state, $mapping, $curl_stats]; }
require_once '../phplib/NagiosApi.php'; require_once '../phplib/NagiosLivestatus.php'; require_once '../phplib/utils.php'; $supported_methods = ["ack", "downtime", "enable", "disable"]; if (!isset($_POST['nag_host'])) { echo "Are you calling this manually? This should be called by Nagdash only."; } else { $nagios_instance = $_POST['nag_host']; $action = $_POST['action']; $details = ["host" => $_POST['hostname'], "service" => $_POST['service'] ? $_POST['service'] : null, "author" => function_exists("nagdash_get_user") ? nagdash_get_user() : "Nagdash", "duration" => $_POST['duration'] ? $_POST['duration'] * 60 : null, "comment" => "{$action} from Nagdash"]; if (!in_array($action, $supported_methods)) { echo "Nagios-api does not support this action ({$action}) yet. "; } else { foreach ($nagios_hosts as $host) { if ($host['tag'] == $nagios_instance) { $nagios_api = NagdashHelpers::get_nagios_api_object($api_type, $host["hostname"], $host["port"], $host["protocol"], $host["url"]); } } switch ($action) { case "ack": $ret = $nagios_api->acknowledge($details); break; case "downtime": $ret = $nagios_api->setDowntime($details); break; case "enable": $ret = $nagios_api->enableNotifications($details); break; case "disable": $ret = $nagios_api->disableNotifications($details); break;