]> granicus.if.org Git - icinga2/blob - lib/icinga/clusterevents.cpp
Merge pull request #6480 from ajaffie/feature/win-check-update-4720
[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("expiry", expiry);
449
450         Dictionary::Ptr message = new Dictionary();
451         message->Set("jsonrpc", "2.0");
452         message->Set("method", "event::SetAcknowledgement");
453         message->Set("params", params);
454
455         listener->RelayMessage(origin, checkable, message, true);
456 }
457
458 Value ClusterEvents::AcknowledgementSetAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
459 {
460         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
461
462         if (!endpoint) {
463                 Log(LogNotice, "ClusterEvents")
464                         << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
465                 return Empty;
466         }
467
468         Host::Ptr host = Host::GetByName(params->Get("host"));
469
470         if (!host)
471                 return Empty;
472
473         Checkable::Ptr checkable;
474
475         if (params->Contains("service"))
476                 checkable = host->GetServiceByShortName(params->Get("service"));
477         else
478                 checkable = host;
479
480         if (!checkable)
481                 return Empty;
482
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.";
487                 return Empty;
488         }
489
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);
493
494         return Empty;
495 }
496
497 void ClusterEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
498 {
499         ApiListener::Ptr listener = ApiListener::GetInstance();
500
501         if (!listener)
502                 return;
503
504         Host::Ptr host;
505         Service::Ptr service;
506         tie(host, service) = GetHostService(checkable);
507
508         Dictionary::Ptr params = new Dictionary();
509         params->Set("host", host->GetName());
510         if (service)
511                 params->Set("service", service->GetShortName());
512
513         Dictionary::Ptr message = new Dictionary();
514         message->Set("jsonrpc", "2.0");
515         message->Set("method", "event::ClearAcknowledgement");
516         message->Set("params", params);
517
518         listener->RelayMessage(origin, checkable, message, true);
519 }
520
521 Value ClusterEvents::AcknowledgementClearedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
522 {
523         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
524
525         if (!endpoint) {
526                 Log(LogNotice, "ClusterEvents")
527                         << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
528                 return Empty;
529         }
530
531         Host::Ptr host = Host::GetByName(params->Get("host"));
532
533         if (!host)
534                 return Empty;
535
536         Checkable::Ptr checkable;
537
538         if (params->Contains("service"))
539                 checkable = host->GetServiceByShortName(params->Get("service"));
540         else
541                 checkable = host;
542
543         if (!checkable)
544                 return Empty;
545
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.";
550                 return Empty;
551         }
552
553         checkable->ClearAcknowledgement(origin);
554
555         return Empty;
556 }
557
558 Value ClusterEvents::ExecuteCommandAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
559 {
560         EnqueueCheck(origin, params);
561
562         return Empty;
563 }
564
565 void ClusterEvents::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
566         const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr& origin)
567 {
568         ApiListener::Ptr listener = ApiListener::GetInstance();
569
570         if (!listener)
571                 return;
572
573         Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
574         message->Set("method", "event::SendNotifications");
575
576         Dictionary::Ptr params = message->Get("params");
577         params->Set("type", type);
578         params->Set("author", author);
579         params->Set("text", text);
580
581         listener->RelayMessage(origin, nullptr, message, true);
582 }
583
584 Value ClusterEvents::SendNotificationsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
585 {
586         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
587
588         if (!endpoint) {
589                 Log(LogNotice, "ClusterEvents")
590                         << "Discarding 'send notification' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
591                 return Empty;
592         }
593
594         Host::Ptr host = Host::GetByName(params->Get("host"));
595
596         if (!host)
597                 return Empty;
598
599         Checkable::Ptr checkable;
600
601         if (params->Contains("service"))
602                 checkable = host->GetServiceByShortName(params->Get("service"));
603         else
604                 checkable = host;
605
606         if (!checkable)
607                 return Empty;
608
609         if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
610                 Log(LogNotice, "ClusterEvents")
611                         << "Discarding 'send custom notification' message for checkable '" << checkable->GetName()
612                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
613                 return Empty;
614         }
615
616         CheckResult::Ptr cr;
617         Array::Ptr vperf;
618
619         if (params->Contains("cr")) {
620                 cr = new CheckResult();
621                 Dictionary::Ptr vcr = params->Get("cr");
622
623                 if (vcr && vcr->Contains("performance_data")) {
624                         vperf = vcr->Get("performance_data");
625
626                         if (vperf)
627                                 vcr->Remove("performance_data");
628
629                         Deserialize(cr, vcr, true);
630                 }
631         }
632
633         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
634         String author = params->Get("author");
635         String text = params->Get("text");
636
637         Checkable::OnNotificationsRequested(checkable, type, cr, author, text, origin);
638
639         return Empty;
640 }
641
642 void ClusterEvents::NotificationSentUserHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const User::Ptr& user,
643         NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const String& command,
644         const MessageOrigin::Ptr& origin)
645 {
646         ApiListener::Ptr listener = ApiListener::GetInstance();
647
648         if (!listener)
649                 return;
650
651         Host::Ptr host;
652         Service::Ptr service;
653         tie(host, service) = GetHostService(checkable);
654
655         Dictionary::Ptr params = new Dictionary();
656         params->Set("host", host->GetName());
657         if (service)
658                 params->Set("service", service->GetShortName());
659         params->Set("notification", notification->GetName());
660         params->Set("user", user->GetName());
661         params->Set("type", notificationType);
662         params->Set("cr", Serialize(cr));
663         params->Set("author", author);
664         params->Set("text", commentText);
665         params->Set("command", command);
666
667         Dictionary::Ptr message = new Dictionary();
668         message->Set("jsonrpc", "2.0");
669         message->Set("method", "event::NotificationSentUser");
670         message->Set("params", params);
671
672         listener->RelayMessage(origin, nullptr, message, true);
673 }
674
675 Value ClusterEvents::NotificationSentUserAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
676 {
677         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
678
679         if (!endpoint) {
680                 Log(LogNotice, "ClusterEvents")
681                         << "Discarding 'sent notification to user' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
682                 return Empty;
683         }
684
685         Host::Ptr host = Host::GetByName(params->Get("host"));
686
687         if (!host)
688                 return Empty;
689
690         Checkable::Ptr checkable;
691
692         if (params->Contains("service"))
693                 checkable = host->GetServiceByShortName(params->Get("service"));
694         else
695                 checkable = host;
696
697         if (!checkable)
698                 return Empty;
699
700         if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
701                 Log(LogNotice, "ClusterEvents")
702                         << "Discarding 'send notification to user' message for checkable '" << checkable->GetName()
703                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
704                 return Empty;
705         }
706
707         CheckResult::Ptr cr;
708         Array::Ptr vperf;
709
710         if (params->Contains("cr")) {
711                 cr = new CheckResult();
712                 Dictionary::Ptr vcr = params->Get("cr");
713
714                 if (vcr && vcr->Contains("performance_data")) {
715                         vperf = vcr->Get("performance_data");
716
717                         if (vperf)
718                                 vcr->Remove("performance_data");
719
720                         Deserialize(cr, vcr, true);
721                 }
722         }
723
724         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
725         String author = params->Get("author");
726         String text = params->Get("text");
727
728         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
729
730         if (!notification)
731                 return Empty;
732
733         User::Ptr user = User::GetByName(params->Get("user"));
734
735         if (!user)
736                 return Empty;
737
738         String command = params->Get("command");
739
740         Checkable::OnNotificationSentToUser(notification, checkable, user, type, cr, author, text, command, origin);
741
742         return Empty;
743 }
744
745 void ClusterEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable, const std::set<User::Ptr>& users,
746         NotificationType notificationType, const CheckResult::Ptr& cr, const String& author, const String& commentText, const MessageOrigin::Ptr& origin)
747 {
748         ApiListener::Ptr listener = ApiListener::GetInstance();
749
750         if (!listener)
751                 return;
752
753         Host::Ptr host;
754         Service::Ptr service;
755         tie(host, service) = GetHostService(checkable);
756
757         Dictionary::Ptr params = new Dictionary();
758         params->Set("host", host->GetName());
759         if (service)
760                 params->Set("service", service->GetShortName());
761         params->Set("notification", notification->GetName());
762
763         ArrayData ausers;
764         for (const User::Ptr& user : users) {
765                 ausers.push_back(user->GetName());
766         }
767         params->Set("users", new Array(std::move(ausers)));
768
769         params->Set("type", notificationType);
770         params->Set("cr", Serialize(cr));
771         params->Set("author", author);
772         params->Set("text", commentText);
773
774         params->Set("last_notification", notification->GetLastNotification());
775         params->Set("next_notification", notification->GetNextNotification());
776         params->Set("notification_number", notification->GetNotificationNumber());
777         params->Set("last_problem_notification", notification->GetLastProblemNotification());
778         params->Set("no_more_notifications", notification->GetNoMoreNotifications());
779
780         Dictionary::Ptr message = new Dictionary();
781         message->Set("jsonrpc", "2.0");
782         message->Set("method", "event::NotificationSentToAllUsers");
783         message->Set("params", params);
784
785         listener->RelayMessage(origin, nullptr, message, true);
786 }
787
788 Value ClusterEvents::NotificationSentToAllUsersAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
789 {
790         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
791
792         if (!endpoint) {
793                 Log(LogNotice, "ClusterEvents")
794                         << "Discarding 'sent notification to all users' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
795                 return Empty;
796         }
797
798         Host::Ptr host = Host::GetByName(params->Get("host"));
799
800         if (!host)
801                 return Empty;
802
803         Checkable::Ptr checkable;
804
805         if (params->Contains("service"))
806                 checkable = host->GetServiceByShortName(params->Get("service"));
807         else
808                 checkable = host;
809
810         if (!checkable)
811                 return Empty;
812
813         if (origin->FromZone && origin->FromZone != Zone::GetLocalZone()) {
814                 Log(LogNotice, "ClusterEvents")
815                         << "Discarding 'sent notification to all users' message for checkable '" << checkable->GetName()
816                         << "' from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
817                 return Empty;
818         }
819
820         CheckResult::Ptr cr;
821         Array::Ptr vperf;
822
823         if (params->Contains("cr")) {
824                 cr = new CheckResult();
825                 Dictionary::Ptr vcr = params->Get("cr");
826
827                 if (vcr && vcr->Contains("performance_data")) {
828                         vperf = vcr->Get("performance_data");
829
830                         if (vperf)
831                                 vcr->Remove("performance_data");
832
833                         Deserialize(cr, vcr, true);
834                 }
835         }
836
837         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
838         String author = params->Get("author");
839         String text = params->Get("text");
840
841         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
842
843         if (!notification)
844                 return Empty;
845
846         Array::Ptr ausers = params->Get("users");
847
848         if (!ausers)
849                 return Empty;
850
851         std::set<User::Ptr> users;
852
853         {
854                 ObjectLock olock(ausers);
855                 for (const String& auser : ausers) {
856                         User::Ptr user = User::GetByName(auser);
857
858                         if (!user)
859                                 continue;
860
861                         users.insert(user);
862                 }
863         }
864
865         notification->SetLastNotification(params->Get("last_notification"));
866         notification->SetNextNotification(params->Get("next_notification"));
867         notification->SetNotificationNumber(params->Get("notification_number"));
868         notification->SetLastProblemNotification(params->Get("last_problem_notification"));
869         notification->SetNoMoreNotifications(params->Get("no_more_notifications"));
870
871         ArrayData notifiedProblemUsers;
872         for (const User::Ptr& user : users) {
873                 notifiedProblemUsers.push_back(user->GetName());
874         }
875
876         notification->SetNotifiedProblemUsers(new Array(std::move(notifiedProblemUsers)));
877
878         Checkable::OnNotificationSentToAllUsers(notification, checkable, users, type, cr, author, text, origin);
879
880         return Empty;
881 }