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 "remote/apilistener.hpp"
23 #include "remote/endpoint.hpp"
24 #include "remote/messageorigin.hpp"
25 #include "remote/zone.hpp"
26 #include "remote/apifunction.hpp"
27 #include "remote/eventqueue.hpp"
28 #include "base/application.hpp"
29 #include "base/configtype.hpp"
30 #include "base/utility.hpp"
31 #include "base/perfdatavalue.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(ExecuteCommand, event, &ClusterEvents::ExecuteCommandAPIHandler);
50 REGISTER_APIFUNCTION(SendNotifications, event, &ClusterEvents::SendNotificationsAPIHandler);
51 REGISTER_APIFUNCTION(NotificationSentUser, event, &ClusterEvents::NotificationSentUserAPIHandler);
52 REGISTER_APIFUNCTION(NotificationSentToAllUsers, event, &ClusterEvents::NotificationSentToAllUsersAPIHandler);
54 void ClusterEvents::StaticInitialize(void)
56 Checkable::OnNewCheckResult.connect(&ClusterEvents::CheckResultHandler);
57 Checkable::OnNextCheckChanged.connect(&ClusterEvents::NextCheckChangedHandler);
58 Notification::OnNextNotificationChanged.connect(&ClusterEvents::NextNotificationChangedHandler);
59 Checkable::OnForceNextCheckChanged.connect(&ClusterEvents::ForceNextCheckChangedHandler);
60 Checkable::OnForceNextNotificationChanged.connect(&ClusterEvents::ForceNextNotificationChangedHandler);
61 Checkable::OnNotificationsRequested.connect(&ClusterEvents::SendNotificationsHandler);
62 Checkable::OnNotificationSentToUser.connect(&ClusterEvents::NotificationSentUserHandler);
63 Checkable::OnNotificationSentToAllUsers.connect(&ClusterEvents::NotificationSentToAllUsersHandler);
65 Checkable::OnAcknowledgementSet.connect(&ClusterEvents::AcknowledgementSetHandler);
66 Checkable::OnAcknowledgementCleared.connect(&ClusterEvents::AcknowledgementClearedHandler);
69 Dictionary::Ptr ClusterEvents::MakeCheckResultMessage(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
71 Dictionary::Ptr message = new Dictionary();
72 message->Set("jsonrpc", "2.0");
73 message->Set("method", "event::CheckResult");
77 tie(host, service) = GetHostService(checkable);
79 Dictionary::Ptr params = new Dictionary();
80 params->Set("host", host->GetName());
82 params->Set("service", service->GetShortName());
84 Value agent_service_name = checkable->GetExtension("agent_service_name");
86 if (!agent_service_name.IsEmpty())
87 params->Set("service", agent_service_name);
89 params->Set("cr", Serialize(cr));
91 message->Set("params", params);
96 void ClusterEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
98 ApiListener::Ptr listener = ApiListener::GetInstance();
103 Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
104 listener->RelayMessage(origin, checkable, message, true);
107 Value ClusterEvents::CheckResultAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
109 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
112 Log(LogNotice, "ClusterEvents")
113 << "Discarding 'check result' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
120 if (params->Contains("cr")) {
121 cr = new CheckResult();
122 Dictionary::Ptr vcr = params->Get("cr");
124 if (vcr && vcr->Contains("performance_data")) {
125 vperf = vcr->Get("performance_data");
128 vcr->Remove("performance_data");
130 Deserialize(cr, vcr, true);
137 Array::Ptr rperf = new Array();
140 ObjectLock olock(vperf);
141 for (const Value& vp : vperf) {
144 if (vp.IsObjectType<Dictionary>()) {
145 PerfdataValue::Ptr val = new PerfdataValue();
146 Deserialize(val, vp, true);
153 cr->SetPerformanceData(rperf);
155 Host::Ptr host = Host::GetByName(params->Get("host"));
160 Checkable::Ptr checkable;
162 if (params->Contains("service"))
163 checkable = host->GetServiceByShortName(params->Get("service"));
170 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable) && endpoint != checkable->GetCommandEndpoint()) {
171 Log(LogNotice, "ClusterEvents")
172 << "Discarding 'check result' message for checkable '" << checkable->GetName()
173 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
177 if (!checkable->IsPaused() && Zone::GetLocalZone() == checkable->GetZone() && endpoint == checkable->GetCommandEndpoint())
178 checkable->ProcessCheckResult(cr);
180 checkable->ProcessCheckResult(cr, origin);
185 void ClusterEvents::NextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
187 ApiListener::Ptr listener = ApiListener::GetInstance();
193 Service::Ptr service;
194 tie(host, service) = GetHostService(checkable);
196 Dictionary::Ptr params = new Dictionary();
197 params->Set("host", host->GetName());
199 params->Set("service", service->GetShortName());
200 params->Set("next_check", checkable->GetNextCheck());
202 Dictionary::Ptr message = new Dictionary();
203 message->Set("jsonrpc", "2.0");
204 message->Set("method", "event::SetNextCheck");
205 message->Set("params", params);
207 listener->RelayMessage(origin, checkable, message, true);
210 Value ClusterEvents::NextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
212 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
215 Log(LogNotice, "ClusterEvents")
216 << "Discarding 'next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
220 Host::Ptr host = Host::GetByName(params->Get("host"));
225 Checkable::Ptr checkable;
227 if (params->Contains("service"))
228 checkable = host->GetServiceByShortName(params->Get("service"));
235 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
236 Log(LogNotice, "ClusterEvents")
237 << "Discarding 'next check changed' message for checkable '" << checkable->GetName()
238 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
242 double nextCheck = params->Get("next_check");
244 if (nextCheck < Application::GetStartTime() + 60)
247 checkable->SetNextCheck(params->Get("next_check"), false, origin);
252 void ClusterEvents::NextNotificationChangedHandler(const Notification::Ptr& notification, const MessageOrigin::Ptr& origin)
254 ApiListener::Ptr listener = ApiListener::GetInstance();
259 Dictionary::Ptr params = new Dictionary();
260 params->Set("notification", notification->GetName());
261 params->Set("next_notification", notification->GetNextNotification());
263 Dictionary::Ptr message = new Dictionary();
264 message->Set("jsonrpc", "2.0");
265 message->Set("method", "event::SetNextNotification");
266 message->Set("params", params);
268 listener->RelayMessage(origin, notification, message, true);
271 Value ClusterEvents::NextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
273 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
276 Log(LogNotice, "ClusterEvents")
277 << "Discarding 'next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
281 Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
286 if (origin->FromZone && !origin->FromZone->CanAccessObject(notification)) {
287 Log(LogNotice, "ClusterEvents")
288 << "Discarding 'next notification changed' message for notification '" << notification->GetName()
289 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
293 double nextNotification = params->Get("next_notification");
295 if (nextNotification < Utility::GetTime())
298 notification->SetNextNotification(nextNotification, false, origin);
303 void ClusterEvents::ForceNextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
305 ApiListener::Ptr listener = ApiListener::GetInstance();
311 Service::Ptr service;
312 tie(host, service) = GetHostService(checkable);
314 Dictionary::Ptr params = new Dictionary();
315 params->Set("host", host->GetName());
317 params->Set("service", service->GetShortName());
318 params->Set("forced", checkable->GetForceNextCheck());
320 Dictionary::Ptr message = new Dictionary();
321 message->Set("jsonrpc", "2.0");
322 message->Set("method", "event::SetForceNextCheck");
323 message->Set("params", params);
325 listener->RelayMessage(origin, checkable, message, true);
328 Value ClusterEvents::ForceNextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
330 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
333 Log(LogNotice, "ClusterEvents")
334 << "Discarding 'force next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
338 Host::Ptr host = Host::GetByName(params->Get("host"));
343 Checkable::Ptr checkable;
345 if (params->Contains("service"))
346 checkable = host->GetServiceByShortName(params->Get("service"));
353 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
354 Log(LogNotice, "ClusterEvents")
355 << "Discarding 'force next check' message for checkable '" << checkable->GetName()
356 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
360 checkable->SetForceNextCheck(params->Get("forced"), false, origin);
365 void ClusterEvents::ForceNextNotificationChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
367 ApiListener::Ptr listener = ApiListener::GetInstance();
373 Service::Ptr service;
374 tie(host, service) = GetHostService(checkable);
376 Dictionary::Ptr params = new Dictionary();
377 params->Set("host", host->GetName());
379 params->Set("service", service->GetShortName());
380 params->Set("forced", checkable->GetForceNextNotification());
382 Dictionary::Ptr message = new Dictionary();
383 message->Set("jsonrpc", "2.0");
384 message->Set("method", "event::SetForceNextNotification");
385 message->Set("params", params);
387 listener->RelayMessage(origin, checkable, message, true);
390 Value ClusterEvents::ForceNextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
392 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
395 Log(LogNotice, "ClusterEvents")
396 << "Discarding 'force next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
400 Host::Ptr host = Host::GetByName(params->Get("host"));
405 Checkable::Ptr checkable;
407 if (params->Contains("service"))
408 checkable = host->GetServiceByShortName(params->Get("service"));
415 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
416 Log(LogNotice, "ClusterEvents")
417 << "Discarding 'force next notification' message for checkable '" << checkable->GetName()
418 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
422 checkable->SetForceNextNotification(params->Get("forced"), false, origin);
427 void ClusterEvents::AcknowledgementSetHandler(const Checkable::Ptr& checkable,
428 const String& author, const String& comment, AcknowledgementType type,
429 bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
431 ApiListener::Ptr listener = ApiListener::GetInstance();
437 Service::Ptr service;
438 tie(host, service) = GetHostService(checkable);
440 Dictionary::Ptr params = new Dictionary();
441 params->Set("host", host->GetName());
443 params->Set("service", service->GetShortName());
444 params->Set("author", author);
445 params->Set("comment", comment);
446 params->Set("acktype", type);
447 params->Set("notify", notify);
448 params->Set("expiry", expiry);
450 Dictionary::Ptr message = new Dictionary();
451 message->Set("jsonrpc", "2.0");
452 message->Set("method", "event::SetAcknowledgement");
453 message->Set("params", params);
455 listener->RelayMessage(origin, checkable, message, true);
458 Value ClusterEvents::AcknowledgementSetAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
460 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
463 Log(LogNotice, "ClusterEvents")
464 << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
468 Host::Ptr host = Host::GetByName(params->Get("host"));
473 Checkable::Ptr checkable;
475 if (params->Contains("service"))
476 checkable = host->GetServiceByShortName(params->Get("service"));
483 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
484 Log(LogNotice, "ClusterEvents")
485 << "Discarding 'acknowledgement set' message for checkable '" << checkable->GetName()
486 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
490 checkable->AcknowledgeProblem(params->Get("author"), params->Get("comment"),
491 static_cast<AcknowledgementType>(static_cast<int>(params->Get("acktype"))),
492 params->Get("notify"), params->Get("persistent"), params->Get("expiry"), origin);
497 void ClusterEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
499 ApiListener::Ptr listener = ApiListener::GetInstance();
505 Service::Ptr service;
506 tie(host, service) = GetHostService(checkable);
508 Dictionary::Ptr params = new Dictionary();
509 params->Set("host", host->GetName());
511 params->Set("service", service->GetShortName());
513 Dictionary::Ptr message = new Dictionary();
514 message->Set("jsonrpc", "2.0");
515 message->Set("method", "event::ClearAcknowledgement");
516 message->Set("params", params);
518 listener->RelayMessage(origin, checkable, message, true);
521 Value ClusterEvents::AcknowledgementClearedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
523 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
526 Log(LogNotice, "ClusterEvents")
527 << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
531 Host::Ptr host = Host::GetByName(params->Get("host"));
536 Checkable::Ptr checkable;
538 if (params->Contains("service"))
539 checkable = host->GetServiceByShortName(params->Get("service"));
546 if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
547 Log(LogNotice, "ClusterEvents")
548 << "Discarding 'acknowledgement cleared' message for checkable '" << checkable->GetName()
549 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
553 checkable->ClearAcknowledgement(origin);
558 Value ClusterEvents::ExecuteCommandAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
560 Endpoint::Ptr sourceEndpoint = origin->FromClient->GetEndpoint();
562 if (!sourceEndpoint || (origin->FromZone && !Zone::GetLocalZone()->IsChildOf(origin->FromZone))) {
563 Log(LogNotice, "ClusterEvents")
564 << "Discarding 'execute command' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
568 ApiListener::Ptr listener = ApiListener::GetInstance();
571 Log(LogCritical, "ApiListener", "No instance available.");
575 if (!listener->GetAcceptCommands()) {
576 Log(LogWarning, "ApiListener")
577 << "Ignoring command. '" << listener->GetName() << "' does not accept commands.";
579 Host::Ptr host = new Host();
580 Dictionary::Ptr attrs = new Dictionary();
582 attrs->Set("__name", params->Get("host"));
583 attrs->Set("type", "Host");
584 attrs->Set("enable_active_checks", false);
586 Deserialize(host, attrs, false, FAConfig);
588 if (params->Contains("service"))
589 host->SetExtension("agent_service_name", params->Get("service"));
591 CheckResult::Ptr cr = new CheckResult();
592 cr->SetState(ServiceUnknown);
593 cr->SetOutput("Endpoint '" + Endpoint::GetLocalEndpoint()->GetName() + "' does not accept commands.");
594 Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
595 listener->SyncSendMessage(sourceEndpoint, message);
600 /* use a virtual host object for executing the command */
601 Host::Ptr host = new Host();
602 Dictionary::Ptr attrs = new Dictionary();
604 attrs->Set("__name", params->Get("host"));
605 attrs->Set("type", "Host");
607 Deserialize(host, attrs, false, FAConfig);
609 if (params->Contains("service"))
610 host->SetExtension("agent_service_name", params->Get("service"));
612 String command = params->Get("command");
613 String command_type = params->Get("command_type");
615 if (command_type == "check_command") {
616 if (!CheckCommand::GetByName(command)) {
617 CheckResult::Ptr cr = new CheckResult();
618 cr->SetState(ServiceUnknown);
619 cr->SetOutput("Check command '" + command + "' does not exist.");
620 Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
621 listener->SyncSendMessage(sourceEndpoint, message);
624 } else if (command_type == "event_command") {
625 if (!EventCommand::GetByName(command)) {
626 Log(LogWarning, "ClusterEvents")
627 << "Event command '" << command << "' does not exist.";
633 attrs->Set(command_type, params->Get("command"));
634 attrs->Set("command_endpoint", sourceEndpoint->GetName());
636 Deserialize(host, attrs, false, FAConfig);
638 host->SetExtension("agent_check", true);
640 Dictionary::Ptr macros = params->Get("macros");
642 if (command_type == "check_command") {
644 host->ExecuteRemoteCheck(macros);
645 } catch (const std::exception& ex) {
646 CheckResult::Ptr cr = new CheckResult();
647 cr->SetState(ServiceUnknown);
649 String output = "Exception occured while checking '" + host->GetName() + "': " + DiagnosticInformation(ex);
650 cr->SetOutput(output);
652 double now = Utility::GetTime();
653 cr->SetScheduleStart(now);
654 cr->SetScheduleEnd(now);
655 cr->SetExecutionStart(now);
656 cr->SetExecutionEnd(now);
658 Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
659 listener->SyncSendMessage(sourceEndpoint, message);
661 Log(LogCritical, "checker", output);
663 } else if (command_type == "event_command") {
664 host->ExecuteEventHandler(macros, true);
670 void ClusterEvents::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
671 const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr& origin)
673 ApiListener::Ptr listener = ApiListener::GetInstance();
678 Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
679 message->Set("method", "event::SendNotifications");
681 Dictionary::Ptr params = message->Get("params");
682 params->Set("type", type);
683 params->Set("author", author);
684 params->Set("text", text);
686 listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
689 Value ClusterEvents::SendNotificationsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
691 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
694 Log(LogNotice, "ClusterEvents")
695 << "Discarding 'send notification' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
699 Host::Ptr host = Host::GetByName(params->Get("host"));
704 Checkable::Ptr checkable;
706 if (params->Contains("service"))
707 checkable = host->GetServiceByShortName(params->Get("service"));
714 if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
715 Log(LogNotice, "ClusterEvents")
716 << "Discarding 'send custom notification' message for checkable '" << checkable->GetName()
717 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
724 if (params->Contains("cr")) {
725 cr = new CheckResult();
726 Dictionary::Ptr vcr = params->Get("cr");
728 if (vcr && vcr->Contains("performance_data")) {
729 vperf = vcr->Get("performance_data");
732 vcr->Remove("performance_data");
734 Deserialize(cr, vcr, true);
738 NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
739 String author = params->Get("author");
740 String text = params->Get("text");
742 Checkable::OnNotificationsRequested(checkable, type, cr, author, text, origin);
747 void ClusterEvents::NotificationSentUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const User::Ptr& user,
748 NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& command,
749 const MessageOrigin::Ptr& origin)
751 ApiListener::Ptr listener = ApiListener::GetInstance();
757 Service::Ptr service;
758 tie(host, service) = GetHostService(checkable);
760 Dictionary::Ptr params = new Dictionary();
761 params->Set("host", host->GetName());
763 params->Set("service", service->GetShortName());
764 params->Set("notification", notification->GetName());
765 params->Set("user", user->GetName());
766 params->Set("type", notificationType);
767 params->Set("cr", Serialize(cr));
768 params->Set("author", author);
769 params->Set("text", commentText);
770 params->Set("command", command);
772 Dictionary::Ptr message = new Dictionary();
773 message->Set("jsonrpc", "2.0");
774 message->Set("method", "event::NotificationSentUser");
775 message->Set("params", params);
777 listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
780 Value ClusterEvents::NotificationSentUserAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
782 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
785 Log(LogNotice, "ClusterEvents")
786 << "Discarding 'sent notification to user' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
790 Host::Ptr host = Host::GetByName(params->Get("host"));
795 Checkable::Ptr checkable;
797 if (params->Contains("service"))
798 checkable = host->GetServiceByShortName(params->Get("service"));
805 if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
806 Log(LogNotice, "ClusterEvents")
807 << "Discarding 'send notification to user' message for checkable '" << checkable->GetName()
808 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
815 if (params->Contains("cr")) {
816 cr = new CheckResult();
817 Dictionary::Ptr vcr = params->Get("cr");
819 if (vcr && vcr->Contains("performance_data")) {
820 vperf = vcr->Get("performance_data");
823 vcr->Remove("performance_data");
825 Deserialize(cr, vcr, true);
829 NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
830 String author = params->Get("author");
831 String text = params->Get("text");
833 Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
838 User::Ptr user = User::GetByName(params->Get("user"));
843 String command = params->Get("command");
845 Checkable::OnNotificationSentToUser(notification, checkable, user, type, cr, author, text, command, origin);
850 void ClusterEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
851 NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const MessageOrigin::Ptr& origin)
853 ApiListener::Ptr listener = ApiListener::GetInstance();
859 Service::Ptr service;
860 tie(host, service) = GetHostService(checkable);
862 Dictionary::Ptr params = new Dictionary();
863 params->Set("host", host->GetName());
865 params->Set("service", service->GetShortName());
866 params->Set("notification", notification->GetName());
868 Array::Ptr ausers = new Array();
869 for (const User::Ptr& user : users) {
870 ausers->Add(user->GetName());
872 params->Set("users", ausers);
874 params->Set("type", notificationType);
875 params->Set("cr", Serialize(cr));
876 params->Set("author", author);
877 params->Set("text", commentText);
879 params->Set("last_notification", notification->GetLastNotification());
880 params->Set("next_notification", notification->GetNextNotification());
881 params->Set("notification_number", notification->GetNotificationNumber());
882 params->Set("last_problem_notification", notification->GetLastProblemNotification());
883 params->Set("no_more_notifications", notification->GetNoMoreNotifications());
885 Dictionary::Ptr message = new Dictionary();
886 message->Set("jsonrpc", "2.0");
887 message->Set("method", "event::NotificationSentToAllUsers");
888 message->Set("params", params);
890 listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
893 Value ClusterEvents::NotificationSentToAllUsersAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
895 Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
898 Log(LogNotice, "ClusterEvents")
899 << "Discarding 'sent notification to all users' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
903 Host::Ptr host = Host::GetByName(params->Get("host"));
908 Checkable::Ptr checkable;
910 if (params->Contains("service"))
911 checkable = host->GetServiceByShortName(params->Get("service"));
918 if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
919 Log(LogNotice, "ClusterEvents")
920 << "Discarding 'sent notification to all users' message for checkable '" << checkable->GetName()
921 << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
928 if (params->Contains("cr")) {
929 cr = new CheckResult();
930 Dictionary::Ptr vcr = params->Get("cr");
932 if (vcr && vcr->Contains("performance_data")) {
933 vperf = vcr->Get("performance_data");
936 vcr->Remove("performance_data");
938 Deserialize(cr, vcr, true);
942 NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
943 String author = params->Get("author");
944 String text = params->Get("text");
946 Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
951 Array::Ptr ausers = params->Get("users");
956 std::set<User::Ptr> users;
959 ObjectLock olock(ausers);
960 for (const String& auser : ausers) {
961 User::Ptr user = User::GetByName(auser);
970 notification->SetLastNotification(params->Get("last_notification"));
971 notification->SetNextNotification(params->Get("next_notification"));
972 notification->SetNotificationNumber(params->Get("notification_number"));
973 notification->SetLastProblemNotification(params->Get("last_problem_notification"));
974 notification->SetNoMoreNotifications(params->Get("no_more_notifications"));
976 Array::Ptr notifiedProblemUsers = new Array();
977 for (const User::Ptr& user : users) {
978 notifiedProblemUsers->Add(user->GetName());
981 notification->SetNotifiedProblemUsers(notifiedProblemUsers);
983 Checkable::OnNotificationSentToAllUsers(notification, checkable, users, type, cr, author, text, origin);