]> granicus.if.org Git - icinga2/commitdiff
Implement all actions except modified attributes
authorJean Flach <jean-marcel.flach@netways.de>
Fri, 21 Aug 2015 13:50:40 +0000 (15:50 +0200)
committerJean Flach <jean-marcel.flach@netways.de>
Wed, 26 Aug 2015 11:53:17 +0000 (13:53 +0200)
refs #9080 #9979

doc/9080-apiactions.md(WIP)
lib/icinga/apiactions.cpp
lib/icinga/apiactions.hpp
lib/icinga/checkable-downtime.cpp
lib/icinga/checkable.hpp
lib/remote/actionshandler.cpp

index 8f3d2548396ef9bb2a3e1f9680ab696aa73e3767..9d9be98d2c4c4a275ca0072162555daa5de30763 100644 (file)
@@ -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                           | - | - | -
index e5a07bb44bcb8b1771ba742b3b242a486adb9cf1..cb55d70e2d443f7abf352b2b7faab77fbdbdee64 100644 (file)
@@ -29,6 +29,7 @@
 #include "remote/httputility.hpp"
 #include "base/utility.hpp"
 #include "base/convert.hpp"
+#include <fstream>
 
 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)
 {
@@ -300,27 +298,31 @@ Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, const Di
 
 Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params)
 {
-       if (!params->Contains("comment_id"))
-               return ApiActions::CreateResult(403, "'comment_id' required.");
+       Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
 
-       int comment_id = HttpUtility::GetLastParameter(params, "comment_id");
+       if (!checkable)
+               return ApiActions::CreateResult(404, "Cannot remove comment form non-existent object");
 
-       String rid = Service::GetCommentIDFromLegacyID(comment_id);
-       Service::RemoveComment(rid);
+       checkable->RemoveAllComments();
 
-       return ApiActions::CreateResult(200, "Successfully removed comment " + Convert::ToString(comment_id) + ".");
+       return ApiActions::CreateResult(200, "Successfully removed comments for " + checkable->GetName());
 }
 
-Dictionary::Ptr ApiActions::RemoveAllComments(const ConfigObject::Ptr& object, const Dictionary::Ptr& params)
+Dictionary::Ptr ApiActions::RemoveCommentByID(const ConfigObject::Ptr& object, const Dictionary::Ptr& params)
 {
-       Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
+       if (!params->Contains("comment_id"))
+               return ApiActions::CreateResult(403, "Parameter 'comment_id' is required.");
 
-       if (!checkable)
-               return ApiActions::CreateResult(404, "Cannot remove comments from non-existent object");
+       int comment_id = HttpUtility::GetLastParameter(params, "comment_id");
 
-       checkable->RemoveAllComments();
+       String rid = Service::GetCommentIDFromLegacyID(comment_id);
+
+       if (rid.IsEmpty())
+               return ApiActions::CreateResult(404, "Comment '" + Convert::ToString(comment_id) + "' does not exist.");
 
-       return ApiActions::CreateResult(200, "Successfully removed all comments for " + checkable->GetName());
+       Service::RemoveComment(rid);
+
+       return ApiActions::CreateResult(200, "Successfully removed comment " + Convert::ToString(comment_id) + ".");
 }
 
 Dictionary::Ptr ApiActions::EnableNotifications(const ConfigObject::Ptr& object, const Dictionary::Ptr& params)
@@ -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<Checkable>(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);
-}
-*/
index c7d06e715faf3bf7ea26a17aca554fa4727c0877..9b868c2872f6801db90acc9792b802370931f453 100644 (file)
@@ -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());
index 5f1f68f0a64abbced05278e92a4151fdf74da993..01e8bba62fe5d94eca410352bd4e9b449c5aafe1 100644 (file)
@@ -165,6 +165,23 @@ void Checkable::RemoveDowntime(const String& id, bool cancelled, const MessageOr
        OnDowntimeRemoved(owner, downtime, origin);
 }
 
+void Checkable::RemoveAllDowntimes(void)
+{
+       std::vector<String> 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();
index 6afd3a321d5b63206f69559c0412fa8e192f62f2..31b9c2011d09a83d782196364d6e92633c306420 100644 (file)
@@ -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);
index eaabd7c371325bab343056ddc310e0d1f739d05f..26fb662c6a1431a2f5657d09d31ab1c023fe0104 100644 (file)
@@ -23,6 +23,7 @@
 #include "remote/apiaction.hpp"
 #include "base/exception.hpp"
 #include "base/serializer.hpp"
+#include "base/logger.hpp"
 #include <boost/algorithm/string.hpp>
 #include <set>
 
@@ -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));