From 01ced1549af13bbaa24d9e23c5ec707051efb538 Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Fri, 21 Aug 2015 15:50:40 +0200 Subject: [PATCH] Implement all actions except modified attributes refs #9080 #9979 --- doc/9080-apiactions.md(WIP) | 32 ++++----- lib/icinga/apiactions.cpp | 105 ++++++++++++------------------ lib/icinga/apiactions.hpp | 13 ++-- lib/icinga/checkable-downtime.cpp | 17 +++++ lib/icinga/checkable.hpp | 1 + lib/remote/actionshandler.cpp | 4 ++ 6 files changed, 87 insertions(+), 85 deletions(-) diff --git a/doc/9080-apiactions.md(WIP) b/doc/9080-apiactions.md(WIP) index 8f3d25483..9d9be98d2 100644 --- a/doc/9080-apiactions.md(WIP) +++ b/doc/9080-apiactions.md(WIP) @@ -8,11 +8,12 @@ reschedule-check | {next_check}; {(force_check)} | Service acknowledge-problem | author; comment; {timestamp}; {(sticky)}; {(notify)} | Service; Host | - remove-acknowledgement | - | Service; Host | - add-comment | author; comment | Service; Host | - -remove-comment | comment_id | - | - -remove-all-comments | - | 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 | downtime_id | - | remove by name? +remove-downtime | - | Service; Host | - +remove-downtime-by-id | downtime_id | - | - send-custom-notification | options[]; author; comment | Service; Host | - enable-passive-checks | - | Service; Host; ServiceGroup; HostGroup | "System" as target? @@ -35,18 +36,17 @@ change-check-interval | check_interval | Service; Host | - change-retry-interval | retry_interval | Service; Host | - change-check-period | time_period_name | Service; Host | - -enable-all-notifications | - | - | - -disable-all-notifications | - | - | - -enable-all-flap-detection | - | - | - -disable-all-flap-detection | - | - | - -enable-all-event-handlers | - | - | - -disable-all-event-handlers | - | - | - -enable-all-performance-data | - | - | - -disable-all-performance-data | - | - | - -start-all-executing-svc-checks | - | - | - -stop-all-executing-svc-checks | - | - | - -start-all-executing-host-checks | - | - | - -stop-all-executing-host-checks | - | - | - +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 | - | - | - -process-file | - | - | - diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index e5a07bb44..cb55d70e2 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -29,6 +29,7 @@ #include "remote/httputility.hpp" #include "base/utility.hpp" #include "base/convert.hpp" +#include using namespace icinga; @@ -39,10 +40,11 @@ REGISTER_APIACTION(delay_notifications, "Service;Host", &ApiActions::DelayNotifi REGISTER_APIACTION(acknowledge_problem, "Service;Host", &ApiActions::AcknowledgeProblem); REGISTER_APIACTION(remove_acknowledgement, "Service;Host", &ApiActions::RemoveAcknowledgement); REGISTER_APIACTION(add_comment, "Service;Host", &ApiActions::AddComment); -REGISTER_APIACTION(remove_comment, "", &ApiActions::RemoveComment); -REGISTER_APIACTION(remove_all_comments, "Service;Host", &ApiActions::RemoveAllComments); +REGISTER_APIACTION(remove_comment, "Service;Host", &ApiActions::RemoveComment); +REGISTER_APIACTION(remove_comment_by_id, "", &ApiActions::RemoveCommentByID); REGISTER_APIACTION(schedule_downtime, "Service;Host", &ApiActions::ScheduleDowntime); -REGISTER_APIACTION(remove_downtime, "", &ApiActions::RemoveDowntime); +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); @@ -76,12 +78,8 @@ REGISTER_APIACTION(stop_global_executing_svc_checks, "", &ApiActions::StopGlobal REGISTER_APIACTION(start_global_executing_host_checks, "", &ApiActions::StartGlobalExecutingHostChecks); REGISTER_APIACTION(stop_global_executing_host_checks, "", &ApiActions::StopGlobalExecutingHostChecks); -//TODO: add process related actions -/* REGISTER_APIACTION(shutdown_process, "", &ApiActions::ShutdownProcess); REGISTER_APIACTION(restart_process, "", &ApiActions::RestartProcess); -REGISTER_APIACTION(process_file, "", &ApiActions::ProcessFile); -*/ Dictionary::Ptr ApiActions::CreateResult(int code, const String& status, const Dictionary::Ptr& additional) { @@ -299,30 +297,34 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, const Di } 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"); + + checkable->RemoveAllComments(); + + return ApiActions::CreateResult(200, "Successfully removed comments for " + checkable->GetName()); +} + +Dictionary::Ptr ApiActions::RemoveCommentByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) { if (!params->Contains("comment_id")) - return ApiActions::CreateResult(403, "'comment_id' required."); + return ApiActions::CreateResult(403, "Parameter 'comment_id' is required."); int comment_id = HttpUtility::GetLastParameter(params, "comment_id"); String rid = Service::GetCommentIDFromLegacyID(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) + "."); } -Dictionary::Ptr ApiActions::RemoveAllComments(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - Checkable::Ptr checkable = static_pointer_cast(object); - - if (!checkable) - return ApiActions::CreateResult(404, "Cannot remove comments from non-existent object"); - - checkable->RemoveAllComments(); - - return ApiActions::CreateResult(200, "Successfully removed all comments for " + checkable->GetName()); -} - Dictionary::Ptr ApiActions::EnableNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast(object); @@ -424,16 +426,31 @@ Dictionary::Ptr ApiActions::DisableFlapDetection(const ConfigObject::Ptr& object } 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"); + + checkable->RemoveAllDowntimes(); + + return ApiActions::CreateResult(200, "Successfully removed downtimes for " + checkable->GetName()); +} + +Dictionary::Ptr ApiActions::RemoveDowntimeByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) { if (!params->Contains("downtime_id")) - return ApiActions::CreateResult(403, "Downtime removal requires a downtime_id"); + return ApiActions::CreateResult(403, "Parameter 'downtime_id' is required."); int 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(rid, true); - return ApiActions::CreateResult(200, "Successfully removed downtime with id " + Convert::ToString(downtime_id) + "."); + return ApiActions::CreateResult(200, "Successfully removed downtime " + Convert::ToString(downtime_id) + "."); } Dictionary::Ptr ApiActions::EnableGlobalNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) @@ -632,55 +649,17 @@ Dictionary::Ptr ApiActions::ChangeRetryInterval(const ConfigObject::Ptr& object, } */ -//TODO: process actions -/* -Dictionary::Ptr ApiActions::RestartProcess(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, "I don't exist!"); + return ApiActions::CreateResult(200, "Shutting down Icinga2"); } Dictionary::Ptr ApiActions::RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) { Application::RequestRestart(); - return ApiActions::CreateResult(200, "That's not how this works"); + return ApiActions::CreateResult(200, "Restarting Icinga"); } -Dictionary::Ptr ApiActions::ProcessFile(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) -{ - if (!params->Contains("file_name") - return ApiActions::CreateResult(403, "Parameter 'file_name' is required"); - - String file = HttpUtility::GetLastParameter(params, "file_name") - - bool del = true; - if (!params->Contains("delete") || !HttpUtility::GetLastParameter(params, "delete")) - del = false; - - std::ifstream ifp; - ifp.exceptions(std::ifstream::badbit); - - ifp.open(file.CStr(), std::ifstream::in); - - while (ifp.good()) { - std::string line; - std::getline(ifp, line); - - try { - Execute(line); - } catch (const std::exception& ex) { - ifp.close(); - return ApiActions::CreateResult(500, "Command execution failed"); - } - } - - ifp.close(); - - if (del) - (void) unlink(file.CStr()); - - return ApiActions::CreateResult(200, "Successfully processed " + (del?"and deleted ":"") + "file " + file); -} -*/ diff --git a/lib/icinga/apiactions.hpp b/lib/icinga/apiactions.hpp index c7d06e715..9b868c287 100644 --- a/lib/icinga/apiactions.hpp +++ b/lib/icinga/apiactions.hpp @@ -41,9 +41,10 @@ public: static Dictionary::Ptr RemoveAcknowledgement(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr RemoveAllComments(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); + static Dictionary::Ptr RemoveCommentByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); 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); @@ -54,12 +55,14 @@ public: 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 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 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); @@ -74,10 +77,8 @@ public: 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 ShutdownProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); static Dictionary::Ptr RestartProcess(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - static Dictionary::Ptr ProcessFile(const ConfigObject::Ptr& object, const Dictionary::Ptr& params); - */ private: static Dictionary::Ptr CreateResult(int code, const String& status, const Dictionary::Ptr& additional = Dictionary::Ptr()); diff --git a/lib/icinga/checkable-downtime.cpp b/lib/icinga/checkable-downtime.cpp index 5f1f68f0a..01e8bba62 100644 --- a/lib/icinga/checkable-downtime.cpp +++ b/lib/icinga/checkable-downtime.cpp @@ -165,6 +165,23 @@ void Checkable::RemoveDowntime(const String& id, bool cancelled, const MessageOr OnDowntimeRemoved(owner, downtime, origin); } +void Checkable::RemoveAllDowntimes(void) +{ + std::vector ids; + Dictionary::Ptr downtimes = GetDowntimes(); + + { + ObjectLock olock(downtimes); + BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) { + ids.push_back(kv.first); + } + } + + BOOST_FOREACH(const String& id, ids) { + RemoveDowntime(id, true); + } +} + void Checkable::TriggerDowntimes(void) { Dictionary::Ptr downtimes = GetDowntimes(); diff --git a/lib/icinga/checkable.hpp b/lib/icinga/checkable.hpp index 6afd3a321..31b9c2011 100644 --- a/lib/icinga/checkable.hpp +++ b/lib/icinga/checkable.hpp @@ -148,6 +148,7 @@ public: const String& scheduledBy = String(), const String& id = String(), const MessageOrigin::Ptr& origin = MessageOrigin::Ptr()); + void RemoveAllDowntimes(void); static void RemoveDowntime(const String& id, bool cancelled, const MessageOrigin::Ptr& origin = MessageOrigin::Ptr()); void TriggerDowntimes(void); diff --git a/lib/remote/actionshandler.cpp b/lib/remote/actionshandler.cpp index eaabd7c37..26fb662c6 100644 --- a/lib/remote/actionshandler.cpp +++ b/lib/remote/actionshandler.cpp @@ -23,6 +23,7 @@ #include "remote/apiaction.hpp" #include "base/exception.hpp" #include "base/serializer.hpp" +#include "base/logger.hpp" #include #include @@ -63,6 +64,9 @@ bool ActionsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& reques Array::Ptr results = new Array(); + Log(LogNotice, "ApiActionHandler") + << "Running action " << actionName; + BOOST_FOREACH(const ConfigObject::Ptr& obj, objs) { try { results->Add(action->Invoke(obj, params));