Skip to content

Commit

Permalink
API: Display a correct status when removing a scheduled downtime
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 15, 2020
1 parent 04704a4 commit 4167624
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
16 changes: 11 additions & 5 deletions lib/icinga/apiactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,11 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
downtime->SetRemovedBy(author);
}

Downtime::RemoveDowntime(downtime->GetName(), true);
try {
Downtime::RemoveDowntime(downtime->GetName(), true);
} catch (std::runtime_error& error) {
return ApiActions::CreateResult(400, error.what());
}
}

return ApiActions::CreateResult(200, "Successfully removed all downtimes for object '" + checkable->GetName() + "'.");
Expand All @@ -504,11 +508,13 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
downtime->SetRemovedBy(author);
}

String downtimeName = downtime->GetName();

Downtime::RemoveDowntime(downtimeName, true);
try {
Downtime::RemoveDowntime(downtime->GetName(), true);

return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName + "'.");
return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtime->GetName() + "'.");
} catch (std::runtime_error& error) {
return ApiActions::CreateResult(400, error.what());
}
}

Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object,
Expand Down
11 changes: 8 additions & 3 deletions lib/icinga/downtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,14 @@ void Downtime::RemoveDowntime(const String& id, bool cancelled, bool expired, co
String config_owner = downtime->GetConfigOwner();

if (!config_owner.IsEmpty() && !expired) {
Log(LogWarning, "Downtime")
<< "Cannot remove downtime '" << downtime->GetName() << "'. It is owned by scheduled downtime object '" << config_owner << "'";
return;
{
String errorMessage = "Cannot remove downtime '" + downtime->GetName() +
"'. It is owned by scheduled downtime object '" + config_owner + "'";

Log(LogWarning, "Downtime") << errorMessage;

throw std::runtime_error(errorMessage);
}
}

downtime->SetWasCancelled(cancelled);
Expand Down
63 changes: 51 additions & 12 deletions lib/icinga/externalcommandprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using namespace icinga;

bool ExternalCommandProcessor::m_DownTimeRemoved;
boost::signals2::signal<void(double, const String&, const std::vector<String>&)> ExternalCommandProcessor::OnNewExternalCommand;

void ExternalCommandProcessor::Execute(const String& line)
Expand Down Expand Up @@ -970,10 +971,19 @@ void ExternalCommandProcessor::ScheduleSvcDowntime(double, const std::vector<Str
void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>& arguments)
{
int id = Convert::ToLong(arguments[0]);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime ID " << arguments[0];
m_DownTimeRemoved = false;
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
Downtime::RemoveDowntime(rid, true);

try {
Downtime::RemoveDowntime(rid, true);
} catch (std::runtime_error& error) {
m_DownTimeRemoved = true;
Log(LogWarning, error.what());
}

if (!m_DownTimeRemoved)
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime ID " << arguments[0];
}

void ExternalCommandProcessor::ScheduleHostDowntime(double, const std::vector<String>& arguments)
Expand Down Expand Up @@ -1072,10 +1082,19 @@ void ExternalCommandProcessor::ScheduleAndPropagateTriggeredHostDowntime(double,
void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>& arguments)
{
int id = Convert::ToLong(arguments[0]);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime ID " << arguments[0];
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
Downtime::RemoveDowntime(rid, true);
m_DownTimeRemoved = false;

try {
Downtime::RemoveDowntime(rid, true);
} catch (std::runtime_error& error) {
m_DownTimeRemoved = true;
Log(LogWarning, error.what());
}

if (!m_DownTimeRemoved)
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime ID " << arguments[0];
}

void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<String>& arguments)
Expand All @@ -1101,13 +1120,24 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
Log(LogWarning, "ExternalCommandProcessor")
<< ("Ignoring additional parameters for host '" + arguments[0] + "' downtime deletion.");

m_DownTimeRemoved = false;
String downTimeName;
for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime '" << downtime->GetName() << "'.";
downTimeName = downtime->GetName();

Downtime::RemoveDowntime(downtime->GetName(), true);
try {
Downtime::RemoveDowntime(downtime->GetName(), true);
} catch (std::runtime_error& error) {
m_DownTimeRemoved = true;
Log(LogWarning, error.what());
}

if (!m_DownTimeRemoved)
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime '" << downTimeName << "'.";
}

m_DownTimeRemoved = false;
for (const Service::Ptr& service : host->GetServices()) {
if (!serviceName.IsEmpty() && serviceName != service->GetName())
continue;
Expand All @@ -1119,10 +1149,19 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
if (!commentString.IsEmpty() && downtime->GetComment() != commentString)
continue;

Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime '" << downtime->GetName() << "'.";
downTimeName = downtime->GetName();

Downtime::RemoveDowntime(downtime->GetName(), true);
try {
Downtime::RemoveDowntime(downtime->GetName(), true);
} catch (std::runtime_error& error) {
m_DownTimeRemoved = true;
Log(LogWarning, error.what());
}

if (!m_DownTimeRemoved) {
Log(LogNotice, "ExternalCommandProcessor")
<< "Removing downtime '" << downtime->GetName() << "'.";
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/icinga/externalcommandprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class ExternalCommandProcessor {

static boost::mutex& GetMutex();
static std::map<String, ExternalCommandInfo>& GetCommands();

static bool m_DownTimeRemoved;
};

}
Expand Down

0 comments on commit 4167624

Please sign in to comment.