diff --git a/doc/9-icinga2-api.md b/doc/9-icinga2-api.md index a4dfcaa9d..3a31443bf 100644 --- a/doc/9-icinga2-api.md +++ b/doc/9-icinga2-api.md @@ -235,85 +235,179 @@ The Icinga 2 API provides multiple url endpoints: Please check the respective sections for detailed urls and parameters. - ## Actions -There are several actions available for Icinga 2 provided by the `actions` url endpoint. - +There are several actions available for Icinga 2 provided by the `actions` +URL endpoint. In case you have been using the [external commands](5-advanced-topics.md#external-commands) -in the past, the API actions provide a yet more powerful interface with -filters and even more functionality. +in the past, the API actions provide a similar interface with filter +capabilities for some of the more common targets which do not directly change +the configuration. +Some actions require specific target types (e.g. `type=Host`) and a +[filter expression](9-icinga2-api.md#icinga2-api-filters). +For each object matching the filter the action in question is performed once. -Actions require specific target types (e.g. `type=Host`) and a [filter expression](9-icinga2-api.md#icinga2-api-filters). +In the following each the actions are listed with their parameters, targets and +examples. The calls are first shown with all their possible query parameters and +their type. Optional parameters are encapsulated by `()` and `[]` mark array +parameters. If an optional parameter has no default value explicitly stated it +is either 0, NULL, Empty depending on the type. Timestamps are always `time_t`, +the seconds since the UNIX epoch. -**TODO** Figure out the final names. +### Process Check Result Action -Action name | Parameters | Target types | Notes ----------------------------------------|-----------------------------------|--------------------------|----------------------- -process-check-result | exit_status; plugin_output; check_source; performance_data[]; check_command[]; execution_end; execution_start; schedule_end; schedule_start | Service; Host | - -reschedule-check | {next_check}; {(force_check)} | Service; Host | - -acknowledge-problem | author; comment; {timestamp}; {(sticky)}; {(notify)} | Service; Host | - -remove-acknowledgement | - | Service; Host | - -add-comment | author; comment | Service; Host | - -remove-comment | - | Service;Host | - -remove-comment-by-id | comment_id | - | - -delay-notifications | timestamp | Service;Host | - -add-downtime | start_time; end_time; duration; author; comment; {trigger_id}; {(fixed)} | Service; Host; ServiceGroup; HostGroup | Downtime for all services on host x? -remove-downtime | - | Service; Host | - -remove-downtime-by-id | downtime_id | - | - -send-custom-notification | options[]; author; comment | Service; Host | - + /v1/actions/process-check-result?exit_status=int&(plugin_output=string)&(performance_data[]=string)&\ + (check_command[]=string)&(check_source=string)&(execution_end=time_t)&(execution_start=time_t)&\ + (schedule_end=time_t)&(schedule_start=time_t) -enable-passive-checks | - | Service; Host; ServiceGroup; HostGroup | "System" as target? -disable-passive-checks | - | Service; Host; ServiceGroup; HostGroup | diable all passive checks for services of hosts y in hostgroup x? -enable-active-checks | - | Host; HostGroup | - -disable-active-checks | - | Host; HostGroup | - -enable-notifications | - | Service; Host; ServiceGroup; HostGroup | Enable all notifications for services of host x? -disable-notifications | - | Service; Host; ServiceGroup; HostGroup | - -enable-flap-detection | - | Service; Host; ServiceGroup; HostGroup | - -disable-flap-detection | - | Service; Host; ServiceGroup; HostGroup | - -enable-event-handler | - | Service; Host | - -disable-event-handler | - | Service; Host | - +Target: `Service` or `Host` -enable-global-notifications | - | - | - -disable-global-notifications | - | - | - -enable-global-flap-detection | - | - | - -disable-global-flap-detection | - | - | - -enable-global-event-handlers | - | - | - -disable-global-event-handlers | - | - | - -enable-global-performance-data | - | - | - -disable-global-performance-data | - | - | - -start-global-executing-svc-checks | - | - | - -stop-global-executing-svc-checks | - | - | - -start-global-executing-host-checks | - | - | - -stop-global-executing-host-checks | - | - | - -shutdown-process | - | - | - -restart-process | - | - | - +This is used to submit a passive check result for a service or host. Passive +checks need to be enabled for the check result to be processed. +The `exit_status` field should be one of the following for services: +0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN or for hosts: 0=OK, 1=CRITICAL. +The `plugin_output` field contains text output from the service check, the +performance data is submitted via `performance_data` as one array entry per ';' +separated block. +Example: -Examples: + $ curl -u root:icinga -k -s 'https://localhost:5665/v1/actions/process-check-result?filter=service.name=="ping6"' -Reschedule a service check for all services in NOT-OK state: +### reschedule-check - $ curl -u root:icinga -k -s 'https://localhost:5665/v1/actions/reschedule-check?filter=service.state!=0&type=Service' -X POST | python -m json.tool - { - "results": [ - { - "code": 200.0, - "status": "Successfully rescheduled check for icinga.org!http." - }, - { - "code": 200.0, - "status": "Successfully rescheduled check for icinga.org!disk." - }, - { - "code": 200.0, - "status": "Successfully rescheduled check for icinga.org!disk /." - } - ] - } + /v1/actions/reschedule-check?next_check=time_t&force_check=boolean +Target: `Service` or `Host` +Schedules an active check of a collection of hosts or services at `next_check`. +If the `forced_check" flag is set the checks are performed regardless of what +time it is (e.g. timeperiod restrictions are ignored) and whether or not active +checks are enabled on a host/service-specific or program-wide basis. +### send-custom-notification + + /v1/actions/send-custom-notification?author=string&comment=string&(force=bool) + +Target: `Service` or `Host` + +Allows you to send a custom host/service notification. Very useful in dire +situations, emergencies or to communicate with all admins that are responsible +for a host or service. The notification requires an `author` and a `comment`, +though those may be empty. If `force` (default: false) is set to true the +notification will be send regardless of downtimes or whether notifications are +enabled or not. + +### delay-notification + + /v1/actions/delay-notification?timestamp=time_t + +Target: `Service` or `Host` + +Delays the next notifications for a collection of services or hosts until +`timestamp`. Note that this will only have an effect if the service stays in +the same problem state that it is currently in. If the service changes to +another state, a new notification may go out before the time you specify in the +`timestamp` argument. + +### acknowledge-problem + + /v1/actions/acknowledge-problem?author=string&comment=string&(expiry=time_t)&(sticky=bool)&(sticky=bool) + +Target: `Service` or `Host` + +Allows you to acknowledge the current problem for hosts or services. By +acknowledging the current problem, future notifications (for the same state) +are disabled. Acknowledgements require an `author` and a `comment` for +documentation purposes, though both may be empty. If you set an `expiry` time +the acknowledgement will vanish after that timestamp. If the `sticky` option is +set (the default), the acknowledgement will remain until the host recovers. +Otherwise the acknowledgement will automatically be removed when the host +changes state. If the `notify` option is set, a notification will be sent out +to contacts indicating that the current host problem has been acknowledged, if +set to false (the default) there will be no notification. + +### remove-acknowledgement + + /v1/actions/remove-acknowledgement + +Target: `Service` or `Host` + +Removes acknowledgements for services or hosts. Once the acknowledgement has +been removed, notifications can once again be sent out. + +### add-comment + + /v1/actions/add-comment?author=string&comment=string + +Target: `service` or `host` + +Adds a `comment` by `author` to services or hosts. + +### remove-comment + + /v1/actions/remove-comment + +Target: `Service` or `Host` + +Removes ALL comments for services or hosts. + +### remove-comment-by-id + + /v1/actions/remove-comment-by-id?comment_id=int + +Target: `None` + +Removes the comment with the legacy ID `comment_id` + +### schedule-downtime + + /v1/actions/schedule-downtime?start_time=time_t&end_time=time_t&duration=int&author=string&comment=string&\ + (fixed=bool)&(trigger_id=int) + +Target: `Host` or `Service` + +Schedules downtime for services or hosts. If the `fixed` argument is set to +true (default: false) the downtime will start and end at the times specified by +the `start_time` and `end_time` arguments. Otherwise, downtime will begin +between `start_time` and `start_end` and last for `duration` seconds. The +downtime can be triggered by another downtime entry if the `trigger_id` is set +to the ID of another scheduled downtime entry. Set the `trigger_id` argument to +zero (the default) if the downtime for the specified host should not be +triggered by another downtime entry. All downtimes also need a `comment` and +with it an `author`, even though both can be empty. + +### remove-downtime + + /v1/actions/remove-downtime + +Target: `Host` or `Service` + +Removes ALL downtimes for services or hosts. + +### remove-downtime-by-id + + /v1/actions/remove-downtime-by-id?downtime_id=int + +Target: `None` + +Removes the comment with the legacy ID `downtime_id` + +### shutdown-process + + /v1/actions/shutdown-process + +Target: `None` + +Shuts down Icinga2. May or may not return. + +### restart-process + + /v1/actions/restart-process + +Target: `None` + +Restarts Icinga2. May or may not return. ## Event Streams diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index cb55d70e2..071fc15bb 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -35,8 +35,8 @@ using namespace icinga; REGISTER_APIACTION(process_check_result, "Service;Host", &ApiActions::ProcessCheckResult); REGISTER_APIACTION(reschedule_check, "Service;Host", &ApiActions::RescheduleCheck); -REGISTER_APIACTION(delay_notifications, "Service;Host", &ApiActions::DelayNotifications); - +REGISTER_APIACTION(send_custom_notification, "Service;Host", &ApiActions::SendCustomNotification); +REGISTER_APIACTION(delay_notification, "Service;Host", &ApiActions::DelayNotification); REGISTER_APIACTION(acknowledge_problem, "Service;Host", &ApiActions::AcknowledgeProblem); REGISTER_APIACTION(remove_acknowledgement, "Service;Host", &ApiActions::RemoveAcknowledgement); REGISTER_APIACTION(add_comment, "Service;Host", &ApiActions::AddComment); @@ -45,43 +45,11 @@ REGISTER_APIACTION(remove_comment_by_id, "", &ApiActions::RemoveCommentByID); REGISTER_APIACTION(schedule_downtime, "Service;Host", &ApiActions::ScheduleDowntime); REGISTER_APIACTION(remove_downtime, "Service;Host", &ApiActions::RemoveDowntime); REGISTER_APIACTION(remove_downtime_by_id, "", &ApiActions::RemoveDowntimeByID); - -REGISTER_APIACTION(enable_passive_checks, "Service;Host", &ApiActions::EnablePassiveChecks); -REGISTER_APIACTION(disable_passive_checks, "Service;Host", &ApiActions::DisablePassiveChecks); -REGISTER_APIACTION(enable_active_checks, "Host", &ApiActions::EnableActiveChecks); -REGISTER_APIACTION(disable_active_checks, "Host", &ApiActions::DisableActiveChecks); -REGISTER_APIACTION(enable_notifications, "Service;Host", &ApiActions::EnableNotifications); -REGISTER_APIACTION(disable_notifications, "Service;Host", &ApiActions::DisableNotifications); -REGISTER_APIACTION(enable_flap_detection, "Service;Host", &ApiActions::EnableFlapDetection); -REGISTER_APIACTION(disable_flap_detection, "Service;Host", &ApiActions::DisableFlapDetection); - -//TODO-MA. Figure out how to handle modified attributes as actions -/* -REGISTER_APIACTION(change_event_handler, "Service;Host", &ApiActions::ChangeEventHandler); -REGISTER_APIACTION(change_check_command, "Service;Host", &ApiActions::ChangeCheckCommand); -REGISTER_APIACTION(change_max_check_attempts, "Service;Host", &ApiActions::ChangeMaxCheckAttempts); -REGISTER_APIACTION(change_check_period, "Service;Host", &ApiActions::ChangeCheckPeriod); -REGISTER_APIACTION(change_check_interval, "Service;Host", &ApiActions::ChangeCheckInterval); -REGISTER_APIACTION(change_retry_interval, "Service;Host", &ApiActions::ChangeRetryInterval); -*/ - -REGISTER_APIACTION(enable_global_notifications, "", &ApiActions::EnableGlobalNotifications); -REGISTER_APIACTION(disable_global_notifications, "", &ApiActions::DisableGlobalNotifications); -REGISTER_APIACTION(enable_global_flap_detection, "", &ApiActions::EnableGlobalFlapDetection); -REGISTER_APIACTION(disable_global_flap_detection, "", &ApiActions::DisableGlobalFlapDetection); -REGISTER_APIACTION(enable_global_event_handlers, "", &ApiActions::EnableGlobalEventHandlers); -REGISTER_APIACTION(disable_global_event_handlers, "", &ApiActions::DisableGlobalEventHandlers); -REGISTER_APIACTION(enable_global_performance_data, "", &ApiActions::EnableGlobalPerformanceData); -REGISTER_APIACTION(disable_global_performance_data, "", &ApiActions::DisableGlobalPerformanceData); -REGISTER_APIACTION(start_global_executing_svc_checks, "", &ApiActions::StartGlobalExecutingSvcChecks); -REGISTER_APIACTION(stop_global_executing_svc_checks, "", &ApiActions::StopGlobalExecutingSvcChecks); -REGISTER_APIACTION(start_global_executing_host_checks, "", &ApiActions::StartGlobalExecutingHostChecks); -REGISTER_APIACTION(stop_global_executing_host_checks, "", &ApiActions::StopGlobalExecutingHostChecks); - REGISTER_APIACTION(shutdown_process, "", &ApiActions::ShutdownProcess); REGISTER_APIACTION(restart_process, "", &ApiActions::RestartProcess); -Dictionary::Ptr ApiActions::CreateResult(int code, const String& status, const Dictionary::Ptr& additional) +Dictionary::Ptr ApiActions::CreateResult(int code, const String& status, + const Dictionary::Ptr& additional) { Dictionary::Ptr result = new Dictionary(); result->Set("code", code); @@ -93,36 +61,17 @@ Dictionary::Ptr ApiActions::CreateResult(int code, const String& status, const D return result; } -Dictionary::Ptr ApiActions::RescheduleCheck(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot reschedule check for non-existent object."); - - if (Convert::ToBool(HttpUtility::GetLastParameter(params, "force"))) - checkable->SetForceNextCheck(true); - - double nextCheck; - if (params->Contains("next_check")) - nextCheck = HttpUtility::GetLastParameter(params, "next_check"); - else - nextCheck = Utility::GetTime(); - - checkable->SetNextCheck(nextCheck); - - return ApiActions::CreateResult(200, "Successfully rescheduled check for " + checkable->GetName() + "."); -} - -Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot process passive check result for non-existent object."); + return ApiActions::CreateResult(404, + "Cannot process passive check result for non-existent object."); if (!checkable->GetEnablePassiveChecks()) - return ApiActions::CreateResult(403, "Passive checks are disabled for " + checkable->GetName()); + return ApiActions::CreateResult(403, "Passive checks are disabled for object '" + checkable->GetName() + "'."); Host::Ptr host; Service::Ptr service; @@ -141,7 +90,8 @@ Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object, else if (exitStatus == 1) state = ServiceCritical; else - return ApiActions::CreateResult(403, "Invalid 'exit_status' for Host " + checkable->GetName() + "."); + return ApiActions::CreateResult(403, "Invalid 'exit_status' for Host " + + checkable->GetName() + "."); } else { state = PluginUtility::ExitStatusToState(exitStatus); } @@ -168,58 +118,74 @@ Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object, * active checks. */ checkable->SetNextCheck(Utility::GetTime() + checkable->GetCheckInterval()); - return ApiActions::CreateResult(200, "Successfully processed check result for object " + checkable->GetName() + "."); + return ApiActions::CreateResult(200, "Successfully processed check result for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::EnablePassiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RescheduleCheck(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot enable passive checks for non-existent object."); + return ApiActions::CreateResult(404, "Cannot reschedule check for non-existent object."); - checkable->SetEnablePassiveChecks(true); + if (Convert::ToBool(HttpUtility::GetLastParameter(params, "force"))) + checkable->SetForceNextCheck(true); - return ApiActions::CreateResult(200, "Successfully enabled passive checks for object " + checkable->GetName() + "."); + double nextCheck; + if (params->Contains("next_check")) + nextCheck = HttpUtility::GetLastParameter(params, "next_check"); + else + nextCheck = Utility::GetTime(); + + checkable->SetNextCheck(nextCheck); + + return ApiActions::CreateResult(200, "Successfully rescheduled check for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::DisablePassiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::SendCustomNotification(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot disable passive checks non-existent object."); + return ApiActions::CreateResult(404, "Cannot send notification for non-existent object."); - checkable->SetEnablePassiveChecks(false); + if (!params->Contains("author")) + return ApiActions::CreateResult(403, "Parameter 'author' is required."); - return ApiActions::CreateResult(200, "Successfully disabled passive checks for object " + checkable->GetName() + "."); + if (!params->Contains("comment")) + return ApiActions::CreateResult(403, "Parameter 'comment' is required."); + + if (Convert::ToBool(HttpUtility::GetLastParameter(params, "force"))) + checkable->SetForceNextNotification(true); + + Checkable::OnNotificationsRequested(checkable, NotificationCustom, checkable->GetLastCheckResult(), + HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "comment")); + + return ApiActions::CreateResult(200, "Successfully sent custom notification for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::EnableActiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::DelayNotification(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot enable passive checks for non-existent object."); + return ApiActions::CreateResult(404, "Cannot delay notifications for non-existent object"); - checkable->SetEnableActiveChecks(true); + if (!params->Contains("timestamp")) + return ApiActions::CreateResult(403, "A timestamp is required to delay notifications"); - return ApiActions::CreateResult(200, "Successfully enabled passive checks for object " + checkable->GetName() + "."); + BOOST_FOREACH(const Notification::Ptr& notification, checkable->GetNotifications()) { + notification->SetNextNotification(HttpUtility::GetLastParameter(params, "timestamp")); + } + + return ApiActions::CreateResult(200, "Successfully delayed notifications for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::DisableActiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot disable passive checks non-existent object."); - - checkable->SetEnableActiveChecks(false); - - return ApiActions::CreateResult(200, "Successfully disabled passive checks for object " + checkable->GetName() + "."); -} - -Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); @@ -232,12 +198,15 @@ Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object, AcknowledgementType sticky = AcknowledgementNormal; bool notify = false; double timestamp = 0.0; + if (params->Contains("sticky")) sticky = AcknowledgementSticky; if (params->Contains("notify")) notify = true; - if (params->Contains("timestamp")) - timestamp = HttpUtility::GetLastParameter(params, "timestamp"); + if (params->Contains("expiry")) + timestamp = HttpUtility::GetLastParameter(params, "expiry"); + else + timestamp = 0; Host::Ptr host; Service::Ptr service; @@ -256,23 +225,27 @@ Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object, checkable->AcknowledgeProblem(HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "comment"), sticky, notify, timestamp); - return ApiActions::CreateResult(200, "Successfully acknowledged problem for " + checkable->GetName()); + return ApiActions::CreateResult(200, "Successfully acknowledged problem for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::RemoveAcknowledgement(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RemoveAcknowledgement(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot remove acknowlegement for non-existent checkable object " + object->GetName() + "."); + return ApiActions::CreateResult(404, + "Cannot remove acknowlegement for non-existent checkable object " + + object->GetName() + "."); checkable->ClearAcknowledgement(); checkable->RemoveCommentsByType(CommentAcknowledgement); - return ApiActions::CreateResult(200, "Successfully removed acknowledgement for " + checkable->GetName() + "."); + return ApiActions::CreateResult(200, "Successfully removed acknowledgement for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); @@ -282,7 +255,8 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, const Di if (!params->Contains("author") || !params->Contains("comment")) return ApiActions::CreateResult(403, "Comments require author and comment."); - String comment_id = checkable->AddComment(CommentUser, HttpUtility::GetLastParameter(params, "author"), + String comment_id = checkable->AddComment(CommentUser, + HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "comment"), 0); Comment::Ptr comment = Checkable::GetCommentByID(comment_id); @@ -292,103 +266,69 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, const Di additional->Set("comment_id", comment_id); additional->Set("legacy_id", legacy_id); - return ApiActions::CreateResult(200, "Successfully added comment with id " + - Convert::ToString(legacy_id) + " for object " + checkable->GetName() + ".", additional); + return ApiActions::CreateResult(200, "Successfully added comment with id '" + + comment_id + "' for object '" + checkable->GetName() + + "'.", additional); } -Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot remove comment form non-existent object"); + return ApiActions::CreateResult(404, "Cannot remove comment form non-existent object."); checkable->RemoveAllComments(); - return ApiActions::CreateResult(200, "Successfully removed comments for " + checkable->GetName()); + return ApiActions::CreateResult(200, "Successfully removed comments for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::RemoveCommentByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RemoveCommentByID(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { if (!params->Contains("comment_id")) return ApiActions::CreateResult(403, "Parameter 'comment_id' is required."); - int comment_id = HttpUtility::GetLastParameter(params, "comment_id"); + String comment_id = HttpUtility::GetLastParameter(params, "comment_id"); - String rid = Service::GetCommentIDFromLegacyID(comment_id); + Service::RemoveComment(comment_id); - if (rid.IsEmpty()) - return ApiActions::CreateResult(404, "Comment '" + Convert::ToString(comment_id) + "' does not exist."); - - Service::RemoveComment(rid); - - return ApiActions::CreateResult(200, "Successfully removed comment " + Convert::ToString(comment_id) + "."); + return ApiActions::CreateResult(200, "Successfully removed comment '" + comment_id + "'."); } -Dictionary::Ptr ApiActions::EnableNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Cannot enable notifications for non-existent object"); + return ApiActions::CreateResult(404, "Can't schedule downtime for non-existent object."); - checkable->SetEnableNotifications(true); + if (!params->Contains("start_time") || !params->Contains("end_time") || + !params->Contains("duration") || !params->Contains("author") || + !params->Contains("comment")) { - return ApiActions::CreateResult(200, "Successfully enabled notifications for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::DisableNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot disable notifications for non-existent object"); - - checkable->SetEnableNotifications(true); - - return ApiActions::CreateResult(200, "Successfully disabled notifications for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::DelayNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot delay notifications for non-existent object"); - - if (!params->Contains("timestamp")) - return ApiActions::CreateResult(403, "A timestamp is required to delay notifications"); - - BOOST_FOREACH(const Notification::Ptr& notification, checkable->GetNotifications()) { - notification->SetNextNotification(HttpUtility::GetLastParameter(params, "timestamp")); - } - - return ApiActions::CreateResult(200, "Successfully delayed notifications for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Can't schedule downtime for non-existent object"); - - if (!params->Contains("start_time") || !params->Contains("end_time") || !params->Contains("duration") || - !params->Contains("author") || !params->Contains("comment")) return ApiActions::CreateResult(404, "Options 'start_time', 'end_time', 'duration', 'author' and 'comment' are required"); + } bool fixed = false; if (params->Contains("fixed")) fixed = HttpUtility::GetLastParameter(params, "fixed"); - int triggeredByLegacy = params->Contains("trigger_id") ? (int) HttpUtility::GetLastParameter(params, "trigger_id") : 0; + int triggeredByLegacy = 0; + + if (params->Contains("trigger_id")) + triggeredByLegacy = HttpUtility::GetLastParameter(params, "trigger_id"); + String triggeredBy; if (triggeredByLegacy) triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy); - String downtime_id = checkable->AddDowntime(HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "comment"), - HttpUtility::GetLastParameter(params, "start_time"), HttpUtility::GetLastParameter(params, "end_time"), - fixed, triggeredBy, HttpUtility::GetLastParameter(params, "duration")); + String downtime_id = checkable->AddDowntime(HttpUtility::GetLastParameter(params, "author"), + HttpUtility::GetLastParameter(params, "comment"), HttpUtility::GetLastParameter(params, "start_time"), + HttpUtility::GetLastParameter(params, "end_time"), fixed, triggeredBy, + HttpUtility::GetLastParameter(params, "duration")); Downtime::Ptr downtime = Checkable::GetDowntimeByID(downtime_id); int legacy_id = downtime->GetLegacyId(); @@ -397,269 +337,49 @@ Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object, co additional->Set("downtime_id", downtime_id); additional->Set("legacy_id", legacy_id); - return ApiActions::CreateResult(200, "Successfully scheduled downtime with id " + - Convert::ToString(legacy_id) + " for object " + checkable->GetName() + ".", additional); + return ApiActions::CreateResult(200, "Successfully scheduled downtime with id '" + + downtime_id + "' for object '" + checkable->GetName() + "'.", additional); } -Dictionary::Ptr ApiActions::EnableFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); if (!checkable) - return ApiActions::CreateResult(404, "Can't enable flap detection for non-existent object"); - - checkable->SetEnableFlapping(true); - - return ApiActions::CreateResult(200, "Successfully enabled flap detection for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::DisableFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Can't disable flap detection for non-existent object"); - - checkable->SetEnableFlapping(false); - - return ApiActions::CreateResult(200, "Successfully disabled flap detection for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot remove downtime for non-existent object"); + return ApiActions::CreateResult(404, "Cannot remove downtime for non-existent object."); checkable->RemoveAllDowntimes(); - return ApiActions::CreateResult(200, "Successfully removed downtimes for " + checkable->GetName()); + return ApiActions::CreateResult(200, "Successfully removed downtimes for object '" + checkable->GetName() + "'."); } -Dictionary::Ptr ApiActions::RemoveDowntimeByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RemoveDowntimeByID(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { if (!params->Contains("downtime_id")) return ApiActions::CreateResult(403, "Parameter 'downtime_id' is required."); - int downtime_id = HttpUtility::GetLastParameter(params, "downtime_id"); + String downtime_id = HttpUtility::GetLastParameter(params, "downtime_id"); - String rid = Service::GetDowntimeIDFromLegacyID(downtime_id); - if (rid.IsEmpty()) - return ApiActions::CreateResult(404, "Downtime '" + Convert::ToString(downtime_id) + "' does not exist."); + Service::RemoveDowntime(downtime_id, true); - Service::RemoveDowntime(rid, true); - - return ApiActions::CreateResult(200, "Successfully removed downtime " + Convert::ToString(downtime_id) + "."); + return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtime_id + "'."); } -Dictionary::Ptr ApiActions::EnableGlobalNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableNotifications(true); - - return ApiActions::CreateResult(200, "Globally enabled notifications."); -} - -Dictionary::Ptr ApiActions::DisableGlobalNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableNotifications(false); - - return ApiActions::CreateResult(200, "Globally disabled notifications."); -} - -Dictionary::Ptr ApiActions::EnableGlobalFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableFlapping(true); - - return ApiActions::CreateResult(200, "Globally enabled flap detection."); -} - -Dictionary::Ptr ApiActions::DisableGlobalFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableFlapping(false); - - return ApiActions::CreateResult(200, "Globally disabled flap detection."); -} - -Dictionary::Ptr ApiActions::EnableGlobalEventHandlers(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableEventHandlers(true); - - return ApiActions::CreateResult(200, "Globally enabled event handlers."); -} - -Dictionary::Ptr ApiActions::DisableGlobalEventHandlers(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableEventHandlers(false); - - return ApiActions::CreateResult(200, "Globally disabled event handlers."); -} - -Dictionary::Ptr ApiActions::EnableGlobalPerformanceData(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnablePerfdata(true); - - return ApiActions::CreateResult(200, "Globally enabled performance data processing."); -} - -Dictionary::Ptr ApiActions::DisableGlobalPerformanceData(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnablePerfdata(false); - - return ApiActions::CreateResult(200, "Globally disabled performance data processing."); -} - -Dictionary::Ptr ApiActions::StartGlobalExecutingSvcChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableServiceChecks(true); - - return ApiActions::CreateResult(200, "Globally enabled service checks."); -} - -Dictionary::Ptr ApiActions::StopGlobalExecutingSvcChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableServiceChecks(false); - - return ApiActions::CreateResult(200, "Globally disabled service checks."); -} - -Dictionary::Ptr ApiActions::StartGlobalExecutingHostChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableHostChecks(true); - - return ApiActions::CreateResult(200, "Globally enabled host checks."); -} - -Dictionary::Ptr ApiActions::StopGlobalExecutingHostChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - IcingaApplication::GetInstance()->SetEnableHostChecks(false); - - return ApiActions::CreateResult(200, "Globally disabled host checks."); -} - -//TODO-MA -/* -Dictionary::Ptr ApiActions::ChangeEventHandler(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot change event handler of a non-existent object"); - - // empty command string implicitely disables event handler - if (!params->Contains("event_command_name")) { - checkable->SetEnableEventHandler(false); - return ApiActions::CreateResult(200, "Successfully disabled event handler for " + checkable->GetName()); - } else { - String event_name = HttpUtility::GetLastParameter(params, "event_command_name"); - - EventCommand::Ptr command = EventCommand::GetByName(event_name); - - if (!command) - return ApiActions::CreateResult(404, "Event command '" + event_name + "' does not exist"); - - checkable->SetEventCommand(command); - - return ApiActions::CreateResult(200, "Successfully changed event command for " + checkable->GetName()); - } -} - -Dictionary::Ptr ApiActions::ChangeCheckCommand(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot change check command of a non-existent object"); - if (!params->Contains("check_command_name")) - return ApiActions::CreateResult(403, "Parameter 'check_command_name' is required"); - - String check_name = HttpUtility::GetLastParameter(params, "check_command_name"); - - CheckCommand::Ptr command = CheckCommand::GetByName(check_name); - - if (!command) - return ApiActions::CreateResult(404, "Check command '" + check_name + "' does not exist"); - - checkable->SetCheckCommand(command); - - return ApiActions::CreateResult(200, "Successfully changed check command for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::ChangeMaxCheckAttempts(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot change check command of a non-existent object"); - if (!params->Contains("max_check_attempts")) - return ApiActions::CreateResult(403, "Parameter 'max_check_attempts' is required"); - - checkable->SetMaxCheckAttempts(HttpUtility::GetLastParameter(params, "max_check_attempts")); - - return ApiActions::CreateResult(200, "Successfully changed the maximum check attempts for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::ChangeCheckPeriod(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot change check command of a non-existent object"); - if (!params->Contains("time_period_name")) - return ApiActions::CreateResult(403, "Parameter 'time_period_name' is required"); - - String time_period_name = HttpUtility::GetLastParameter(params, "time_period_name"); - - TimePeriod::Ptr time_period = TimePeriod::GetByName(time_period_name); - - if (!time_period) - return ApiActions::CreateResult(404, "Time period '" + time_period_name + "' does not exist"); - - checkable->SetCheckPeriod(time_period); - - return ApiActions::CreateResult(200, "Successfully changed the time period for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::ChangeCheckInterval(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot change check command of a non-existent object"); - if (!params->Contains("check_interval")) - return ApiActions::CreateResult(403, "Parameter 'check_interval' is required"); - - checkable->SetCheckInterval(HttpUtility::GetLastParameter(params, "check_interval")); - - return ApiActions::CreateResult(200, "Successfully changed the check interval for " + checkable->GetName()); -} - -Dictionary::Ptr ApiActions::ChangeRetryInterval(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot change check command of a non-existent object"); - if (!params->Contains("retry_interval")) - return ApiActions::CreateResult(403, "Parameter 'retry_interval' is required"); - - checkable->SetRetryInterval(HttpUtility::GetLastParameter(params, "retry_interval")); - - return ApiActions::CreateResult(200, "Successfully changed the retry interval for " + checkable->GetName()); -} -*/ - -Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Application::RequestShutdown(); - return ApiActions::CreateResult(200, "Shutting down Icinga2"); + return ApiActions::CreateResult(200, "Shutting down Icinga 2."); } -Dictionary::Ptr ApiActions::RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) +Dictionary::Ptr ApiActions::RestartProcess(const ConfigObject::Ptr& object, + const Dictionary::Ptr& params) { Application::RequestRestart(); - return ApiActions::CreateResult(200, "Restarting Icinga"); + return ApiActions::CreateResult(200, "Restarting Icinga 2."); } diff --git a/lib/icinga/apiactions.hpp b/lib/icinga/apiactions.hpp index 9b868c287..246da8fb3 100644 --- a/lib/icinga/apiactions.hpp +++ b/lib/icinga/apiactions.hpp @@ -35,8 +35,8 @@ class I2_ICINGA_API ApiActions public: static Dictionary::Ptr ProcessCheckResult(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RescheduleCheck(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DelayNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - + static Dictionary::Ptr SendCustomNotification(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); + static Dictionary::Ptr DelayNotification(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr AcknowledgeProblem(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveAcknowledgement(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); @@ -45,38 +45,6 @@ public: static Dictionary::Ptr ScheduleDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveDowntime(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveDowntimeByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - - static Dictionary::Ptr EnablePassiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisablePassiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr EnableActiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableActiveChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr EnableNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr EnableFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - -/* - static Dictionary::Ptr ChangeEventHandler(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ChangeCheckCommand(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ChangeMaxCheckAttempts(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ChangeCheckInterval(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ChangeRetryInterval(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ChangeCheckPeriod(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); -*/ - - static Dictionary::Ptr EnableGlobalNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableGlobalNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr EnableGlobalFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableGlobalFlapDetection(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr EnableGlobalEventHandlers(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableGlobalEventHandlers(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr EnableGlobalPerformanceData(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr DisableGlobalPerformanceData(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr StartGlobalExecutingSvcChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr StopGlobalExecutingSvcChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr StartGlobalExecutingHostChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr StopGlobalExecutingHostChecks(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ShutdownProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); @@ -87,4 +55,3 @@ private: } #endif /* APIACTIONS_H */ -