2015-07-30 17:50:17 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2015-07-30 17:50:17 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "icinga/apiactions.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
2015-08-03 15:27:09 +02:00
|
|
|
#include "icinga/servicegroup.hpp"
|
|
|
|
#include "icinga/hostgroup.hpp"
|
2015-07-31 17:18:49 +02:00
|
|
|
#include "icinga/pluginutility.hpp"
|
2015-08-03 15:27:09 +02:00
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "icinga/eventcommand.hpp"
|
|
|
|
#include "icinga/notificationcommand.hpp"
|
2015-07-30 17:50:17 +02:00
|
|
|
#include "remote/apiaction.hpp"
|
2016-08-15 14:32:41 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2015-07-30 17:50:17 +02:00
|
|
|
#include "remote/httputility.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2015-08-21 15:50:40 +02:00
|
|
|
#include <fstream>
|
2015-07-30 17:50:17 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-03 15:27:09 +02:00
|
|
|
REGISTER_APIACTION(process_check_result, "Service;Host", &ApiActions::ProcessCheckResult);
|
|
|
|
REGISTER_APIACTION(reschedule_check, "Service;Host", &ApiActions::RescheduleCheck);
|
2015-10-16 11:44:18 +02:00
|
|
|
REGISTER_APIACTION(send_custom_notification, "Service;Host", &ApiActions::SendCustomNotification);
|
|
|
|
REGISTER_APIACTION(delay_notification, "Service;Host", &ApiActions::DelayNotification);
|
2015-08-03 15:27:09 +02:00
|
|
|
REGISTER_APIACTION(acknowledge_problem, "Service;Host", &ApiActions::AcknowledgeProblem);
|
|
|
|
REGISTER_APIACTION(remove_acknowledgement, "Service;Host", &ApiActions::RemoveAcknowledgement);
|
|
|
|
REGISTER_APIACTION(add_comment, "Service;Host", &ApiActions::AddComment);
|
2015-11-08 12:41:47 +01:00
|
|
|
REGISTER_APIACTION(remove_comment, "Service;Host;Comment", &ApiActions::RemoveComment);
|
2015-08-21 12:47:49 +02:00
|
|
|
REGISTER_APIACTION(schedule_downtime, "Service;Host", &ApiActions::ScheduleDowntime);
|
2015-11-08 12:41:47 +01:00
|
|
|
REGISTER_APIACTION(remove_downtime, "Service;Host;Downtime", &ApiActions::RemoveDowntime);
|
2015-08-03 15:27:09 +02:00
|
|
|
REGISTER_APIACTION(shutdown_process, "", &ApiActions::ShutdownProcess);
|
|
|
|
REGISTER_APIACTION(restart_process, "", &ApiActions::RestartProcess);
|
2016-08-15 14:32:41 +02:00
|
|
|
REGISTER_APIACTION(generate_ticket, "", &ApiActions::GenerateTicket);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::CreateResult(int code, const String& status,
|
|
|
|
const Dictionary::Ptr& additional)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
2015-07-31 17:18:49 +02:00
|
|
|
Dictionary::Ptr result = new Dictionary();
|
|
|
|
result->Set("code", code);
|
|
|
|
result->Set("status", status);
|
2015-08-21 12:47:49 +02:00
|
|
|
|
|
|
|
if (additional)
|
|
|
|
additional->CopyTo(result);
|
|
|
|
|
2015-07-31 17:18:49 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::ProcessCheckResult(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-07-31 17:18:49 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(404,
|
|
|
|
"Cannot process passive check result for non-existent object.");
|
2015-07-31 17:18:49 +02:00
|
|
|
|
|
|
|
if (!checkable->GetEnablePassiveChecks())
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(403, "Passive checks are disabled for object '" + checkable->GetName() + "'.");
|
2015-07-31 17:18:49 +02:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
if (!params->Contains("exit_status"))
|
2015-08-21 12:47:49 +02:00
|
|
|
return ApiActions::CreateResult(403, "Parameter 'exit_status' is required.");
|
2015-07-31 17:18:49 +02:00
|
|
|
|
|
|
|
int exitStatus = HttpUtility::GetLastParameter(params, "exit_status");
|
|
|
|
|
|
|
|
ServiceState state;
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
if (exitStatus == 0)
|
|
|
|
state = ServiceOK;
|
|
|
|
else if (exitStatus == 1)
|
|
|
|
state = ServiceCritical;
|
|
|
|
else
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(403, "Invalid 'exit_status' for Host "
|
|
|
|
+ checkable->GetName() + ".");
|
2015-07-31 17:18:49 +02:00
|
|
|
} else {
|
|
|
|
state = PluginUtility::ExitStatusToState(exitStatus);
|
|
|
|
}
|
|
|
|
|
2015-08-03 15:27:09 +02:00
|
|
|
if (!params->Contains("plugin_output"))
|
|
|
|
return ApiActions::CreateResult(403, "Parameter 'plugin_output' is required");
|
2015-07-31 17:18:49 +02:00
|
|
|
|
|
|
|
CheckResult::Ptr cr = new CheckResult();
|
2015-08-03 15:27:09 +02:00
|
|
|
cr->SetOutput(HttpUtility::GetLastParameter(params, "plugin_output"));
|
2015-07-31 17:18:49 +02:00
|
|
|
cr->SetState(state);
|
|
|
|
|
|
|
|
cr->SetCheckSource(HttpUtility::GetLastParameter(params, "check_source"));
|
2015-08-03 15:27:09 +02:00
|
|
|
cr->SetPerformanceData(params->Get("performance_data"));
|
|
|
|
cr->SetCommand(params->Get("check_command"));
|
2016-03-11 10:30:50 +01:00
|
|
|
|
|
|
|
/* Mark this check result as passive. */
|
|
|
|
cr->SetActive(false);
|
|
|
|
|
2015-07-31 17:18:49 +02:00
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Successfully processed check result for object '" + checkable->GetName() + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::RescheduleCheck(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(404, "Cannot reschedule check for non-existent object.");
|
|
|
|
|
|
|
|
if (Convert::ToBool(HttpUtility::GetLastParameter(params, "force")))
|
|
|
|
checkable->SetForceNextCheck(true);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
double nextCheck;
|
|
|
|
if (params->Contains("next_check"))
|
|
|
|
nextCheck = HttpUtility::GetLastParameter(params, "next_check");
|
|
|
|
else
|
|
|
|
nextCheck = Utility::GetTime();
|
|
|
|
|
|
|
|
checkable->SetNextCheck(nextCheck);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2016-01-22 18:42:15 +01:00
|
|
|
/* trigger update event for DB IDO */
|
|
|
|
Checkable::OnNextCheckUpdated(checkable);
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Successfully rescheduled check for object '" + checkable->GetName() + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::SendCustomNotification(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(404, "Cannot send notification for non-existent object.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
if (!params->Contains("author"))
|
|
|
|
return ApiActions::CreateResult(403, "Parameter 'author' is required.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
if (!params->Contains("comment"))
|
|
|
|
return ApiActions::CreateResult(403, "Parameter 'comment' is required.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
if (Convert::ToBool(HttpUtility::GetLastParameter(params, "force")))
|
|
|
|
checkable->SetForceNextNotification(true);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Checkable::OnNotificationsRequested(checkable, NotificationCustom, checkable->GetLastCheckResult(),
|
2016-06-07 12:44:12 +02:00
|
|
|
HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "comment"), MessageOrigin::Ptr());
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Successfully sent custom notification for object '" + checkable->GetName() + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::DelayNotification(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
2015-08-21 12:47:49 +02:00
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-08-21 12:47:49 +02:00
|
|
|
if (!checkable)
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(404, "Cannot delay notifications for non-existent object");
|
|
|
|
|
|
|
|
if (!params->Contains("timestamp"))
|
|
|
|
return ApiActions::CreateResult(403, "A timestamp is required to delay notifications");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Notification::Ptr& notification : checkable->GetNotifications()) {
|
2015-10-16 11:44:18 +02:00
|
|
|
notification->SetNextNotification(HttpUtility::GetLastParameter(params, "timestamp"));
|
|
|
|
}
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Successfully delayed notifications for object '" + checkable->GetName() + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::AcknowledgeProblem(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
2015-08-21 12:47:49 +02:00
|
|
|
return ApiActions::CreateResult(404, "Cannot acknowledge problem for non-existent object.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
|
|
|
if (!params->Contains("author") || !params->Contains("comment"))
|
2015-08-21 12:47:49 +02:00
|
|
|
return ApiActions::CreateResult(403, "Acknowledgements require author and comment.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
|
|
|
AcknowledgementType sticky = AcknowledgementNormal;
|
|
|
|
bool notify = false;
|
2015-08-21 12:47:49 +02:00
|
|
|
double timestamp = 0.0;
|
2015-10-16 11:44:18 +02:00
|
|
|
|
2017-01-12 11:43:22 +01:00
|
|
|
if (params->Contains("sticky") && HttpUtility::GetLastParameter(params, "sticky"))
|
2015-08-03 15:27:09 +02:00
|
|
|
sticky = AcknowledgementSticky;
|
|
|
|
if (params->Contains("notify"))
|
2017-01-12 11:43:22 +01:00
|
|
|
notify = HttpUtility::GetLastParameter(params, "notify");
|
2015-10-16 11:44:18 +02:00
|
|
|
if (params->Contains("expiry"))
|
|
|
|
timestamp = HttpUtility::GetLastParameter(params, "expiry");
|
|
|
|
else
|
|
|
|
timestamp = 0;
|
2015-08-03 15:27:09 +02:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2015-07-31 17:18:49 +02:00
|
|
|
if (!service) {
|
2015-08-03 15:27:09 +02:00
|
|
|
if (host->GetState() == HostUp)
|
2015-08-21 12:47:49 +02:00
|
|
|
return ApiActions::CreateResult(409, "Host " + checkable->GetName() + " is UP.");
|
2015-08-03 15:27:09 +02:00
|
|
|
} else {
|
|
|
|
if (service->GetState() == ServiceOK)
|
2015-08-21 12:47:49 +02:00
|
|
|
return ApiActions::CreateResult(409, "Service " + checkable->GetName() + " is OK.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
Comment::AddComment(checkable, CommentAcknowledgement, HttpUtility::GetLastParameter(params, "author"),
|
2015-08-03 15:27:09 +02:00
|
|
|
HttpUtility::GetLastParameter(params, "comment"), timestamp);
|
|
|
|
checkable->AcknowledgeProblem(HttpUtility::GetLastParameter(params, "author"),
|
|
|
|
HttpUtility::GetLastParameter(params, "comment"), sticky, notify, timestamp);
|
2015-08-21 12:47:49 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Successfully acknowledged problem for object '" + checkable->GetName() + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::RemoveAcknowledgement(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(404,
|
|
|
|
"Cannot remove acknowlegement for non-existent checkable object "
|
|
|
|
+ object->GetName() + ".");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
|
|
|
checkable->ClearAcknowledgement();
|
|
|
|
checkable->RemoveCommentsByType(CommentAcknowledgement);
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Successfully removed acknowledgement for object '" + checkable->GetName() + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
|
|
|
return ApiActions::CreateResult(404, "Cannot add comment for non-existent object");
|
|
|
|
|
|
|
|
if (!params->Contains("author") || !params->Contains("comment"))
|
2015-08-21 12:47:49 +02:00
|
|
|
return ApiActions::CreateResult(403, "Comments require author and comment.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
String commentName = Comment::AddComment(checkable, CommentUser,
|
2015-10-16 11:44:18 +02:00
|
|
|
HttpUtility::GetLastParameter(params, "author"),
|
2015-08-03 15:27:09 +02:00
|
|
|
HttpUtility::GetLastParameter(params, "comment"), 0);
|
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Comment::Ptr comment = Comment::GetByName(commentName);
|
2015-08-21 12:47:49 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr additional = new Dictionary();
|
2015-11-08 12:41:47 +01:00
|
|
|
additional->Set("name", commentName);
|
|
|
|
additional->Set("legacy_id", comment->GetLegacyId());
|
2015-08-21 12:47:49 +02:00
|
|
|
|
2015-11-06 16:45:09 +01:00
|
|
|
return ApiActions::CreateResult(200, "Successfully added comment '"
|
2015-11-08 12:41:47 +01:00
|
|
|
+ commentName + "' for object '" + checkable->GetName()
|
2015-10-16 11:44:18 +02:00
|
|
|
+ "'.", additional);
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Dictionary::Ptr ApiActions::RemoveComment(const ConfigObject::Ptr& object,
|
2015-10-16 11:44:18 +02:00
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
2015-11-08 12:41:47 +01:00
|
|
|
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
if (checkable) {
|
|
|
|
std::set<Comment::Ptr> comments = checkable->GetComments();
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Comment::Ptr& comment : comments) {
|
2015-11-08 12:41:47 +01:00
|
|
|
Comment::RemoveComment(comment->GetName());
|
|
|
|
}
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
return ApiActions::CreateResult(200, "Successfully removed all comments for object '" + checkable->GetName() + "'.");
|
|
|
|
}
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Comment::Ptr comment = static_pointer_cast<Comment>(object);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
if (!comment)
|
|
|
|
return ApiActions::CreateResult(404, "Cannot remove non-existent comment object.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
String commentName = comment->GetName();
|
2015-08-21 12:47:49 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Comment::RemoveComment(commentName);
|
2015-11-01 16:33:43 +01:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
return ApiActions::CreateResult(200, "Successfully removed comment '" + commentName + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::ScheduleDowntime(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (!checkable)
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(404, "Can't schedule downtime for non-existent object.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
if (!params->Contains("start_time") || !params->Contains("end_time") ||
|
2016-08-16 17:08:41 +02:00
|
|
|
!params->Contains("author") || !params->Contains("comment")) {
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2016-08-16 17:08:41 +02:00
|
|
|
return ApiActions::CreateResult(404, "Options 'start_time', 'end_time', 'author' and 'comment' are required");
|
2015-10-16 11:44:18 +02:00
|
|
|
}
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2016-02-08 16:04:41 +01:00
|
|
|
bool fixed = true;
|
2015-08-03 15:27:09 +02:00
|
|
|
if (params->Contains("fixed"))
|
2015-08-21 12:47:49 +02:00
|
|
|
fixed = HttpUtility::GetLastParameter(params, "fixed");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2016-08-16 17:08:41 +02:00
|
|
|
if (!fixed && !params->Contains("duration"))
|
|
|
|
return ApiActions::CreateResult(404, "Option 'duration' is required for flexible downtime");
|
|
|
|
|
2016-10-05 17:36:43 +02:00
|
|
|
double duration = 0.0;
|
|
|
|
if (params->Contains("duration"))
|
|
|
|
duration = HttpUtility::GetLastParameter(params, "duration");
|
|
|
|
|
|
|
|
String triggerName;
|
|
|
|
if (params->Contains("trigger_name"))
|
|
|
|
triggerName = HttpUtility::GetLastParameter(params, "trigger_name");
|
|
|
|
|
|
|
|
String author = HttpUtility::GetLastParameter(params, "author");
|
|
|
|
String comment = HttpUtility::GetLastParameter(params, "comment");
|
|
|
|
double startTime = HttpUtility::GetLastParameter(params, "start_time");
|
|
|
|
double endTime = HttpUtility::GetLastParameter(params, "end_time");
|
|
|
|
|
|
|
|
String downtimeName = Downtime::AddDowntime(checkable, author, comment, startTime, endTime,
|
|
|
|
fixed, triggerName, duration);
|
2015-08-21 12:47:49 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Downtime::Ptr downtime = Downtime::GetByName(downtimeName);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-08-21 12:47:49 +02:00
|
|
|
Dictionary::Ptr additional = new Dictionary();
|
2015-11-08 12:41:47 +01:00
|
|
|
additional->Set("name", downtimeName);
|
|
|
|
additional->Set("legacy_id", downtime->GetLegacyId());
|
2015-08-21 12:47:49 +02:00
|
|
|
|
2016-10-05 17:36:43 +02:00
|
|
|
/* Schedule downtime for all child objects. */
|
|
|
|
int childOptions = 0;
|
|
|
|
if (params->Contains("child_options"))
|
|
|
|
childOptions = HttpUtility::GetLastParameter(params, "child_options");
|
|
|
|
|
|
|
|
if (childOptions > 0) {
|
|
|
|
/* '1' schedules child downtimes triggered by the parent downtime.
|
|
|
|
* '2' schedules non-triggered downtimes for all children.
|
|
|
|
*/
|
|
|
|
if (childOptions == 1)
|
|
|
|
triggerName = downtimeName;
|
|
|
|
|
|
|
|
Array::Ptr childDowntimes = new Array();
|
|
|
|
|
|
|
|
Log(LogCritical, "ApiActions")
|
|
|
|
<< "Processing child options " << childOptions << " for downtime " << downtimeName;
|
|
|
|
|
|
|
|
for (const Checkable::Ptr& child : checkable->GetAllChildren()) {
|
|
|
|
Log(LogCritical, "ApiActions")
|
|
|
|
<< "Scheduling downtime for child object " << child->GetName();
|
|
|
|
|
|
|
|
String childDowntimeName = Downtime::AddDowntime(child, author, comment, startTime, endTime,
|
|
|
|
fixed, triggerName, duration);
|
|
|
|
|
|
|
|
Log(LogCritical, "ApiActions")
|
|
|
|
<< "Add child downtime '" << childDowntimeName << "'.";
|
|
|
|
|
|
|
|
Downtime::Ptr childDowntime = Downtime::GetByName(childDowntimeName);
|
|
|
|
|
|
|
|
Dictionary::Ptr additionalChild = new Dictionary();
|
|
|
|
additionalChild->Set("name", childDowntimeName);
|
|
|
|
additionalChild->Set("legacy_id", childDowntime->GetLegacyId());
|
|
|
|
childDowntimes->Add(additionalChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
additional->Set("child_downtimes", childDowntimes);
|
|
|
|
}
|
|
|
|
|
2015-11-06 16:45:09 +01:00
|
|
|
return ApiActions::CreateResult(200, "Successfully scheduled downtime '" +
|
2015-11-08 12:41:47 +01:00
|
|
|
downtimeName + "' for object '" + checkable->GetName() + "'.", additional);
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
|
2015-10-16 11:44:18 +02:00
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
2015-11-08 12:41:47 +01:00
|
|
|
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
if (checkable) {
|
|
|
|
std::set<Downtime::Ptr> downtimes = checkable->GetDowntimes();
|
2015-08-21 15:50:40 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Downtime::Ptr& downtime : downtimes) {
|
2015-11-08 12:41:47 +01:00
|
|
|
Downtime::RemoveDowntime(downtime->GetName(), true);
|
|
|
|
}
|
2015-08-21 15:50:40 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
return ApiActions::CreateResult(200, "Successfully removed all downtimes for object '" + checkable->GetName() + "'.");
|
|
|
|
}
|
2015-08-21 15:50:40 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Downtime::Ptr downtime = static_pointer_cast<Downtime>(object);
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
if (!downtime)
|
|
|
|
return ApiActions::CreateResult(404, "Cannot remove non-existent downtime object.");
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
String downtimeName = downtime->GetName();
|
2015-08-03 15:27:09 +02:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
Downtime::RemoveDowntime(downtimeName, true);
|
2015-11-01 16:33:43 +01:00
|
|
|
|
2015-11-08 12:41:47 +01:00
|
|
|
return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName + "'.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::ShutdownProcess(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Application::RequestShutdown();
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Shutting down Icinga 2.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
Dictionary::Ptr ApiActions::RestartProcess(const ConfigObject::Ptr& object,
|
|
|
|
const Dictionary::Ptr& params)
|
2015-08-03 15:27:09 +02:00
|
|
|
{
|
|
|
|
Application::RequestRestart();
|
|
|
|
|
2015-10-16 11:44:18 +02:00
|
|
|
return ApiActions::CreateResult(200, "Restarting Icinga 2.");
|
2015-08-03 15:27:09 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 14:32:41 +02:00
|
|
|
Dictionary::Ptr ApiActions::GenerateTicket(const ConfigObject::Ptr&,
|
|
|
|
const Dictionary::Ptr& params)
|
|
|
|
{
|
|
|
|
if (!params->Contains("cn"))
|
|
|
|
return ApiActions::CreateResult(404, "Option 'cn' is required");
|
|
|
|
|
|
|
|
String cn = HttpUtility::GetLastParameter(params, "cn");
|
|
|
|
|
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
String salt = listener->GetTicketSalt();
|
|
|
|
|
|
|
|
if (salt.IsEmpty())
|
|
|
|
return ApiActions::CreateResult(500, "Ticket salt is not configured in ApiListener object");
|
|
|
|
|
|
|
|
String ticket = PBKDF2_SHA1(cn, salt, 50000);
|
|
|
|
|
|
|
|
Dictionary::Ptr additional = new Dictionary();
|
|
|
|
additional->Set("ticket", ticket);
|
|
|
|
|
|
|
|
return ApiActions::CreateResult(200, "Generated PKI ticket '" + ticket + "' for common name '"
|
|
|
|
+ cn + "'.", additional);
|
|
|
|
}
|