1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/clusterevents.hpp"
21 #include "icinga/service.hpp"
22 #include "icinga/perfdatavalue.hpp"
23 #include "remote/apilistener.hpp"
24 #include "remote/endpoint.hpp"
25 #include "remote/messageorigin.hpp"
26 #include "remote/zone.hpp"
27 #include "remote/apifunction.hpp"
28 #include "remote/eventqueue.hpp"
29 #include "base/application.hpp"
30 #include "base/configtype.hpp"
31 #include "base/utility.hpp"
32 #include "base/exception.hpp"
33 #include "base/initialize.hpp"
34 #include "base/serializer.hpp"
35 #include "base/json.hpp"
38 using namespace icinga;
40 INITIALIZE_ONCE(&ClusterEvents::StaticInitialize);
42 REGISTER_APIFUNCTION(CheckResult, event, &ClusterEvents::CheckResultAPIHandler);
43 REGISTER_APIFUNCTION(SetNextCheck, event, &ClusterEvents::NextCheckChangedAPIHandler);
44 REGISTER_APIFUNCTION(SetNextNotification, event, &ClusterEvents::NextNotificationChangedAPIHandler);
45 REGISTER_APIFUNCTION(SetForceNextCheck, event, &ClusterEvents::ForceNextCheckChangedAPIHandler);
46 REGISTER_APIFUNCTION(SetForceNextNotification, event, &ClusterEvents::ForceNextNotificationChangedAPIHandler);
47 REGISTER_APIFUNCTION(SetAcknowledgement, event, &ClusterEvents::AcknowledgementSetAPIHandler);
48 REGISTER_APIFUNCTION(ClearAcknowledgement, event, &ClusterEvents::AcknowledgementClearedAPIHandler);
49 REGISTER_APIFUNCTION(UpdateRepository, event, &ClusterEvents::UpdateRepositoryAPIHandler);
50 REGISTER_APIFUNCTION(ExecuteCommand, event, &ClusterEvents::ExecuteCommandAPIHandler);
51 REGISTER_APIFUNCTION(SendNotifications, event, &ClusterEvents::SendNotificationsAPIHandler);
52 REGISTER_APIFUNCTION(NotificationSentUser, event, &ClusterEvents::NotificationSentUserAPIHandler);
53 REGISTER_APIFUNCTION(NotificationSentToAllUsers, event, &ClusterEvents::NotificationSentToAllUsersAPIHandler);
55 static Timer::Ptr l_RepositoryTimer;
57 void ClusterEvents::StaticInitialize(void)
59 Checkable::OnNewCheckResult.connect(&ClusterEvents::CheckResultHandler);
60 Checkable::OnNextCheckChanged.connect(&ClusterEvents::NextCheckChangedHandler);
61 Notification::OnNextNotificationChanged.connect(&ClusterEvents::NextNotificationChangedHandler);
62 Checkable::OnForceNextCheckChanged.connect(&ClusterEvents::ForceNextCheckChangedHandler);
63 Checkable::OnForceNextNotificationChanged.connect(&ClusterEvents::ForceNextNotificationChangedHandler);
64 Checkable::OnNotificationsRequested.connect(&ClusterEvents::SendNotificationsHandler);
65 Checkable::OnNotificationSentToUser.connect(&ClusterEvents::NotificationSentUserHandler);
66 Checkable::OnNotificationSentToAllUsers.connect(&ClusterEvents::NotificationSentToAllUsersHandler);
68 Checkable::OnAcknowledgementSet.connect(&ClusterEvents::AcknowledgementSetHandler);
69 Checkable::OnAcknowledgementCleared.connect(&ClusterEvents::AcknowledgementClearedHandler);
71 l_RepositoryTimer = new Timer();
72 l_RepositoryTimer->SetInterval(30);
73 l_RepositoryTimer->OnTimerExpired.connect(boost::bind(&ClusterEvents::RepositoryTimerHandler));
74 l_RepositoryTimer->Start();
75 l_RepositoryTimer->Reschedule(0);
78 Dictionary::Ptr ClusterEvents::MakeCheckResultMessage(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
80 Dictionary::Ptr message = new Dictionary();
81 message->Set("jsonrpc", "2.0");
82 message->Set("method", "event::CheckResult");
86 tie(host, service) = GetHostService(checkable);
88 Dictionary::Ptr params = new Dictionary();
89 params->Set("host", host->GetName());
91 params->Set("service", service->GetShortName());
93 Value agent_service_name = checkable->GetExtension("agent_service_name");
95 if (!agent_service_name.IsEmpty())
96 params->Set("service", agent_service_name);
98 params->Set("cr", Serialize(cr));
100 message->Set("params", params);
105 void ClusterEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
107 ApiListener::Ptr listener = ApiListener::GetInstance();
112 Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
113 listener->RelayMessage(origin, checkable, message, true);
116 Value ClusterEvents::CheckResultAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
118 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
121 Log(LogNotice, "ClusterEvents")
122 << "Discarding 'check result' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
132 if (params->Contains("cr")) {
133 cr = new CheckResult();
134 Dictionary::Ptr vcr = params->Get("cr");
136 if (vcr && vcr->Contains("performance_data")) {
137 vperf = vcr->Get("performance_data");
140 vcr->Remove("performance_data");
142 Deserialize(cr, vcr, true);
149 Array::Ptr rperf = new Array();
152 ObjectLock olock(vperf);
153 for (const Value& vp : vperf) {
156 if (vp.IsObjectType<Dictionary>()) {
157 PerfdataValue::Ptr val = new PerfdataValue();
158 Deserialize(val, vp, true);
165 cr->SetPerformanceData(rperf);
167 Host::Ptr host = Host::GetByName(params->Get("host"));
172 Checkable::Ptr checkable;
174 if (params->Contains("service"))
175 checkable = host->GetServiceByShortName(params->Get("service"));
182 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable) && endpoint != checkable->GetCommandEndpoint()) {
183 Log(LogNotice, "ClusterEvents")
184 << "Discarding 'check result' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
188 if (!checkable->IsPaused() && Zone::GetLocalZone() == checkable->GetZone() && endpoint == checkable->GetCommandEndpoint())
189 checkable->ProcessCheckResult(cr);
191 checkable->ProcessCheckResult(cr, origin);
196 void ClusterEvents::NextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
198 ApiListener::Ptr listener = ApiListener::GetInstance();
204 Service::Ptr service;
205 tie(host, service) = GetHostService(checkable);
207 Dictionary::Ptr params = new Dictionary();
208 params->Set("host", host->GetName());
210 params->Set("service", service->GetShortName());
211 params->Set("next_check", checkable->GetNextCheck());
213 Dictionary::Ptr message = new Dictionary();
214 message->Set("jsonrpc", "2.0");
215 message->Set("method", "event::SetNextCheck");
216 message->Set("params", params);
218 listener->RelayMessage(origin, checkable, message, true);
221 Value ClusterEvents::NextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
223 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
226 Log(LogNotice, "ClusterEvents")
227 << "Discarding 'next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
234 Host::Ptr host = Host::GetByName(params->Get("host"));
239 Checkable::Ptr checkable;
241 if (params->Contains("service"))
242 checkable = host->GetServiceByShortName(params->Get("service"));
249 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
250 Log(LogNotice, "ClusterEvents")
251 << "Discarding 'next check changed' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
255 double nextCheck = params->Get("next_check");
257 if (nextCheck < Application::GetStartTime() + 60)
260 checkable->SetNextCheck(params->Get("next_check"), false, origin);
265 void ClusterEvents::NextNotificationChangedHandler(const Notification::Ptr& notification, const MessageOrigin::Ptr& origin)
267 ApiListener::Ptr listener = ApiListener::GetInstance();
272 Dictionary::Ptr params = new Dictionary();
273 params->Set("notification", notification->GetName());
274 params->Set("next_notification", notification->GetNextNotification());
276 Dictionary::Ptr message = new Dictionary();
277 message->Set("jsonrpc", "2.0");
278 message->Set("method", "event::SetNextNotification");
279 message->Set("params", params);
281 listener->RelayMessage(origin, notification, message, true);
284 Value ClusterEvents::NextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
286 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
289 Log(LogNotice, "ClusterEvents")
290 << "Discarding 'next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
297 Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
302 if (origin->FromZone && !origin->FromZone->CanAccessObject(notification)) {
303 Log(LogNotice, "ClusterEvents")
304 << "Discarding 'next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
308 double nextNotification = params->Get("next_notification");
310 if (nextNotification < Utility::GetTime())
313 notification->SetNextNotification(nextNotification, false, origin);
318 void ClusterEvents::ForceNextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
320 ApiListener::Ptr listener = ApiListener::GetInstance();
326 Service::Ptr service;
327 tie(host, service) = GetHostService(checkable);
329 Dictionary::Ptr params = new Dictionary();
330 params->Set("host", host->GetName());
332 params->Set("service", service->GetShortName());
333 params->Set("forced", checkable->GetForceNextCheck());
335 Dictionary::Ptr message = new Dictionary();
336 message->Set("jsonrpc", "2.0");
337 message->Set("method", "event::SetForceNextCheck");
338 message->Set("params", params);
340 listener->RelayMessage(origin, checkable, message, true);
343 Value ClusterEvents::ForceNextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
345 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
348 Log(LogNotice, "ClusterEvents")
349 << "Discarding 'force next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
356 Host::Ptr host = Host::GetByName(params->Get("host"));
361 Checkable::Ptr checkable;
363 if (params->Contains("service"))
364 checkable = host->GetServiceByShortName(params->Get("service"));
371 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
372 Log(LogNotice, "ClusterEvents")
373 << "Discarding 'force next check' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
377 checkable->SetForceNextCheck(params->Get("forced"), false, origin);
382 void ClusterEvents::ForceNextNotificationChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
384 ApiListener::Ptr listener = ApiListener::GetInstance();
390 Service::Ptr service;
391 tie(host, service) = GetHostService(checkable);
393 Dictionary::Ptr params = new Dictionary();
394 params->Set("host", host->GetName());
396 params->Set("service", service->GetShortName());
397 params->Set("forced", checkable->GetForceNextNotification());
399 Dictionary::Ptr message = new Dictionary();
400 message->Set("jsonrpc", "2.0");
401 message->Set("method", "event::SetForceNextNotification");
402 message->Set("params", params);
404 listener->RelayMessage(origin, checkable, message, true);
407 Value ClusterEvents::ForceNextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
409 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
412 Log(LogNotice, "ClusterEvents")
413 << "Discarding 'force next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
420 Host::Ptr host = Host::GetByName(params->Get("host"));
425 Checkable::Ptr checkable;
427 if (params->Contains("service"))
428 checkable = host->GetServiceByShortName(params->Get("service"));
435 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
436 Log(LogNotice, "ClusterEvents")
437 << "Discarding 'force next notification' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
441 checkable->SetForceNextNotification(params->Get("forced"), false, origin);
446 void ClusterEvents::AcknowledgementSetHandler(const Checkable::Ptr& checkable,
447 const String& author, const String& comment, AcknowledgementType type,
448 bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
450 ApiListener::Ptr listener = ApiListener::GetInstance();
456 Service::Ptr service;
457 tie(host, service) = GetHostService(checkable);
459 Dictionary::Ptr params = new Dictionary();
460 params->Set("host", host->GetName());
462 params->Set("service", service->GetShortName());
463 params->Set("author", author);
464 params->Set("comment", comment);
465 params->Set("acktype", type);
466 params->Set("notify", notify);
467 params->Set("expiry", expiry);
469 Dictionary::Ptr message = new Dictionary();
470 message->Set("jsonrpc", "2.0");
471 message->Set("method", "event::SetAcknowledgement");
472 message->Set("params", params);
474 listener->RelayMessage(origin, checkable, message, true);
477 Value ClusterEvents::AcknowledgementSetAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
479 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
482 Log(LogNotice, "ClusterEvents")
483 << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
490 Host::Ptr host = Host::GetByName(params->Get("host"));
495 Checkable::Ptr checkable;
497 if (params->Contains("service"))
498 checkable = host->GetServiceByShortName(params->Get("service"));
505 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
506 Log(LogNotice, "ClusterEvents")
507 << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
511 checkable->AcknowledgeProblem(params->Get("author"), params->Get("comment"),
512 static_cast<AcknowledgementType>(static_cast<int>(params->Get("acktype"))),
513 params->Get("notify"), params->Get("persistent"), params->Get("expiry"), origin);
518 void ClusterEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
520 ApiListener::Ptr listener = ApiListener::GetInstance();
526 Service::Ptr service;
527 tie(host, service) = GetHostService(checkable);
529 Dictionary::Ptr params = new Dictionary();
530 params->Set("host", host->GetName());
532 params->Set("service", service->GetShortName());
534 Dictionary::Ptr message = new Dictionary();
535 message->Set("jsonrpc", "2.0");
536 message->Set("method", "event::ClearAcknowledgement");
537 message->Set("params", params);
539 listener->RelayMessage(origin, checkable, message, true);
542 Value ClusterEvents::AcknowledgementClearedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
544 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
547 Log(LogNotice, "ClusterEvents")
548 << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
555 Host::Ptr host = Host::GetByName(params->Get("host"));
560 Checkable::Ptr checkable;
562 if (params->Contains("service"))
563 checkable = host->GetServiceByShortName(params->Get("service"));
570 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
571 Log(LogNotice, "ClusterEvents")
572 << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
576 checkable->ClearAcknowledgement(origin);
581 Value ClusterEvents::ExecuteCommandAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
583 Endpoint::Ptr sourceEndpoint = origin->FromClient->GetEndpoint();
585 if (!sourceEndpoint || (origin->FromZone && !Zone::GetLocalZone()->IsChildOf(origin->FromZone))) {
586 Log(LogNotice, "ClusterEvents")
587 << "Discarding 'execute command' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
591 ApiListener::Ptr listener = ApiListener::GetInstance();
594 Log(LogCritical, "ApiListener", "No instance available.");
598 if (!listener->GetAcceptCommands()) {
599 Log(LogWarning, "ApiListener")
600 << "Ignoring command. '" << listener->GetName() << "' does not accept commands.";
602 Host::Ptr host = new Host();
603 Dictionary::Ptr attrs = new Dictionary();
605 attrs->Set("__name", params->Get("host"));
606 attrs->Set("type", "Host");
607 attrs->Set("enable_active_checks", false);
609 Deserialize(host, attrs, false, FAConfig);
611 if (params->Contains("service"))
612 host->SetExtension("agent_service_name", params->Get("service"));
614 CheckResult::Ptr cr = new CheckResult();
615 cr->SetState(ServiceUnknown);
616 cr->SetOutput("Endpoint '" + Endpoint::GetLocalEndpoint()->GetName() + "' does not accept commands.");
617 Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
618 listener->SyncSendMessage(sourceEndpoint, message);
623 /* use a virtual host object for executing the command */
624 Host::Ptr host = new Host();
625 Dictionary::Ptr attrs = new Dictionary();
627 attrs->Set("__name", params->Get("host"));
628 attrs->Set("type", "Host");
630 Deserialize(host, attrs, false, FAConfig);
632 if (params->Contains("service"))
633 host->SetExtension("agent_service_name", params->Get("service"));
635 String command = params->Get("command");
636 String command_type = params->Get("command_type");
638 if (command_type == "check_command") {
639 if (!CheckCommand::GetByName(command)) {
640 CheckResult::Ptr cr = new CheckResult();
641 cr->SetState(ServiceUnknown);
642 cr->SetOutput("Check command '" + command + "' does not exist.");
643 Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
644 listener->SyncSendMessage(sourceEndpoint, message);
647 } else if (command_type == "event_command") {
648 if (!EventCommand::GetByName(command)) {
649 Log(LogWarning, "ClusterEvents")
650 << "Event command '" << command << "' does not exist.";
656 attrs->Set(command_type, params->Get("command"));
657 attrs->Set("command_endpoint", sourceEndpoint->GetName());
659 Deserialize(host, attrs, false, FAConfig);
661 host->SetExtension("agent_check", true);
663 Dictionary::Ptr macros = params->Get("macros");
665 if (command_type == "check_command") {
667 host->ExecuteRemoteCheck(macros);
668 } catch (const std::exception& ex) {
669 CheckResult::Ptr cr = new CheckResult();
670 cr->SetState(ServiceUnknown);
672 String output = "Exception occured while checking '" + host->GetName() + "': " + DiagnosticInformation(ex);
673 cr->SetOutput(output);
675 double now = Utility::GetTime();
676 cr->SetScheduleStart(now);
677 cr->SetScheduleEnd(now);
678 cr->SetExecutionStart(now);
679 cr->SetExecutionEnd(now);
681 Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
682 listener->SyncSendMessage(sourceEndpoint, message);
684 Log(LogCritical, "checker", output);
686 } else if (command_type == "event_command") {
687 host->ExecuteEventHandler(macros, true);
693 void ClusterEvents::RepositoryTimerHandler(void)
695 ApiListener::Ptr listener = ApiListener::GetInstance();
700 Dictionary::Ptr repository = new Dictionary();
702 for (const Host::Ptr& host : ConfigType::GetObjectsByType<Host>()) {
703 Array::Ptr services = new Array();
705 for (const Service::Ptr& service : host->GetServices()) {
706 services->Add(service->GetShortName());
709 repository->Set(host->GetName(), services);
712 Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
715 Log(LogWarning, "ClusterEvents", "No local endpoint defined. Bailing out.");
719 Zone::Ptr my_zone = my_endpoint->GetZone();
724 Dictionary::Ptr params = new Dictionary();
725 params->Set("seen", Utility::GetTime());
726 params->Set("endpoint", my_endpoint->GetName());
727 params->Set("zone", my_zone->GetName());
728 params->Set("repository", repository);
730 Dictionary::Ptr message = new Dictionary();
731 message->Set("jsonrpc", "2.0");
732 message->Set("method", "event::UpdateRepository");
733 message->Set("params", params);
735 listener->RelayMessage(MessageOrigin::Ptr(), my_zone, message, false);
738 String ClusterEvents::GetRepositoryDir(void)
740 return Application::GetLocalStateDir() + "/lib/icinga2/api/repository/";
743 Value ClusterEvents::UpdateRepositoryAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
748 Value vrepository = params->Get("repository");
749 if (vrepository.IsEmpty() || !vrepository.IsObjectType<Dictionary>())
752 Utility::MkDirP(GetRepositoryDir(), 0755);
754 String repositoryFile = GetRepositoryDir() + SHA256(params->Get("endpoint")) + ".repo";
757 String tempRepositoryFile = Utility::CreateTempFile(repositoryFile + ".XXXXXX", 0644, fp);
759 fp << JsonEncode(params);
763 _unlink(repositoryFile.CStr());
766 if (rename(tempRepositoryFile.CStr(), repositoryFile.CStr()) < 0) {
767 BOOST_THROW_EXCEPTION(posix_error()
768 << boost::errinfo_api_function("rename")
769 << boost::errinfo_errno(errno)
770 << boost::errinfo_file_name(tempRepositoryFile));
773 ApiListener::Ptr listener = ApiListener::GetInstance();
778 Dictionary::Ptr message = new Dictionary();
779 message->Set("jsonrpc", "2.0");
780 message->Set("method", "event::UpdateRepository");
781 message->Set("params", params);
783 listener->RelayMessage(origin, Zone::GetLocalZone(), message, true);
788 void ClusterEvents::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
789 const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr& origin)
791 ApiListener::Ptr listener = ApiListener::GetInstance();
796 Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
797 message->Set("method", "event::SendNotifications");
799 Dictionary::Ptr params = message->Get("params");
800 params->Set("type", type);
801 params->Set("author", author);
802 params->Set("text", text);
804 listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
807 Value ClusterEvents::SendNotificationsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
809 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
812 Log(LogNotice, "ClusterEvents")
813 << "Discarding 'send notification' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
820 Host::Ptr host = Host::GetByName(params->Get("host"));
825 Checkable::Ptr checkable;
827 if (params->Contains("service"))
828 checkable = host->GetServiceByShortName(params->Get("service"));
835 if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
836 Log(LogNotice, "ClusterEvents")
837 << "Discarding 'send custom notification' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
844 if (params->Contains("cr")) {
845 cr = new CheckResult();
846 Dictionary::Ptr vcr = params->Get("cr");
848 if (vcr && vcr->Contains("performance_data")) {
849 vperf = vcr->Get("performance_data");
852 vcr->Remove("performance_data");
854 Deserialize(cr, vcr, true);
858 NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
859 String author = params->Get("author");
860 String text = params->Get("text");
862 Checkable::OnNotificationsRequested(checkable, type, cr, author, text, origin);
867 void ClusterEvents::NotificationSentUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const User::Ptr& user,
868 NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& command,
869 const MessageOrigin::Ptr& origin)
871 ApiListener::Ptr listener = ApiListener::GetInstance();
877 Service::Ptr service;
878 tie(host, service) = GetHostService(checkable);
880 Dictionary::Ptr params = new Dictionary();
881 params->Set("host", host->GetName());
883 params->Set("service", service->GetShortName());
884 params->Set("notification", notification->GetName());
885 params->Set("user", user->GetName());
886 params->Set("type", notificationType);
887 params->Set("cr", Serialize(cr));
888 params->Set("author", author);
889 params->Set("text", commentText);
890 params->Set("command", command);
892 Dictionary::Ptr message = new Dictionary();
893 message->Set("jsonrpc", "2.0");
894 message->Set("method", "event::NotificationSentUser");
895 message->Set("params", params);
897 listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
900 Value ClusterEvents::NotificationSentUserAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
902 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
905 Log(LogNotice, "ClusterEvents")
906 << "Discarding 'sent notification to user' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
913 Host::Ptr host = Host::GetByName(params->Get("host"));
918 Checkable::Ptr checkable;
920 if (params->Contains("service"))
921 checkable = host->GetServiceByShortName(params->Get("service"));
928 if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
929 Log(LogNotice, "ClusterEvents")
930 << "Discarding 'sent notification to user' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
937 if (params->Contains("cr")) {
938 cr = new CheckResult();
939 Dictionary::Ptr vcr = params->Get("cr");
941 if (vcr && vcr->Contains("performance_data")) {
942 vperf = vcr->Get("performance_data");
945 vcr->Remove("performance_data");
947 Deserialize(cr, vcr, true);
951 NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
952 String author = params->Get("author");
953 String text = params->Get("text");
955 Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
960 User::Ptr user = User::GetByName(params->Get("user"));
965 String command = params->Get("command");
967 Checkable::OnNotificationSentToUser(notification, checkable, user, type, cr, author, text, command, origin);
972 void ClusterEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
973 NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const MessageOrigin::Ptr& origin)
975 ApiListener::Ptr listener = ApiListener::GetInstance();
981 Service::Ptr service;
982 tie(host, service) = GetHostService(checkable);
984 Dictionary::Ptr params = new Dictionary();
985 params->Set("host", host->GetName());
987 params->Set("service", service->GetShortName());
988 params->Set("notification", notification->GetName());
990 Array::Ptr ausers = new Array();
991 for (const User::Ptr& user : users) {
992 ausers->Add(user->GetName());
994 params->Set("users", ausers);
996 params->Set("type", notificationType);
997 params->Set("cr", Serialize(cr));
998 params->Set("author", author);
999 params->Set("text", commentText);
1001 params->Set("last_notification", notification->GetLastNotification());
1002 params->Set("next_notification", notification->GetNextNotification());
1003 params->Set("notification_number", notification->GetNotificationNumber());
1004 params->Set("last_problem_notification", notification->GetLastProblemNotification());
1005 params->Set("no_more_notifications", notification->GetNoMoreNotifications());
1007 Dictionary::Ptr message = new Dictionary();
1008 message->Set("jsonrpc", "2.0");
1009 message->Set("method", "event::NotificationSentToAllUsers");
1010 message->Set("params", params);
1012 listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
1015 Value ClusterEvents::NotificationSentToAllUsersAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
1017 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
1020 Log(LogNotice, "ClusterEvents")
1021 << "Discarding 'sent notification to all users' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
1028 Host::Ptr host = Host::GetByName(params->Get("host"));
1033 Checkable::Ptr checkable;
1035 if (params->Contains("service"))
1036 checkable = host->GetServiceByShortName(params->Get("service"));
1043 if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
1044 Log(LogNotice, "ClusterEvents")
1045 << "Discarding 'sent notification to all users' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
1049 CheckResult::Ptr cr;
1052 if (params->Contains("cr")) {
1053 cr = new CheckResult();
1054 Dictionary::Ptr vcr = params->Get("cr");
1056 if (vcr && vcr->Contains("performance_data")) {
1057 vperf = vcr->Get("performance_data");
1060 vcr->Remove("performance_data");
1062 Deserialize(cr, vcr, true);
1066 NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
1067 String author = params->Get("author");
1068 String text = params->Get("text");
1070 Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
1075 Array::Ptr ausers = params->Get("users");
1080 std::set<User::Ptr> users;
1083 ObjectLock olock(ausers);
1084 for (const String& auser : ausers) {
1085 User::Ptr user = User::GetByName(auser);
1094 notification->SetLastNotification(params->Get("last_notification"));
1095 notification->SetNextNotification(params->Get("next_notification"));
1096 notification->SetNotificationNumber(params->Get("notification_number"));
1097 notification->SetLastProblemNotification(params->Get("last_problem_notification"));
1098 notification->SetNoMoreNotifications(params->Get("no_more_notifications"));
1100 Array::Ptr notifiedProblemUsers = new Array();
1101 for (const User::Ptr& user : users) {
1102 notifiedProblemUsers->Add(user->GetName());
1105 notification->SetNotifiedProblemUsers(notifiedProblemUsers);
1107 Checkable::OnNotificationSentToAllUsers(notification, checkable, users, type, cr, author, text, origin);