Skip to content

Commit 8643cbc

Browse files
committed
API: Display a correct status when removing a scheduled downtime
1 parent 04704a4 commit 8643cbc

File tree

4 files changed

+71
-21
lines changed

4 files changed

+71
-21
lines changed

lib/icinga/apiactions.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,11 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
488488
downtime->SetRemovedBy(author);
489489
}
490490

491-
Downtime::RemoveDowntime(downtime->GetName(), true);
491+
try {
492+
Downtime::RemoveDowntime(downtime->GetName(), true);
493+
} catch (std::runtime_error& error) {
494+
return ApiActions::CreateResult(400, error.what());
495+
}
492496
}
493497

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

507-
String downtimeName = downtime->GetName();
508-
509-
Downtime::RemoveDowntime(downtimeName, true);
511+
try {
512+
Downtime::RemoveDowntime(downtime->GetName(), true);
510513

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

514520
Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object,

lib/icinga/downtime.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,14 @@ void Downtime::RemoveDowntime(const String& id, bool cancelled, bool expired, co
317317
String config_owner = downtime->GetConfigOwner();
318318

319319
if (!config_owner.IsEmpty() && !expired) {
320-
Log(LogWarning, "Downtime")
321-
<< "Cannot remove downtime '" << downtime->GetName() << "'. It is owned by scheduled downtime object '" << config_owner << "'";
322-
return;
320+
{
321+
String errorMessage = "Cannot remove downtime '" + downtime->GetName() +
322+
"'. It is owned by scheduled downtime object '" + config_owner + "'";
323+
Log(LogWarning, "Downtime")
324+
<< errorMessage;
325+
326+
throw std::runtime_error(errorMessage);
327+
}
323328
}
324329

325330
downtime->SetWasCancelled(cancelled);

lib/icinga/externalcommandprocessor.cpp

+51-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
using namespace icinga;
2626

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

2930
void ExternalCommandProcessor::Execute(const String& line)
@@ -970,10 +971,19 @@ void ExternalCommandProcessor::ScheduleSvcDowntime(double, const std::vector<Str
970971
void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>& arguments)
971972
{
972973
int id = Convert::ToLong(arguments[0]);
973-
Log(LogNotice, "ExternalCommandProcessor")
974-
<< "Removing downtime ID " << arguments[0];
974+
m_DownTimeRemoved = false;
975975
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
976-
Downtime::RemoveDowntime(rid, true);
976+
977+
try {
978+
Downtime::RemoveDowntime(rid, true);
979+
} catch (std::runtime_error& error) {
980+
m_DownTimeRemoved = true;
981+
Log(LogWarning, error.what());
982+
}
983+
984+
if (!m_DownTimeRemoved)
985+
Log(LogNotice, "ExternalCommandProcessor")
986+
<< "Removing downtime ID " << arguments[0];
977987
}
978988

979989
void ExternalCommandProcessor::ScheduleHostDowntime(double, const std::vector<String>& arguments)
@@ -1072,10 +1082,19 @@ void ExternalCommandProcessor::ScheduleAndPropagateTriggeredHostDowntime(double,
10721082
void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>& arguments)
10731083
{
10741084
int id = Convert::ToLong(arguments[0]);
1075-
Log(LogNotice, "ExternalCommandProcessor")
1076-
<< "Removing downtime ID " << arguments[0];
10771085
String rid = Downtime::GetDowntimeIDFromLegacyID(id);
1078-
Downtime::RemoveDowntime(rid, true);
1086+
m_DownTimeRemoved = false;
1087+
1088+
try {
1089+
Downtime::RemoveDowntime(rid, true);
1090+
} catch (std::runtime_error& error) {
1091+
m_DownTimeRemoved = true;
1092+
Log(LogWarning, error.what());
1093+
}
1094+
1095+
if (!m_DownTimeRemoved)
1096+
Log(LogNotice, "ExternalCommandProcessor")
1097+
<< "Removing downtime ID " << arguments[0];
10791098
}
10801099

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

1123+
m_DownTimeRemoved = false;
1124+
String downTimeName;
11041125
for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
1105-
Log(LogNotice, "ExternalCommandProcessor")
1106-
<< "Removing downtime '" << downtime->GetName() << "'.";
1126+
downTimeName = downtime->GetName();
11071127

1108-
Downtime::RemoveDowntime(downtime->GetName(), true);
1128+
try {
1129+
Downtime::RemoveDowntime(downtime->GetName(), true);
1130+
} catch (std::runtime_error& error) {
1131+
m_DownTimeRemoved = true;
1132+
Log(LogWarning, error.what());
1133+
}
1134+
1135+
if (!m_DownTimeRemoved)
1136+
Log(LogNotice, "ExternalCommandProcessor")
1137+
<< "Removing downtime '" << downTimeName << "'.";
11091138
}
11101139

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

1122-
Log(LogNotice, "ExternalCommandProcessor")
1123-
<< "Removing downtime '" << downtime->GetName() << "'.";
1152+
downTimeName = downtime->GetName();
11241153

1125-
Downtime::RemoveDowntime(downtime->GetName(), true);
1154+
try {
1155+
Downtime::RemoveDowntime(downtime->GetName(), true);
1156+
} catch (std::runtime_error& error) {
1157+
m_DownTimeRemoved = true;
1158+
Log(LogWarning, error.what());
1159+
}
1160+
1161+
if (!m_DownTimeRemoved) {
1162+
Log(LogNotice, "ExternalCommandProcessor")
1163+
<< "Removing downtime '" << downtime->GetName() << "'.";
1164+
}
11261165
}
11271166
}
11281167
}

lib/icinga/externalcommandprocessor.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ExternalCommandProcessor {
161161

162162
static boost::mutex& GetMutex();
163163
static std::map<String, ExternalCommandInfo>& GetCommands();
164-
164+
static bool m_DownTimeRemoved;
165165
};
166166

167167
}

0 commit comments

Comments
 (0)