]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/externalcommandprocessor.cpp
External commands fail gracefully when given invalid host/service names.
[icinga2] / lib / icinga / externalcommandprocessor.cpp
index aa1b9ca62b08cf279b6623ac63e3d7e7ccfd8bcb..538d60a91e516415f655304f9691307b5e305e35 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
+ * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
  *                                                                            *
  * This program is free software; you can redistribute it and/or              *
  * modify it under the terms of the GNU General Public License                *
@@ -180,7 +180,10 @@ void ExternalCommandProcessor::ProcessHostCheckResult(double time, const std::ve
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
-       Service::Ptr hc = host->GetHostCheckService();
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot passive host check result for non-existent host '" + arguments[0] + "'"));
+
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc->GetEnablePassiveChecks())
                BOOST_THROW_EXCEPTION(std::invalid_argument("Got passive check result for host '" + arguments[0] + "' which has passive checks disabled."));
@@ -215,6 +218,9 @@ void ExternalCommandProcessor::ProcessServiceCheckResult(double time, const std:
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot process passive service check result for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        if (!service->GetEnablePassiveChecks())
                BOOST_THROW_EXCEPTION(std::invalid_argument("Got passive check result for service '" + arguments[1] + "' which has passive checks disabled."));
 
@@ -248,7 +254,10 @@ void ExternalCommandProcessor::ScheduleHostCheck(double, const std::vector<Strin
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
-       Service::Ptr hc = host->GetHostCheckService();
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot reschedule host check for non-existent host '" + arguments[0] + "'"));
+
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc) {
                Log(LogInformation, "icinga", "Ignoring request request for host '" +
@@ -283,7 +292,10 @@ void ExternalCommandProcessor::ScheduleForcedHostCheck(double, const std::vector
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
-       Service::Ptr hc = host->GetHostCheckService();
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot reschedule forced host check for non-existent host '" + arguments[0] + "'"));
+
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc) {
                Log(LogInformation, "icinga", "Ignoring request request for host '" +
@@ -308,6 +320,9 @@ void ExternalCommandProcessor::ScheduleSvcCheck(double, const std::vector<String
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot reschedule service check for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        double planned_check = Convert::ToDouble(arguments[2]);
 
        if (planned_check > service->GetNextCheck()) {
@@ -335,6 +350,9 @@ void ExternalCommandProcessor::ScheduleForcedSvcCheck(double, const std::vector<
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot reschedule forced service check for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Rescheduling next check for service '" + arguments[1] + "'");
 
        {
@@ -352,8 +370,11 @@ void ExternalCommandProcessor::EnableHostCheck(double, const std::vector<String>
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable host check non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling active checks for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -372,8 +393,11 @@ void ExternalCommandProcessor::DisableHostCheck(double, const std::vector<String
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable host check non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling active checks for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -392,6 +416,9 @@ void ExternalCommandProcessor::EnableSvcCheck(double, const std::vector<String>&
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable service check for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling active checks for service '" + arguments[1] + "'");
 
        {
@@ -408,6 +435,9 @@ void ExternalCommandProcessor::DisableSvcCheck(double, const std::vector<String>
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable service check for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling active checks for service '" + arguments[1] + "'");
 
        {
@@ -438,6 +468,9 @@ void ExternalCommandProcessor::ScheduleForcedHostSvcChecks(double, const std::ve
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot reschedule forced host service checks for non-existent host '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                Log(LogInformation, "icinga", "Rescheduling next check for service '" + service->GetName() + "'");
 
@@ -459,6 +492,9 @@ void ExternalCommandProcessor::ScheduleHostSvcChecks(double, const std::vector<S
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot reschedule host service checks for non-existent host '" + arguments[0] + "'"));
+
        if (planned_check < Utility::GetTime())
                planned_check = Utility::GetTime();
 
@@ -486,6 +522,9 @@ void ExternalCommandProcessor::EnableHostSvcChecks(double, const std::vector<Str
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable host service checks for non-existent host '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                Log(LogInformation, "icinga", "Enabling active checks for service '" + service->GetName() + "'");
                service->SetEnableActiveChecks(true);
@@ -499,6 +538,9 @@ void ExternalCommandProcessor::DisableHostSvcChecks(double, const std::vector<St
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable host service checks for non-existent host '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                Log(LogInformation, "icinga", "Disabling active checks for service '" + service->GetName() + "'");
 
@@ -519,6 +561,9 @@ void ExternalCommandProcessor::AcknowledgeSvcProblem(double, const std::vector<S
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot acknowledge service problem for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        if (service->GetState() == StateOK)
                BOOST_THROW_EXCEPTION(std::invalid_argument("The service '" + arguments[1] + "' is OK."));
 
@@ -538,6 +583,9 @@ void ExternalCommandProcessor::AcknowledgeSvcProblemExpire(double, const std::ve
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot acknowledge service problem with expire time for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        if (service->GetState() == StateOK)
                BOOST_THROW_EXCEPTION(std::invalid_argument("The service '" + arguments[1] + "' is OK."));
 
@@ -554,6 +602,9 @@ void ExternalCommandProcessor::RemoveSvcAcknowledgement(double, const std::vecto
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot remove service acknowledgement for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Removing acknowledgement for service '" + service->GetName() + "'");
 
        service->ClearAcknowledgement();
@@ -568,8 +619,11 @@ void ExternalCommandProcessor::AcknowledgeHostProblem(double, const std::vector<
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot acknowledge host problem for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Setting acknowledgement for host '" + host->GetName() + "'");
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service) {
                if (service->GetState() == StateOK)
                        BOOST_THROW_EXCEPTION(std::invalid_argument("The host '" + arguments[0] + "' is OK."));
@@ -589,8 +643,11 @@ void ExternalCommandProcessor::AcknowledgeHostProblemExpire(double, const std::v
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot acknowledge host problem with expire time for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Setting timed acknowledgement for host '" + host->GetName() + "'");
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service) {
                if (service->GetState() == StateOK)
                        BOOST_THROW_EXCEPTION(std::invalid_argument("The host '" + arguments[0] + "' is OK."));
@@ -607,8 +664,11 @@ void ExternalCommandProcessor::RemoveHostAcknowledgement(double, const std::vect
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot remove acknowledgement for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Removing acknowledgement for host '" + host->GetName() + "'");
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service)
                service->ClearAcknowledgement();
 }
@@ -620,6 +680,9 @@ void ExternalCommandProcessor::EnableHostgroupSvcChecks(double, const std::vecto
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable hostgroup service checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
                BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                        Log(LogInformation, "icinga", "Enabling active checks for service '" + service->GetName() + "'");
@@ -640,6 +703,9 @@ void ExternalCommandProcessor::DisableHostgroupSvcChecks(double, const std::vect
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable hostgroup service checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
                BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                        Log(LogInformation, "icinga", "Disabling active checks for service '" + service->GetName() + "'");
@@ -660,6 +726,9 @@ void ExternalCommandProcessor::EnableServicegroupSvcChecks(double, const std::ve
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable servicegroup service checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Log(LogInformation, "icinga", "Enabling active checks for service '" + service->GetName() + "'");
 
@@ -678,6 +747,9 @@ void ExternalCommandProcessor::DisableServicegroupSvcChecks(double, const std::v
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable servicegroup service checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Log(LogInformation, "icinga", "Disabling active checks for service '" + service->GetName() + "'");
 
@@ -696,8 +768,11 @@ void ExternalCommandProcessor::EnablePassiveHostChecks(double, const std::vector
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable passive host checks for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling passive checks for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -716,8 +791,11 @@ void ExternalCommandProcessor::DisablePassiveHostChecks(double, const std::vecto
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable passive host checks for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling passive checks for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -736,6 +814,9 @@ void ExternalCommandProcessor::EnablePassiveSvcChecks(double, const std::vector<
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable service checks for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling passive checks for service '" + arguments[1] + "'");
 
        {
@@ -752,6 +833,9 @@ void ExternalCommandProcessor::DisablePassiveSvcChecks(double, const std::vector
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable service checks for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling passive checks for service '" + arguments[1] + "'");
 
        {
@@ -768,6 +852,9 @@ void ExternalCommandProcessor::EnableServicegroupPassiveSvcChecks(double, const
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable servicegroup passive service checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Log(LogInformation, "icinga", "Enabling passive checks for service '" + service->GetName() + "'");
 
@@ -786,6 +873,9 @@ void ExternalCommandProcessor::DisableServicegroupPassiveSvcChecks(double, const
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable servicegroup passive service checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Log(LogInformation, "icinga", "Disabling passive checks for service '" + service->GetName() + "'");
 
@@ -804,6 +894,9 @@ void ExternalCommandProcessor::EnableHostgroupPassiveSvcChecks(double, const std
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable hostgroup passive service checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
                BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                        Log(LogInformation, "icinga", "Enabling passive checks for service '" + service->GetName() + "'");
@@ -824,6 +917,9 @@ void ExternalCommandProcessor::DisableHostgroupPassiveSvcChecks(double, const st
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable hostgroup passive service checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
                BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
                        Log(LogInformation, "icinga", "Disabling passive checks for service '" + service->GetName() + "'");
@@ -878,6 +974,9 @@ void ExternalCommandProcessor::ScheduleSvcDowntime(double, const std::vector<Str
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule service downtime for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[5]);
        if (triggeredByLegacy != 0)
@@ -898,7 +997,7 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>&
        int id = Convert::ToLong(arguments[0]);
        Log(LogInformation, "icinga", "Removing downtime ID " + arguments[0]);
        String rid = Service::GetDowntimeIDFromLegacyID(id);
-       Service::RemoveDowntime(rid);
+       Service::RemoveDowntime(rid, true);
 }
 
 void ExternalCommandProcessor::ScheduleHostDowntime(double, const std::vector<String>& arguments)
@@ -908,13 +1007,16 @@ void ExternalCommandProcessor::ScheduleHostDowntime(double, const std::vector<St
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule host downtime for non-existent host '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[4]);
        if (triggeredByLegacy != 0)
                triggeredBy = Service::GetDowntimeIDFromLegacyID(triggeredByLegacy);
 
        Log(LogInformation, "icinga", "Creating downtime for host " + host->GetName());
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service) {
                String comment_id = service->AddComment(CommentDowntime, arguments[6], arguments[7], Convert::ToDouble(arguments[2]));
                (void) service->AddDowntime(comment_id,
@@ -931,7 +1033,7 @@ void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>
        int id = Convert::ToLong(arguments[0]);
        Log(LogInformation, "icinga", "Removing downtime ID " + arguments[0]);
        String rid = Service::GetDowntimeIDFromLegacyID(id);
-       Service::RemoveDowntime(rid);
+       Service::RemoveDowntime(rid, true);
 }
 
 void ExternalCommandProcessor::ScheduleHostSvcDowntime(double, const std::vector<String>& arguments)
@@ -941,6 +1043,9 @@ void ExternalCommandProcessor::ScheduleHostSvcDowntime(double, const std::vector
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule host services downtime for non-existent host '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[4]);
        if (triggeredByLegacy != 0)
@@ -962,6 +1067,9 @@ void ExternalCommandProcessor::ScheduleHostgroupHostDowntime(double, const std::
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule hostgroup host downtime for non-existent hostgroup '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[4]);
        if (triggeredByLegacy != 0)
@@ -969,7 +1077,7 @@ void ExternalCommandProcessor::ScheduleHostgroupHostDowntime(double, const std::
 
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
                Log(LogInformation, "icinga", "Creating downtime for host " + host->GetName());
-               Service::Ptr service = host->GetHostCheckService();
+               Service::Ptr service = host->GetCheckService();
                if (service) {
                        String comment_id = service->AddComment(CommentDowntime, arguments[6], arguments[7], Convert::ToDouble(arguments[2]));
                        (void) service->AddDowntime(comment_id,
@@ -986,6 +1094,9 @@ void ExternalCommandProcessor::ScheduleHostgroupSvcDowntime(double, const std::v
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule hostgroup service downtime for non-existent hostgroup '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[4]);
        if (triggeredByLegacy != 0)
@@ -1019,6 +1130,9 @@ void ExternalCommandProcessor::ScheduleServicegroupHostDowntime(double, const st
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule servicegroup host downtime for non-existent servicegroup '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[4]);
        if (triggeredByLegacy != 0)
@@ -1032,7 +1146,7 @@ void ExternalCommandProcessor::ScheduleServicegroupHostDowntime(double, const st
 
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Host::Ptr host = service->GetHost();
-               Service::Ptr hcService = host->GetHostCheckService();
+               Service::Ptr hcService = host->GetCheckService();
                if (hcService)
                        services.insert(hcService);
        }
@@ -1053,6 +1167,9 @@ void ExternalCommandProcessor::ScheduleServicegroupSvcDowntime(double, const std
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot schedule servicegroup service downtime for non-existent servicegroup '" + arguments[0] + "'"));
+
        String triggeredBy;
        int triggeredByLegacy = Convert::ToLong(arguments[4]);
        if (triggeredByLegacy != 0)
@@ -1074,8 +1191,11 @@ void ExternalCommandProcessor::AddHostComment(double, const std::vector<String>&
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot add host comment for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Creating comment for host " + host->GetName());
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service)
                (void) service->AddComment(CommentUser, arguments[2], arguments[3], 0);
 }
@@ -1098,6 +1218,9 @@ void ExternalCommandProcessor::AddSvcComment(double, const std::vector<String>&
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot add service comment for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Creating comment for service " + service->GetName());
        (void) service->AddComment(CommentUser, arguments[3], arguments[4], 0);
 }
@@ -1121,8 +1244,11 @@ void ExternalCommandProcessor::DelAllHostComments(double, const std::vector<Stri
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot delete all host comments for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Removing all comments for host " + host->GetName());
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service)
                service->RemoveAllComments();
 }
@@ -1134,6 +1260,9 @@ void ExternalCommandProcessor::DelAllSvcComments(double, const std::vector<Strin
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot delete all service comments for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Removing all comments for service " + service->GetName());
        service->RemoveAllComments();
 }
@@ -1144,10 +1273,14 @@ void ExternalCommandProcessor::SendCustomHostNotification(double, const std::vec
                BOOST_THROW_EXCEPTION(std::invalid_argument("Expected 4 arguments."));
 
        Host::Ptr host = Host::GetByName(arguments[0]);
+
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot send custom host notification for non-existent host '" + arguments[0] + "'"));
+
        int options = Convert::ToLong(arguments[1]);
 
        Log(LogInformation, "icinga", "Sending custom notification for host " + host->GetName());
-       Service::Ptr service = host->GetHostCheckService();
+       Service::Ptr service = host->GetCheckService();
        if (service) {
                if (options & 2) {
                        ObjectLock olock(service);
@@ -1164,6 +1297,10 @@ void ExternalCommandProcessor::SendCustomSvcNotification(double, const std::vect
                BOOST_THROW_EXCEPTION(std::invalid_argument("Expected 5 arguments."));
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
+
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot send custom service notification for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        int options = Convert::ToLong(arguments[2]);
 
        Log(LogInformation, "icinga", "Sending custom notification for service " + service->GetName());
@@ -1183,8 +1320,11 @@ void ExternalCommandProcessor::DelayHostNotification(double, const std::vector<S
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot delay host notification for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Delaying notifications for host " + host->GetName());
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
        if (!hc)
                return;
 
@@ -1202,6 +1342,9 @@ void ExternalCommandProcessor::DelaySvcNotification(double, const std::vector<St
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot delay service notification for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Delaying notifications for service " + service->GetName());
 
        BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
@@ -1218,8 +1361,11 @@ void ExternalCommandProcessor::EnableHostNotifications(double, const std::vector
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable host notifications for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling notifications for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -1238,8 +1384,11 @@ void ExternalCommandProcessor::DisableHostNotifications(double, const std::vecto
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable host notifications for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling notifications for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -1258,6 +1407,9 @@ void ExternalCommandProcessor::EnableSvcNotifications(double, const std::vector<
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable service notifications for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling notifications for service '" + arguments[1] + "'");
 
        {
@@ -1274,6 +1426,9 @@ void ExternalCommandProcessor::DisableSvcNotifications(double, const std::vector
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable service notifications for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling notifications for service '" + arguments[1] + "'");
 
        {
@@ -1290,8 +1445,11 @@ void ExternalCommandProcessor::DisableHostgroupHostChecks(double, const std::vec
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable hostgroup host checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1313,8 +1471,11 @@ void ExternalCommandProcessor::DisableHostgroupPassiveHostChecks(double, const s
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable hostgroup passive host checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1336,13 +1497,16 @@ void ExternalCommandProcessor::DisableServicegroupHostChecks(double, const std::
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable servicegroup host checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Host::Ptr host = service->GetHost();
 
                if (!host)
                        continue;
 
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1364,13 +1528,16 @@ void ExternalCommandProcessor::DisableServicegroupPassiveHostChecks(double, cons
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable servicegroup passive host checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Host::Ptr host = service->GetHost();
 
                if (!host)
                        continue;
 
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1392,8 +1559,11 @@ void ExternalCommandProcessor::EnableHostgroupHostChecks(double, const std::vect
 
        HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
 
+       if (!hg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable hostgroup host checks for non-existent hostgroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1422,13 +1592,16 @@ void ExternalCommandProcessor::EnableServicegroupHostChecks(double, const std::v
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable servicegroup host checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Host::Ptr host = service->GetHost();
 
                if (!host)
                        continue;
 
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1450,13 +1623,16 @@ void ExternalCommandProcessor::EnableServicegroupPassiveHostChecks(double, const
 
        ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
 
+       if (!sg)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable servicegroup passive host checks for non-existent servicegroup '" + arguments[0] + "'"));
+
        BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
                Host::Ptr host = service->GetHost();
 
                if (!host)
                        continue;
 
-               Service::Ptr hc = host->GetHostCheckService();
+               Service::Ptr hc = host->GetCheckService();
 
                if (!hc)
                        continue;
@@ -1478,8 +1654,11 @@ void ExternalCommandProcessor::EnableHostFlapping(double, const std::vector<Stri
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable host flapping for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling flapping detection for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -1498,8 +1677,11 @@ void ExternalCommandProcessor::DisableHostFlapping(double, const std::vector<Str
 
        Host::Ptr host = Host::GetByName(arguments[0]);
 
+       if (!host)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable host flapping for non-existent host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling flapping detection for host '" + arguments[0] + "'");
-       Service::Ptr hc = host->GetHostCheckService();
+       Service::Ptr hc = host->GetCheckService();
 
        if (!hc)
                return;
@@ -1518,6 +1700,9 @@ void ExternalCommandProcessor::EnableSvcFlapping(double, const std::vector<Strin
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot enable service flapping for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Enabling flapping detection for service '" + arguments[1] + "'");
 
        {
@@ -1534,6 +1719,9 @@ void ExternalCommandProcessor::DisableSvcFlapping(double, const std::vector<Stri
 
        Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
 
+       if (!service)
+               BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot disable service flapping for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'"));
+
        Log(LogInformation, "icinga", "Disabling flapping detection for service '" + arguments[1] + "'");
 
        {