]> granicus.if.org Git - icinga2/blob - lib/icinga/clusterevents.cpp
Fix nullptr deref in cluster events
[icinga2] / lib / icinga / clusterevents.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.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(void)
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         Array::Ptr rperf = new Array();
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->Add(val);
148                         } else
149                                 rperf->Add(vp);
150                 }
151         }
152
153         cr->SetPerformanceData(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         Endpoint::Ptr sourceEndpoint = origin->FromClient->GetEndpoint();
561
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).";
565                 return Empty;
566         }
567
568         ApiListener::Ptr listener = ApiListener::GetInstance();
569
570         if (!listener) {
571                 Log(LogCritical, "ApiListener", "No instance available.");
572                 return Empty;
573         }
574
575         if (!listener->GetAcceptCommands()) {
576                 Log(LogWarning, "ApiListener")
577                     << "Ignoring command. '" << listener->GetName() << "' does not accept commands.";
578
579                 Host::Ptr host = new Host();
580                 Dictionary::Ptr attrs = new Dictionary();
581
582                 attrs->Set("__name", params->Get("host"));
583                 attrs->Set("type", "Host");
584                 attrs->Set("enable_active_checks", false);
585
586                 Deserialize(host, attrs, false, FAConfig);
587
588                 if (params->Contains("service"))
589                         host->SetExtension("agent_service_name", params->Get("service"));
590
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);
596
597                 return Empty;
598         }
599
600         /* use a virtual host object for executing the command */
601         Host::Ptr host = new Host();
602         Dictionary::Ptr attrs = new Dictionary();
603
604         attrs->Set("__name", params->Get("host"));
605         attrs->Set("type", "Host");
606
607         Deserialize(host, attrs, false, FAConfig);
608
609         if (params->Contains("service"))
610                 host->SetExtension("agent_service_name", params->Get("service"));
611
612         String command = params->Get("command");
613         String command_type = params->Get("command_type");
614
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);
622                         return Empty;
623                 }
624         } else if (command_type == "event_command") {
625                 if (!EventCommand::GetByName(command)) {
626                         Log(LogWarning, "ClusterEvents")
627                             << "Event command '" << command << "' does not exist.";
628                         return Empty;
629                 }
630         } else
631                 return Empty;
632
633         attrs->Set(command_type, params->Get("command"));
634         attrs->Set("command_endpoint", sourceEndpoint->GetName());
635
636         Deserialize(host, attrs, false, FAConfig);
637
638         host->SetExtension("agent_check", true);
639
640         Dictionary::Ptr macros = params->Get("macros");
641
642         if (command_type == "check_command") {
643                 try {
644                         host->ExecuteRemoteCheck(macros);
645                 } catch (const std::exception& ex) {
646                         CheckResult::Ptr cr = new CheckResult();
647                         cr->SetState(ServiceUnknown);
648
649                         String output = "Exception occured while checking '" + host->GetName() + "': " + DiagnosticInformation(ex);
650                         cr->SetOutput(output);
651
652                         double now = Utility::GetTime();
653                         cr->SetScheduleStart(now);
654                         cr->SetScheduleEnd(now);
655                         cr->SetExecutionStart(now);
656                         cr->SetExecutionEnd(now);
657
658                         Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
659                         listener->SyncSendMessage(sourceEndpoint, message);
660
661                         Log(LogCritical, "checker", output);
662                 }
663         } else if (command_type == "event_command") {
664                 host->ExecuteEventHandler(macros, true);
665         }
666
667         return Empty;
668 }
669
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)
672 {
673         ApiListener::Ptr listener = ApiListener::GetInstance();
674
675         if (!listener)
676                 return;
677
678         Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
679         message->Set("method", "event::SendNotifications");
680
681         Dictionary::Ptr params = message->Get("params");
682         params->Set("type", type);
683         params->Set("author", author);
684         params->Set("text", text);
685
686         listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
687 }
688
689 Value ClusterEvents::SendNotificationsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
690 {
691         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
692
693         if (!endpoint) {
694                 Log(LogNotice, "ClusterEvents")
695                     << "Discarding 'send notification' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
696                 return Empty;
697         }
698
699         Host::Ptr host = Host::GetByName(params->Get("host"));
700
701         if (!host)
702                 return Empty;
703
704         Checkable::Ptr checkable;
705
706         if (params->Contains("service"))
707                 checkable = host->GetServiceByShortName(params->Get("service"));
708         else
709                 checkable = host;
710
711         if (!checkable)
712                 return Empty;
713
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.";
718                 return Empty;
719         }
720
721         CheckResult::Ptr cr;
722         Array::Ptr vperf;
723
724         if (params->Contains("cr")) {
725                 cr = new CheckResult();
726                 Dictionary::Ptr vcr = params->Get("cr");
727
728                 if (vcr && vcr->Contains("performance_data")) {
729                         vperf = vcr->Get("performance_data");
730
731                         if (vperf)
732                                 vcr->Remove("performance_data");
733
734                         Deserialize(cr, vcr, true);
735                 }
736         }
737
738         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
739         String author = params->Get("author");
740         String text = params->Get("text");
741
742         Checkable::OnNotificationsRequested(checkable, type, cr, author, text, origin);
743
744         return Empty;
745 }
746
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)
750 {
751         ApiListener::Ptr listener = ApiListener::GetInstance();
752
753         if (!listener)
754                 return;
755
756         Host::Ptr host;
757         Service::Ptr service;
758         tie(host, service) = GetHostService(checkable);
759
760         Dictionary::Ptr params = new Dictionary();
761         params->Set("host", host->GetName());
762         if (service)
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);
771
772         Dictionary::Ptr message = new Dictionary();
773         message->Set("jsonrpc", "2.0");
774         message->Set("method", "event::NotificationSentUser");
775         message->Set("params", params);
776
777         listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
778 }
779
780 Value ClusterEvents::NotificationSentUserAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
781 {
782         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
783
784         if (!endpoint) {
785                 Log(LogNotice, "ClusterEvents")
786                     << "Discarding 'sent notification to user' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
787                 return Empty;
788         }
789
790         Host::Ptr host = Host::GetByName(params->Get("host"));
791
792         if (!host)
793                 return Empty;
794
795         Checkable::Ptr checkable;
796
797         if (params->Contains("service"))
798                 checkable = host->GetServiceByShortName(params->Get("service"));
799         else
800                 checkable = host;
801
802         if (!checkable)
803                 return Empty;
804
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.";
809                 return Empty;
810         }
811
812         CheckResult::Ptr cr;
813         Array::Ptr vperf;
814
815         if (params->Contains("cr")) {
816                 cr = new CheckResult();
817                 Dictionary::Ptr vcr = params->Get("cr");
818
819                 if (vcr && vcr->Contains("performance_data")) {
820                         vperf = vcr->Get("performance_data");
821
822                         if (vperf)
823                                 vcr->Remove("performance_data");
824
825                         Deserialize(cr, vcr, true);
826                 }
827         }
828
829         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
830         String author = params->Get("author");
831         String text = params->Get("text");
832
833         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
834
835         if (!notification)
836                 return Empty;
837
838         User::Ptr user = User::GetByName(params->Get("user"));
839
840         if (!user)
841                 return Empty;
842
843         String command = params->Get("command");
844
845         Checkable::OnNotificationSentToUser(notification, checkable, user, type, cr, author, text, command, origin);
846
847         return Empty;
848 }
849
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)
852 {
853         ApiListener::Ptr listener = ApiListener::GetInstance();
854
855         if (!listener)
856                 return;
857
858         Host::Ptr host;
859         Service::Ptr service;
860         tie(host, service) = GetHostService(checkable);
861
862         Dictionary::Ptr params = new Dictionary();
863         params->Set("host", host->GetName());
864         if (service)
865                 params->Set("service", service->GetShortName());
866         params->Set("notification", notification->GetName());
867
868         Array::Ptr ausers = new Array();
869         for (const User::Ptr& user : users) {
870                 ausers->Add(user->GetName());
871         }
872         params->Set("users", ausers);
873
874         params->Set("type", notificationType);
875         params->Set("cr", Serialize(cr));
876         params->Set("author", author);
877         params->Set("text", commentText);
878
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());
884
885         Dictionary::Ptr message = new Dictionary();
886         message->Set("jsonrpc", "2.0");
887         message->Set("method", "event::NotificationSentToAllUsers");
888         message->Set("params", params);
889
890         listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
891 }
892
893 Value ClusterEvents::NotificationSentToAllUsersAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
894 {
895         Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
896
897         if (!endpoint) {
898                 Log(LogNotice, "ClusterEvents")
899                     << "Discarding 'sent notification to all users' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
900                 return Empty;
901         }
902
903         Host::Ptr host = Host::GetByName(params->Get("host"));
904
905         if (!host)
906                 return Empty;
907
908         Checkable::Ptr checkable;
909
910         if (params->Contains("service"))
911                 checkable = host->GetServiceByShortName(params->Get("service"));
912         else
913                 checkable = host;
914
915         if (!checkable)
916                 return Empty;
917
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.";
922                 return Empty;
923         }
924
925         CheckResult::Ptr cr;
926         Array::Ptr vperf;
927
928         if (params->Contains("cr")) {
929                 cr = new CheckResult();
930                 Dictionary::Ptr vcr = params->Get("cr");
931
932                 if (vcr && vcr->Contains("performance_data")) {
933                         vperf = vcr->Get("performance_data");
934
935                         if (vperf)
936                                 vcr->Remove("performance_data");
937
938                         Deserialize(cr, vcr, true);
939                 }
940         }
941
942         NotificationType type = static_cast<NotificationType>(static_cast<int>(params->Get("type")));
943         String author = params->Get("author");
944         String text = params->Get("text");
945
946         Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
947
948         if (!notification)
949                 return Empty;
950
951         Array::Ptr ausers = params->Get("users");
952
953         if (!ausers)
954                 return Empty;
955
956         std::set<User::Ptr> users;
957
958         {
959                 ObjectLock olock(ausers);
960                 for (const String& auser : ausers) {
961                         User::Ptr user = User::GetByName(auser);
962
963                         if (!user)
964                                 continue;
965
966                         users.insert(user);
967                 }
968         }
969
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"));
975
976         Array::Ptr notifiedProblemUsers = new Array();
977         for (const User::Ptr& user : users) {
978                 notifiedProblemUsers->Add(user->GetName());
979         }
980
981         notification->SetNotifiedProblemUsers(notifiedProblemUsers);
982
983         Checkable::OnNotificationSentToAllUsers(notification, checkable, users, type, cr, author, text, origin);
984
985         return Empty;
986 }