]> granicus.if.org Git - icinga2/blob - lib/icinga/clusterevents.cpp
Merge pull request #6854 from Icinga/bugfix/unamehelper-inefficient-6452
[icinga2] / lib / icinga / clusterevents.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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"
36 #include <fstream>
37
38 using namespace icinga;
39
40 INITIALIZE_ONCE(&ClusterEvents::StaticInitialize);
41
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);
53
54 void ClusterEvents::StaticInitialize()
55 {
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);
64
65         Checkable::OnAcknowledgementSet.connect(&ClusterEvents::AcknowledgementSetHandler);
66         Checkable::OnAcknowledgementCleared.connect(&ClusterEvents::AcknowledgementClearedHandler);
67 }
68
69 Dictionary::Ptr ClusterEvents::MakeCheckResultMessage(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
70 {
71         Dictionary::Ptr message = new Dictionary();
72         message->Set("jsonrpc", "2.0");
73         message->Set("method", "event::CheckResult");
74
75         Host::Ptr host;
76         Service::Ptr service;
77         tie(host, service) = GetHostService(checkable);
78
79         Dictionary::Ptr params = new Dictionary();
80         params->Set("host", host->GetName());
81         if (service)
82                 params->Set("service", service->GetShortName());
83         else {
84                 Value agent_service_name = checkable->GetExtension("agent_service_name");
85
86                 if (!agent_service_name.IsEmpty())
87                         params->Set("service", agent_service_name);
88         }
89         params->Set("cr", Serialize(cr));
90
91         message->Set("params", params);
92
93         return message;
94 }
95
96 void ClusterEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
97 {
98         ApiListener::Ptr listener = ApiListener::GetInstance();
99
100         if (!listener)
101                 return;
102
103         Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
104         listener->RelayMessage(origin, checkable, message, true);
105 }
106
107 Value ClusterEvents::CheckResultAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
108 {
109         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
110
111         if (!endpoint) {
112                 Log(LogNotice, "ClusterEvents")
113                         << "Discarding 'check result' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
114                 return Empty;
115         }
116
117         CheckResult::Ptr cr;
118         Array::Ptr vperf;
119
120         if (params->Contains("cr")) {
121                 cr = new CheckResult();
122                 Dictionary::Ptr vcr = params->Get("cr");
123
124                 if (vcr && vcr->Contains("performance_data")) {
125                         vperf = vcr->Get("performance_data");
126
127                         if (vperf)
128                                 vcr->Remove("performance_data");
129
130                         Deserialize(cr, vcr, true);
131                 }
132         }
133
134         if (!cr)
135                 return Empty;
136
137         ArrayData rperf;
138
139         if (vperf) {
140                 ObjectLock olock(vperf);
141                 for (const Value& vp : vperf) {
142                         Value p;
143
144                         if (vp.IsObjectType<Dictionary>()) {
145                                 PerfdataValue::Ptr val = new PerfdataValue();
146                                 Deserialize(val, vp, true);
147                                 rperf.push_back(val);
148                         } else
149                                 rperf.push_back(vp);
150                 }
151         }
152
153         cr->SetPerformanceData(new Array(std::move(rperf)));
154
155         Host::Ptr host = Host::GetByName(params->Get("host"));
156
157         if (!host)
158                 return Empty;
159
160         Checkable::Ptr checkable;
161
162         if (params->Contains("service"))
163                 checkable = host->GetServiceByShortName(params->Get("service"));
164         else
165                 checkable = host;
166
167         if (!checkable)
168                 return Empty;
169
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.";
174                 return Empty;
175         }
176
177         if (!checkable->IsPaused() && Zone::GetLocalZone() == checkable->GetZone() && endpoint == checkable->GetCommandEndpoint())
178                 checkable->ProcessCheckResult(cr);
179         else
180                 checkable->ProcessCheckResult(cr, origin);
181
182         return Empty;
183 }
184
185 void ClusterEvents::NextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
186 {
187         ApiListener::Ptr listener = ApiListener::GetInstance();
188
189         if (!listener)
190                 return;
191
192         Host::Ptr host;
193         Service::Ptr service;
194         tie(host, service) = GetHostService(checkable);
195
196         Dictionary::Ptr params = new Dictionary();
197         params->Set("host", host->GetName());
198         if (service)
199                 params->Set("service", service->GetShortName());
200         params->Set("next_check", checkable->GetNextCheck());
201
202         Dictionary::Ptr message = new Dictionary();
203         message->Set("jsonrpc", "2.0");
204         message->Set("method", "event::SetNextCheck");
205         message->Set("params", params);
206
207         listener->RelayMessage(origin, checkable, message, true);
208 }
209
210 Value ClusterEvents::NextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
211 {
212         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
213
214         if (!endpoint) {
215                 Log(LogNotice, "ClusterEvents")
216                         << "Discarding 'next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
217                 return Empty;
218         }
219
220         Host::Ptr host = Host::GetByName(params->Get("host"));
221
222         if (!host)
223                 return Empty;
224
225         Checkable::Ptr checkable;
226
227         if (params->Contains("service"))
228                 checkable = host->GetServiceByShortName(params->Get("service"));
229         else
230                 checkable = host;
231
232         if (!checkable)
233                 return Empty;
234
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.";
239                 return Empty;
240         }
241
242         double nextCheck = params->Get("next_check");
243
244         if (nextCheck < Application::GetStartTime() + 60)
245                 return Empty;
246
247         checkable->SetNextCheck(params->Get("next_check"), false, origin);
248
249         return Empty;
250 }
251
252 void ClusterEvents::NextNotificationChangedHandler(const Notification::Ptr& notification, const MessageOrigin::Ptr& origin)
253 {
254         ApiListener::Ptr listener = ApiListener::GetInstance();
255
256         if (!listener)
257                 return;
258
259         Dictionary::Ptr params = new Dictionary();
260         params->Set("notification", notification->GetName());
261         params->Set("next_notification", notification->GetNextNotification());
262
263         Dictionary::Ptr message = new Dictionary();
264         message->Set("jsonrpc", "2.0");
265         message->Set("method", "event::SetNextNotification");
266         message->Set("params", params);
267
268         listener->RelayMessage(origin, notification, message, true);
269 }
270
271 Value ClusterEvents::NextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
272 {
273         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
274
275         if (!endpoint) {
276                 Log(LogNotice, "ClusterEvents")
277                         << "Discarding 'next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
278                 return Empty;
279         }
280
281         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
282
283         if (!notification)
284                 return Empty;
285
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.";
290                 return Empty;
291         }
292
293         double nextNotification = params->Get("next_notification");
294
295         if (nextNotification < Utility::GetTime())
296                 return Empty;
297
298         notification->SetNextNotification(nextNotification, false, origin);
299
300         return Empty;
301 }
302
303 void ClusterEvents::ForceNextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
304 {
305         ApiListener::Ptr listener = ApiListener::GetInstance();
306
307         if (!listener)
308                 return;
309
310         Host::Ptr host;
311         Service::Ptr service;
312         tie(host, service) = GetHostService(checkable);
313
314         Dictionary::Ptr params = new Dictionary();
315         params->Set("host", host->GetName());
316         if (service)
317                 params->Set("service", service->GetShortName());
318         params->Set("forced", checkable->GetForceNextCheck());
319
320         Dictionary::Ptr message = new Dictionary();
321         message->Set("jsonrpc", "2.0");
322         message->Set("method", "event::SetForceNextCheck");
323         message->Set("params", params);
324
325         listener->RelayMessage(origin, checkable, message, true);
326 }
327
328 Value ClusterEvents::ForceNextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
329 {
330         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
331
332         if (!endpoint) {
333                 Log(LogNotice, "ClusterEvents")
334                         << "Discarding 'force next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
335                 return Empty;
336         }
337
338         Host::Ptr host = Host::GetByName(params->Get("host"));
339
340         if (!host)
341                 return Empty;
342
343         Checkable::Ptr checkable;
344
345         if (params->Contains("service"))
346                 checkable = host->GetServiceByShortName(params->Get("service"));
347         else
348                 checkable = host;
349
350         if (!checkable)
351                 return Empty;
352
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.";
357                 return Empty;
358         }
359
360         checkable->SetForceNextCheck(params->Get("forced"), false, origin);
361
362         return Empty;
363 }
364
365 void ClusterEvents::ForceNextNotificationChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
366 {
367         ApiListener::Ptr listener = ApiListener::GetInstance();
368
369         if (!listener)
370                 return;
371
372         Host::Ptr host;
373         Service::Ptr service;
374         tie(host, service) = GetHostService(checkable);
375
376         Dictionary::Ptr params = new Dictionary();
377         params->Set("host", host->GetName());
378         if (service)
379                 params->Set("service", service->GetShortName());
380         params->Set("forced", checkable->GetForceNextNotification());
381
382         Dictionary::Ptr message = new Dictionary();
383         message->Set("jsonrpc", "2.0");
384         message->Set("method", "event::SetForceNextNotification");
385         message->Set("params", params);
386
387         listener->RelayMessage(origin, checkable, message, true);
388 }
389
390 Value ClusterEvents::ForceNextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
391 {
392         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
393
394         if (!endpoint) {
395                 Log(LogNotice, "ClusterEvents")
396                         << "Discarding 'force next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
397                 return Empty;
398         }
399
400         Host::Ptr host = Host::GetByName(params->Get("host"));
401
402         if (!host)
403                 return Empty;
404
405         Checkable::Ptr checkable;
406
407         if (params->Contains("service"))
408                 checkable = host->GetServiceByShortName(params->Get("service"));
409         else
410                 checkable = host;
411
412         if (!checkable)
413                 return Empty;
414
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.";
419                 return Empty;
420         }
421
422         checkable->SetForceNextNotification(params->Get("forced"), false, origin);
423
424         return Empty;
425 }
426
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)
430 {
431         ApiListener::Ptr listener = ApiListener::GetInstance();
432
433         if (!listener)
434                 return;
435
436         Host::Ptr host;
437         Service::Ptr service;
438         tie(host, service) = GetHostService(checkable);
439
440         Dictionary::Ptr params = new Dictionary();
441         params->Set("host", host->GetName());
442         if (service)
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("persistent", persistent);
449         params->Set("expiry", expiry);
450
451         Dictionary::Ptr message = new Dictionary();
452         message->Set("jsonrpc", "2.0");
453         message->Set("method", "event::SetAcknowledgement");
454         message->Set("params", params);
455
456         listener->RelayMessage(origin, checkable, message, true);
457 }
458
459 Value ClusterEvents::AcknowledgementSetAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
460 {
461         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
462
463         if (!endpoint) {
464                 Log(LogNotice, "ClusterEvents")
465                         << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
466                 return Empty;
467         }
468
469         Host::Ptr host = Host::GetByName(params->Get("host"));
470
471         if (!host)
472                 return Empty;
473
474         Checkable::Ptr checkable;
475
476         if (params->Contains("service"))
477                 checkable = host->GetServiceByShortName(params->Get("service"));
478         else
479                 checkable = host;
480
481         if (!checkable)
482                 return Empty;
483
484         if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
485                 Log(LogNotice, "ClusterEvents")
486                         << "Discarding 'acknowledgement set' message for checkable '" << checkable->GetName()
487                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
488                 return Empty;
489         }
490
491         checkable->AcknowledgeProblem(params->Get("author"), params->Get("comment"),
492                 static_cast<AcknowledgementType>(static_cast<int>(params->Get("acktype"))),
493                 params->Get("notify"), params->Get("persistent"), params->Get("expiry"), origin);
494
495         return Empty;
496 }
497
498 void ClusterEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
499 {
500         ApiListener::Ptr listener = ApiListener::GetInstance();
501
502         if (!listener)
503                 return;
504
505         Host::Ptr host;
506         Service::Ptr service;
507         tie(host, service) = GetHostService(checkable);
508
509         Dictionary::Ptr params = new Dictionary();
510         params->Set("host", host->GetName());
511         if (service)
512                 params->Set("service", service->GetShortName());
513
514         Dictionary::Ptr message = new Dictionary();
515         message->Set("jsonrpc", "2.0");
516         message->Set("method", "event::ClearAcknowledgement");
517         message->Set("params", params);
518
519         listener->RelayMessage(origin, checkable, message, true);
520 }
521
522 Value ClusterEvents::AcknowledgementClearedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
523 {
524         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
525
526         if (!endpoint) {
527                 Log(LogNotice, "ClusterEvents")
528                         << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
529                 return Empty;
530         }
531
532         Host::Ptr host = Host::GetByName(params->Get("host"));
533
534         if (!host)
535                 return Empty;
536
537         Checkable::Ptr checkable;
538
539         if (params->Contains("service"))
540                 checkable = host->GetServiceByShortName(params->Get("service"));
541         else
542                 checkable = host;
543
544         if (!checkable)
545                 return Empty;
546
547         if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
548                 Log(LogNotice, "ClusterEvents")
549                         << "Discarding 'acknowledgement cleared' message for checkable '" << checkable->GetName()
550                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
551                 return Empty;
552         }
553
554         checkable->ClearAcknowledgement(origin);
555
556         return Empty;
557 }
558
559 Value ClusterEvents::ExecuteCommandAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
560 {
561         EnqueueCheck(origin, params);
562
563         return Empty;
564 }
565
566 void ClusterEvents::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
567         const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr& origin)
568 {
569         ApiListener::Ptr listener = ApiListener::GetInstance();
570
571         if (!listener)
572                 return;
573
574         Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
575         message->Set("method", "event::SendNotifications");
576
577         Dictionary::Ptr params = message->Get("params");
578         params->Set("type", type);
579         params->Set("author", author);
580         params->Set("text", text);
581
582         listener->RelayMessage(origin, nullptr, message, true);
583 }
584
585 Value ClusterEvents::SendNotificationsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
586 {
587         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
588
589         if (!endpoint) {
590                 Log(LogNotice, "ClusterEvents")
591                         << "Discarding 'send notification' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
592                 return Empty;
593         }
594
595         Host::Ptr host = Host::GetByName(params->Get("host"));
596
597         if (!host)
598                 return Empty;
599
600         Checkable::Ptr checkable;
601
602         if (params->Contains("service"))
603                 checkable = host->GetServiceByShortName(params->Get("service"));
604         else
605                 checkable = host;
606
607         if (!checkable)
608                 return Empty;
609
610         if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
611                 Log(LogNotice, "ClusterEvents")
612                         << "Discarding 'send custom notification' message for checkable '" << checkable->GetName()
613                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
614                 return Empty;
615         }
616
617         CheckResult::Ptr cr;
618         Array::Ptr vperf;
619
620         if (params->Contains("cr")) {
621                 cr = new CheckResult();
622                 Dictionary::Ptr vcr = params->Get("cr");
623
624                 if (vcr && vcr->Contains("performance_data")) {
625                         vperf = vcr->Get("performance_data");
626
627                         if (vperf)
628                                 vcr->Remove("performance_data");
629
630                         Deserialize(cr, vcr, true);
631                 }
632         }
633
634         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
635         String author = params->Get("author");
636         String text = params->Get("text");
637
638         Checkable::OnNotificationsRequested(checkable, type, cr, author, text, origin);
639
640         return Empty;
641 }
642
643 void ClusterEvents::NotificationSentUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const User::Ptr& user,
644         NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& command,
645         const MessageOrigin::Ptr& origin)
646 {
647         ApiListener::Ptr listener = ApiListener::GetInstance();
648
649         if (!listener)
650                 return;
651
652         Host::Ptr host;
653         Service::Ptr service;
654         tie(host, service) = GetHostService(checkable);
655
656         Dictionary::Ptr params = new Dictionary();
657         params->Set("host", host->GetName());
658         if (service)
659                 params->Set("service", service->GetShortName());
660         params->Set("notification", notification->GetName());
661         params->Set("user", user->GetName());
662         params->Set("type", notificationType);
663         params->Set("cr", Serialize(cr));
664         params->Set("author", author);
665         params->Set("text", commentText);
666         params->Set("command", command);
667
668         Dictionary::Ptr message = new Dictionary();
669         message->Set("jsonrpc", "2.0");
670         message->Set("method", "event::NotificationSentUser");
671         message->Set("params", params);
672
673         listener->RelayMessage(origin, nullptr, message, true);
674 }
675
676 Value ClusterEvents::NotificationSentUserAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
677 {
678         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
679
680         if (!endpoint) {
681                 Log(LogNotice, "ClusterEvents")
682                         << "Discarding 'sent notification to user' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
683                 return Empty;
684         }
685
686         Host::Ptr host = Host::GetByName(params->Get("host"));
687
688         if (!host)
689                 return Empty;
690
691         Checkable::Ptr checkable;
692
693         if (params->Contains("service"))
694                 checkable = host->GetServiceByShortName(params->Get("service"));
695         else
696                 checkable = host;
697
698         if (!checkable)
699                 return Empty;
700
701         if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
702                 Log(LogNotice, "ClusterEvents")
703                         << "Discarding 'send notification to user' message for checkable '" << checkable->GetName()
704                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
705                 return Empty;
706         }
707
708         CheckResult::Ptr cr;
709         Array::Ptr vperf;
710
711         if (params->Contains("cr")) {
712                 cr = new CheckResult();
713                 Dictionary::Ptr vcr = params->Get("cr");
714
715                 if (vcr && vcr->Contains("performance_data")) {
716                         vperf = vcr->Get("performance_data");
717
718                         if (vperf)
719                                 vcr->Remove("performance_data");
720
721                         Deserialize(cr, vcr, true);
722                 }
723         }
724
725         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
726         String author = params->Get("author");
727         String text = params->Get("text");
728
729         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
730
731         if (!notification)
732                 return Empty;
733
734         User::Ptr user = User::GetByName(params->Get("user"));
735
736         if (!user)
737                 return Empty;
738
739         String command = params->Get("command");
740
741         Checkable::OnNotificationSentToUser(notification, checkable, user, type, cr, author, text, command, origin);
742
743         return Empty;
744 }
745
746 void ClusterEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
747         NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const MessageOrigin::Ptr& origin)
748 {
749         ApiListener::Ptr listener = ApiListener::GetInstance();
750
751         if (!listener)
752                 return;
753
754         Host::Ptr host;
755         Service::Ptr service;
756         tie(host, service) = GetHostService(checkable);
757
758         Dictionary::Ptr params = new Dictionary();
759         params->Set("host", host->GetName());
760         if (service)
761                 params->Set("service", service->GetShortName());
762         params->Set("notification", notification->GetName());
763
764         ArrayData ausers;
765         for (const User::Ptr& user : users) {
766                 ausers.push_back(user->GetName());
767         }
768         params->Set("users", new Array(std::move(ausers)));
769
770         params->Set("type", notificationType);
771         params->Set("cr", Serialize(cr));
772         params->Set("author", author);
773         params->Set("text", commentText);
774
775         params->Set("last_notification", notification->GetLastNotification());
776         params->Set("next_notification", notification->GetNextNotification());
777         params->Set("notification_number", notification->GetNotificationNumber());
778         params->Set("last_problem_notification", notification->GetLastProblemNotification());
779         params->Set("no_more_notifications", notification->GetNoMoreNotifications());
780
781         Dictionary::Ptr message = new Dictionary();
782         message->Set("jsonrpc", "2.0");
783         message->Set("method", "event::NotificationSentToAllUsers");
784         message->Set("params", params);
785
786         listener->RelayMessage(origin, nullptr, message, true);
787 }
788
789 Value ClusterEvents::NotificationSentToAllUsersAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
790 {
791         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
792
793         if (!endpoint) {
794                 Log(LogNotice, "ClusterEvents")
795                         << "Discarding 'sent notification to all users' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
796                 return Empty;
797         }
798
799         Host::Ptr host = Host::GetByName(params->Get("host"));
800
801         if (!host)
802                 return Empty;
803
804         Checkable::Ptr checkable;
805
806         if (params->Contains("service"))
807                 checkable = host->GetServiceByShortName(params->Get("service"));
808         else
809                 checkable = host;
810
811         if (!checkable)
812                 return Empty;
813
814         if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
815                 Log(LogNotice, "ClusterEvents")
816                         << "Discarding 'sent notification to all users' message for checkable '" << checkable->GetName()
817                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
818                 return Empty;
819         }
820
821         CheckResult::Ptr cr;
822         Array::Ptr vperf;
823
824         if (params->Contains("cr")) {
825                 cr = new CheckResult();
826                 Dictionary::Ptr vcr = params->Get("cr");
827
828                 if (vcr && vcr->Contains("performance_data")) {
829                         vperf = vcr->Get("performance_data");
830
831                         if (vperf)
832                                 vcr->Remove("performance_data");
833
834                         Deserialize(cr, vcr, true);
835                 }
836         }
837
838         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
839         String author = params->Get("author");
840         String text = params->Get("text");
841
842         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
843
844         if (!notification)
845                 return Empty;
846
847         Array::Ptr ausers = params->Get("users");
848
849         if (!ausers)
850                 return Empty;
851
852         std::set<User::Ptr> users;
853
854         {
855                 ObjectLock olock(ausers);
856                 for (const String& auser : ausers) {
857                         User::Ptr user = User::GetByName(auser);
858
859                         if (!user)
860                                 continue;
861
862                         users.insert(user);
863                 }
864         }
865
866         notification->SetLastNotification(params->Get("last_notification"));
867         notification->SetNextNotification(params->Get("next_notification"));
868         notification->SetNotificationNumber(params->Get("notification_number"));
869         notification->SetLastProblemNotification(params->Get("last_problem_notification"));
870         notification->SetNoMoreNotifications(params->Get("no_more_notifications"));
871
872         ArrayData notifiedProblemUsers;
873         for (const User::Ptr& user : users) {
874                 notifiedProblemUsers.push_back(user->GetName());
875         }
876
877         notification->SetNotifiedProblemUsers(new Array(std::move(notifiedProblemUsers)));
878
879         Checkable::OnNotificationSentToAllUsers(notification, checkable, users, type, cr, author, text, origin);
880
881         return Empty;
882 }