1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "i2-icinga.h"
22 using namespace icinga;
24 bool I2_EXPORT ExternalCommandProcessor::m_Initialized;
25 map<String, ExternalCommandProcessor::Callback> I2_EXPORT ExternalCommandProcessor::m_Commands;
27 void ExternalCommandProcessor::Execute(const String& line)
33 throw_exception(invalid_argument("Missing timestamp in command: " + line));
35 size_t pos = line.FindFirstOf("]");
37 if (pos == String::NPos)
38 throw_exception(invalid_argument("Missing timestamp in command: " + line));
40 String timestamp = line.SubStr(1, pos - 1);
41 String args = line.SubStr(pos + 2, String::NPos);
43 double ts = Convert::ToDouble(timestamp);
46 throw_exception(invalid_argument("Invalid timestamp in command: " + line));
48 vector<String> argv = args.Split(is_any_of(";"));
51 throw_exception(invalid_argument("Missing arguments in command: " + line));
53 vector<String> argvExtra(argv.begin() + 1, argv.end());
54 Execute(ts, argv[0], argvExtra);
57 void ExternalCommandProcessor::Execute(double time, const String& command, const vector<String>& arguments)
60 RegisterCommand("PROCESS_SERVICE_CHECK_RESULT", &ExternalCommandProcessor::ProcessServiceCheckResult);
61 RegisterCommand("SCHEDULE_SVC_CHECK", &ExternalCommandProcessor::ScheduleSvcCheck);
62 RegisterCommand("SCHEDULE_FORCED_SVC_CHECK", &ExternalCommandProcessor::ScheduleForcedSvcCheck);
63 RegisterCommand("ENABLE_SVC_CHECK", &ExternalCommandProcessor::EnableSvcCheck);
64 RegisterCommand("DISABLE_SVC_CHECK", &ExternalCommandProcessor::DisableSvcCheck);
65 RegisterCommand("SHUTDOWN_PROCESS", &ExternalCommandProcessor::ShutdownProcess);
66 RegisterCommand("SCHEDULE_FORCED_HOST_SVC_CHECKS", &ExternalCommandProcessor::ScheduleForcedHostSvcChecks);
67 RegisterCommand("SCHEDULE_HOST_SVC_CHECKS", &ExternalCommandProcessor::ScheduleHostSvcChecks);
68 RegisterCommand("ENABLE_HOST_SVC_CHECKS", &ExternalCommandProcessor::EnableHostSvcChecks);
69 RegisterCommand("DISABLE_HOST_SVC_CHECKS", &ExternalCommandProcessor::DisableHostSvcChecks);
70 RegisterCommand("ACKNOWLEDGE_SVC_PROBLEM", &ExternalCommandProcessor::AcknowledgeSvcProblem);
71 RegisterCommand("ACKNOWLEDGE_SVC_PROBLEM_EXPIRE", &ExternalCommandProcessor::AcknowledgeSvcProblemExpire);
72 RegisterCommand("REMOVE_SVC_ACKNOWLEDGEMENT", &ExternalCommandProcessor::RemoveHostAcknowledgement);
73 RegisterCommand("ACKNOWLEDGE_HOST_PROBLEM", &ExternalCommandProcessor::AcknowledgeHostProblem);
74 RegisterCommand("ACKNOWLEDGE_HOST_PROBLEM_EXPIRE", &ExternalCommandProcessor::AcknowledgeHostProblemExpire);
75 RegisterCommand("REMOVE_HOST_ACKNOWLEDGEMENT", &ExternalCommandProcessor::RemoveHostAcknowledgement);
76 RegisterCommand("ENABLE_HOSTGROUP_SVC_CHECKS", &ExternalCommandProcessor::EnableHostgroupSvcChecks);
77 RegisterCommand("DISABLE_HOSTGROUP_SVC_CHECKS", &ExternalCommandProcessor::DisableHostgroupSvcChecks);
78 RegisterCommand("ENABLE_SERVICEGROUP_SVC_CHECKS", &ExternalCommandProcessor::EnableServicegroupSvcChecks);
79 RegisterCommand("DISABLE_SERVICEGROUP_SVC_CHECKS", &ExternalCommandProcessor::DisableServicegroupSvcChecks);
80 RegisterCommand("ENABLE_PASSIVE_SVC_CHECKS", &ExternalCommandProcessor::EnablePassiveSvcChecks);
81 RegisterCommand("DISABLE_PASSIVE_SVC_CHECKS", &ExternalCommandProcessor::DisablePassiveSvcChecks);
82 RegisterCommand("ENABLE_SERVICEGROUP_PASSIVE_SVC_CHECKS", &ExternalCommandProcessor::EnableServicegroupPassiveSvcChecks);
83 RegisterCommand("DISABLE_SERVICEGROUP_PASSIVE_SVC_CHECKS", &ExternalCommandProcessor::DisableServicegroupPassiveSvcChecks);
84 RegisterCommand("ENABLE_HOSTGROUP_PASSIVE_SVC_CHECKS", &ExternalCommandProcessor::EnableHostgroupPassiveSvcChecks);
85 RegisterCommand("DISABLE_HOSTGROUP_PASSIVE_SVC_CHECKS", &ExternalCommandProcessor::DisableHostgroupPassiveSvcChecks);
86 RegisterCommand("PROCESS_FILE", &ExternalCommandProcessor::ProcessFile);
87 RegisterCommand("SCHEDULE_SVC_DOWNTIME", &ExternalCommandProcessor::ScheduleSvcDowntime);
88 RegisterCommand("DEL_SVC_DOWNTIME", &ExternalCommandProcessor::DelSvcDowntime);
89 RegisterCommand("SCHEDULE_HOST_DOWNTIME", &ExternalCommandProcessor::ScheduleHostDowntime);
90 RegisterCommand("DEL_HOST_DOWNTIME", &ExternalCommandProcessor::DelHostDowntime);
91 RegisterCommand("SCHEDULE_HOST_SVC_DOWNTIME", &ExternalCommandProcessor::ScheduleHostSvcDowntime);
92 RegisterCommand("SCHEDULE_HOSTGROUP_HOST_DOWNTIME", &ExternalCommandProcessor::ScheduleHostgroupHostDowntime);
93 RegisterCommand("SCHEDULE_HOSTGROUP_SVC_DOWNTIME", &ExternalCommandProcessor::ScheduleHostgroupSvcDowntime);
94 RegisterCommand("SCHEDULE_SERVICEGROUP_HOST_DOWNTIME", &ExternalCommandProcessor::ScheduleServicegroupHostDowntime);
95 RegisterCommand("SCHEDULE_SERVICEGROUP_SVC_DOWNTIME", &ExternalCommandProcessor::ScheduleServicegroupSvcDowntime);
96 RegisterCommand("ADD_HOST_COMMENT", &ExternalCommandProcessor::AddHostComment);
97 RegisterCommand("DEL_HOST_COMMENT", &ExternalCommandProcessor::DelHostComment);
98 RegisterCommand("ADD_SVC_COMMENT", &ExternalCommandProcessor::AddSvcComment);
99 RegisterCommand("DEL_SVC_COMMENT", &ExternalCommandProcessor::DelSvcComment);
100 RegisterCommand("DEL_ALL_HOST_COMMENTS", &ExternalCommandProcessor::DelAllHostComments);
101 RegisterCommand("DEL_ALL_SVC_COMMENTS", &ExternalCommandProcessor::DelAllSvcComments);
103 m_Initialized = true;
106 map<String, ExternalCommandProcessor::Callback>::iterator it;
107 it = m_Commands.find(command);
109 if (it == m_Commands.end())
110 throw_exception(invalid_argument("The external command '" + command + "' does not exist."));
112 it->second(time, arguments);
115 void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandProcessor::Callback& callback)
117 m_Commands[command] = callback;
120 void ExternalCommandProcessor::ProcessServiceCheckResult(double time, const vector<String>& arguments)
122 if (arguments.size() < 4)
123 throw_exception(invalid_argument("Expected 4 arguments."));
125 if (!Service::Exists(arguments[1]))
126 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
128 Service::Ptr service = Service::GetByName(arguments[1]);
130 if (!service->GetEnablePassiveChecks())
131 throw_exception(invalid_argument("Got passive check result for service '" + arguments[1] + "' which has passive checks disabled."));
133 int exitStatus = Convert::ToDouble(arguments[2]);
134 Dictionary::Ptr result = PluginCheckTask::ParseCheckOutput(arguments[3]);
135 result->Set("state", PluginCheckTask::ExitStatusToState(exitStatus));
137 result->Set("schedule_start", time);
138 result->Set("schedule_end", time);
139 result->Set("execution_start", time);
140 result->Set("execution_end", time);
141 result->Set("active", 0);
143 Logger::Write(LogInformation, "icinga", "Processing passive check result for service '" + arguments[1] + "'");
144 service->ProcessCheckResult(result);
146 /* Reschedule the next check. The side effect of this is that for as long
147 * as we receive passive results for a service we won't execute any
149 service->SetNextCheck(Utility::GetTime() + service->GetCheckInterval());
152 void ExternalCommandProcessor::ScheduleSvcCheck(double, const vector<String>& arguments)
154 if (arguments.size() < 3)
155 throw_exception(invalid_argument("Expected 3 arguments."));
157 if (!Service::Exists(arguments[1]))
158 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
160 Service::Ptr service = Service::GetByName(arguments[1]);
162 double planned_check = Convert::ToDouble(arguments[2]);
164 if (planned_check > service->GetNextCheck()) {
165 Logger::Write(LogInformation, "icinga", "Ignoring reschedule request for service '" +
166 arguments[1] + "' (next check is already sooner than requested check time)");
170 Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + arguments[1] + "'");
171 service->SetNextCheck(planned_check);
174 void ExternalCommandProcessor::ScheduleForcedSvcCheck(double, const vector<String>& arguments)
176 if (arguments.size() < 3)
177 throw_exception(invalid_argument("Expected 3 arguments."));
179 if (!Service::Exists(arguments[1]))
180 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
182 Service::Ptr service = Service::GetByName(arguments[1]);
184 Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + arguments[1] + "'");
185 service->SetForceNextCheck(true);
186 service->SetNextCheck(Convert::ToDouble(arguments[2]));
189 void ExternalCommandProcessor::EnableSvcCheck(double, const vector<String>& arguments)
191 if (arguments.size() < 2)
192 throw_exception(invalid_argument("Expected 2 arguments."));
194 if (!Service::Exists(arguments[1]))
195 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
197 Service::Ptr service = Service::GetByName(arguments[1]);
199 Logger::Write(LogInformation, "icinga", "Enabling active checks for service '" + arguments[1] + "'");
200 service->SetEnableActiveChecks(true);
203 void ExternalCommandProcessor::DisableSvcCheck(double, const vector<String>& arguments)
205 if (arguments.size() < 2)
206 throw_exception(invalid_argument("Expected 2 arguments."));
208 if (!Service::Exists(arguments[1]))
209 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
211 Service::Ptr service = Service::GetByName(arguments[1]);
213 Logger::Write(LogInformation, "icinga", "Disabling active checks for service '" + arguments[1] + "'");
214 service->SetEnableActiveChecks(false);
217 void ExternalCommandProcessor::ShutdownProcess(double, const vector<String>&)
219 Logger::Write(LogInformation, "icinga", "Shutting down Icinga via external command.");
220 Application::RequestShutdown();
223 void ExternalCommandProcessor::ScheduleForcedHostSvcChecks(double, const vector<String>& arguments)
225 if (arguments.size() < 2)
226 throw_exception(invalid_argument("Expected 2 arguments."));
228 if (!Host::Exists(arguments[0]))
229 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
231 double planned_check = Convert::ToDouble(arguments[1]);
233 Host::Ptr host = Host::GetByName(arguments[0]);
235 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
236 Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + service->GetName() + "'");
237 service->SetNextCheck(planned_check);
238 service->SetForceNextCheck(true);
242 void ExternalCommandProcessor::ScheduleHostSvcChecks(double, const vector<String>& arguments)
244 if (arguments.size() < 2)
245 throw_exception(invalid_argument("Expected 2 arguments."));
247 if (!Host::Exists(arguments[0]))
248 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
250 double planned_check = Convert::ToDouble(arguments[1]);
252 Host::Ptr host = Host::GetByName(arguments[0]);
254 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
255 if (planned_check > service->GetNextCheck()) {
256 Logger::Write(LogInformation, "icinga", "Ignoring reschedule request for service '" +
257 service->GetName() + "' (next check is already sooner than requested check time)");
261 Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + service->GetName() + "'");
262 service->SetNextCheck(planned_check);
266 void ExternalCommandProcessor::EnableHostSvcChecks(double, const vector<String>& arguments)
268 if (arguments.size() < 1)
269 throw_exception(invalid_argument("Expected 1 argument."));
271 if (!Host::Exists(arguments[0]))
272 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
274 Host::Ptr host = Host::GetByName(arguments[0]);
276 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
277 Logger::Write(LogInformation, "icinga", "Enabling active checks for service '" + service->GetName() + "'");
278 service->SetEnableActiveChecks(true);
282 void ExternalCommandProcessor::DisableHostSvcChecks(double, const vector<String>& arguments)
284 if (arguments.size() < 1)
285 throw_exception(invalid_argument("Expected 1 arguments."));
287 if (!Host::Exists(arguments[0]))
288 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
290 Host::Ptr host = Host::GetByName(arguments[0]);
292 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
293 Logger::Write(LogInformation, "icinga", "Disabling active checks for service '" + service->GetName() + "'");
294 service->SetEnableActiveChecks(false);
298 void ExternalCommandProcessor::AcknowledgeSvcProblem(double, const vector<String>& arguments)
300 if (arguments.size() < 7)
301 throw_exception(invalid_argument("Expected 7 arguments."));
303 if (!Service::Exists(arguments[1]))
304 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
306 bool sticky = Convert::ToBool(arguments[2]);
308 Service::Ptr service = Service::GetByName(arguments[1]);
310 if (service->GetState() == StateOK)
311 throw_exception(invalid_argument("The service '" + arguments[1] + "' is OK."));
313 Logger::Write(LogInformation, "icinga", "Setting acknowledgement for service '" + service->GetName() + "'");
314 service->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal);
315 service->SetAcknowledgementExpiry(0);
318 void ExternalCommandProcessor::AcknowledgeSvcProblemExpire(double, const vector<String>& arguments)
320 if (arguments.size() < 8)
321 throw_exception(invalid_argument("Expected 8 arguments."));
323 if (!Service::Exists(arguments[1]))
324 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
326 bool sticky = Convert::ToBool(arguments[2]);
327 double timestamp = Convert::ToDouble(arguments[5]);
329 Service::Ptr service = Service::GetByName(arguments[1]);
331 if (service->GetState() == StateOK)
332 throw_exception(invalid_argument("The service '" + arguments[1] + "' is OK."));
334 Logger::Write(LogInformation, "icinga", "Setting timed acknowledgement for service '" + service->GetName() + "'");
335 service->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal);
336 service->SetAcknowledgementExpiry(timestamp);
339 void ExternalCommandProcessor::RemoveSvcAcknowledgement(double, const vector<String>& arguments)
341 if (arguments.size() < 2)
342 throw_exception(invalid_argument("Expected 2 arguments."));
344 if (!Service::Exists(arguments[1]))
345 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
347 Service::Ptr service = Service::GetByName(arguments[1]);
349 Logger::Write(LogInformation, "icinga", "Removing acknowledgement for service '" + service->GetName() + "'");
350 service->SetAcknowledgement(AcknowledgementNone);
351 service->SetAcknowledgementExpiry(0);
354 void ExternalCommandProcessor::AcknowledgeHostProblem(double, const vector<String>& arguments)
356 if (arguments.size() < 6)
357 throw_exception(invalid_argument("Expected 6 arguments."));
359 if (!Host::Exists(arguments[0]))
360 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
362 bool sticky = Convert::ToBool(arguments[0]);
364 Host::Ptr host = Host::GetByName(arguments[0]);
367 throw_exception(invalid_argument("The host '" + arguments[0] + "' is OK."));
369 Logger::Write(LogInformation, "icinga", "Setting acknowledgement for host '" + host->GetName() + "'");
370 host->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal);
371 host->SetAcknowledgementExpiry(0);
374 void ExternalCommandProcessor::AcknowledgeHostProblemExpire(double, const vector<String>& arguments)
376 if (arguments.size() < 7)
377 throw_exception(invalid_argument("Expected 7 arguments."));
379 if (!Host::Exists(arguments[0]))
380 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
382 bool sticky = Convert::ToBool(arguments[1]);
383 double timestamp = Convert::ToDouble(arguments[4]);
385 Host::Ptr host = Host::GetByName(arguments[0]);
388 throw_exception(invalid_argument("The host '" + arguments[0] + "' is OK."));
390 Logger::Write(LogInformation, "icinga", "Setting timed acknowledgement for host '" + host->GetName() + "'");
391 host->SetAcknowledgement(sticky ? AcknowledgementSticky : AcknowledgementNormal);
392 host->SetAcknowledgementExpiry(timestamp);
395 void ExternalCommandProcessor::RemoveHostAcknowledgement(double, const vector<String>& arguments)
397 if (arguments.size() < 1)
398 throw_exception(invalid_argument("Expected 1 argument."));
400 if (!Host::Exists(arguments[0]))
401 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
403 Host::Ptr host = Host::GetByName(arguments[0]);
405 Logger::Write(LogInformation, "icinga", "Removing acknowledgement for host '" + host->GetName() + "'");
406 host->SetAcknowledgement(AcknowledgementNone);
407 host->SetAcknowledgementExpiry(0);
410 void ExternalCommandProcessor::EnableHostgroupSvcChecks(double, const vector<String>& arguments)
412 if (arguments.size() < 1)
413 throw_exception(invalid_argument("Expected 1 argument."));
415 if (!HostGroup::Exists(arguments[0]))
416 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
418 HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
420 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
421 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
422 Logger::Write(LogInformation, "icinga", "Enabling active checks for service '" + service->GetName() + "'");
423 service->SetEnableActiveChecks(true);
428 void ExternalCommandProcessor::DisableHostgroupSvcChecks(double, const vector<String>& arguments)
430 if (arguments.size() < 1)
431 throw_exception(invalid_argument("Expected 1 argument."));
433 if (!HostGroup::Exists(arguments[0]))
434 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
436 HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
438 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
439 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
440 Logger::Write(LogInformation, "icinga", "Disabling active checks for service '" + service->GetName() + "'");
441 service->SetEnableActiveChecks(false);
446 void ExternalCommandProcessor::EnableServicegroupSvcChecks(double, const vector<String>& arguments)
448 if (arguments.size() < 1)
449 throw_exception(invalid_argument("Expected 1 argument."));
451 if (!ServiceGroup::Exists(arguments[0]))
452 throw_exception(invalid_argument("The service group '" + arguments[0] + "' does not exist."));
454 ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
456 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
457 Logger::Write(LogInformation, "icinga", "Enabling active checks for service '" + service->GetName() + "'");
458 service->SetEnableActiveChecks(true);
462 void ExternalCommandProcessor::DisableServicegroupSvcChecks(double, const vector<String>& arguments)
464 if (arguments.size() < 1)
465 throw_exception(invalid_argument("Expected 1 argument."));
467 if (!ServiceGroup::Exists(arguments[0]))
468 throw_exception(invalid_argument("The service group '" + arguments[0] + "' does not exist."));
470 ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
472 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
473 Logger::Write(LogInformation, "icinga", "Disabling active checks for service '" + service->GetName() + "'");
474 service->SetEnableActiveChecks(false);
478 void ExternalCommandProcessor::EnablePassiveSvcChecks(double, const vector<String>& arguments)
480 if (arguments.size() < 2)
481 throw_exception(invalid_argument("Expected 2 arguments."));
483 if (!Service::Exists(arguments[1]))
484 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
486 Service::Ptr service = Service::GetByName(arguments[1]);
488 Logger::Write(LogInformation, "icinga", "Enabling passive checks for service '" + arguments[1] + "'");
489 service->SetEnablePassiveChecks(true);
492 void ExternalCommandProcessor::DisablePassiveSvcChecks(double, const vector<String>& arguments)
494 if (arguments.size() < 2)
495 throw_exception(invalid_argument("Expected 2 arguments."));
497 if (!Service::Exists(arguments[1]))
498 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
500 Service::Ptr service = Service::GetByName(arguments[1]);
502 Logger::Write(LogInformation, "icinga", "Disabling passive checks for service '" + arguments[1] + "'");
503 service->SetEnablePassiveChecks(false);
506 void ExternalCommandProcessor::EnableServicegroupPassiveSvcChecks(double, const vector<String>& arguments)
508 if (arguments.size() < 1)
509 throw_exception(invalid_argument("Expected 1 argument."));
511 if (!ServiceGroup::Exists(arguments[0]))
512 throw_exception(invalid_argument("The service group '" + arguments[0] + "' does not exist."));
514 ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
516 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
517 Logger::Write(LogInformation, "icinga", "Enabling passive checks for service '" + service->GetName() + "'");
518 service->SetEnablePassiveChecks(true);
522 void ExternalCommandProcessor::DisableServicegroupPassiveSvcChecks(double, const vector<String>& arguments)
524 if (arguments.size() < 1)
525 throw_exception(invalid_argument("Expected 1 argument."));
527 if (!ServiceGroup::Exists(arguments[0]))
528 throw_exception(invalid_argument("The service group '" + arguments[0] + "' does not exist."));
530 ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
532 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
533 Logger::Write(LogInformation, "icinga", "Disabling passive checks for service '" + service->GetName() + "'");
534 service->SetEnablePassiveChecks(true);
538 void ExternalCommandProcessor::EnableHostgroupPassiveSvcChecks(double, const vector<String>& arguments)
540 if (arguments.size() < 1)
541 throw_exception(invalid_argument("Expected 1 argument."));
543 if (!HostGroup::Exists(arguments[0]))
544 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
546 HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
548 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
549 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
550 Logger::Write(LogInformation, "icinga", "Enabling passive checks for service '" + service->GetName() + "'");
551 service->SetEnablePassiveChecks(true);
556 void ExternalCommandProcessor::DisableHostgroupPassiveSvcChecks(double, const vector<String>& arguments)
558 if (arguments.size() < 1)
559 throw_exception(invalid_argument("Expected 1 argument."));
561 if (!HostGroup::Exists(arguments[0]))
562 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
564 HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
566 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
567 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
568 Logger::Write(LogInformation, "icinga", "Disabling passive checks for service '" + service->GetName() + "'");
569 service->SetEnablePassiveChecks(false);
574 void ExternalCommandProcessor::ProcessFile(double, const vector<String>& arguments)
576 if (arguments.size() < 2)
577 throw_exception(invalid_argument("Expected 2 arguments."));
579 String file = arguments[0];
580 bool del = Convert::ToBool(arguments[1]);
583 ifp.exceptions(ifstream::badbit);
585 ifp.open(file.CStr(), ifstream::in);
589 std::getline(ifp, line);
592 Logger::Write(LogInformation, "compat", "Executing external command: " + line);
595 } catch (const exception& ex) {
597 msgbuf << "External command failed: " << ex.what();
598 Logger::Write(LogWarning, "icinga", msgbuf.str());
605 (void) unlink(file.CStr());
608 void ExternalCommandProcessor::ScheduleSvcDowntime(double, const vector<String>& arguments)
610 if (arguments.size() < 9)
611 throw_exception(invalid_argument("Expected 9 arguments."));
613 if (!Service::Exists(arguments[1]))
614 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
616 Service::Ptr service = Service::GetByName(arguments[1]);
619 int triggeredByLegacy = Convert::ToLong(arguments[5]);
620 if (triggeredByLegacy != 0)
621 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
623 Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName());
624 (void) DowntimeProcessor::AddDowntime(service, arguments[7], arguments[8],
625 Convert::ToDouble(arguments[2]), Convert::ToDouble(arguments[3]),
626 Convert::ToBool(arguments[4]), triggeredBy, Convert::ToDouble(arguments[6]));
629 void ExternalCommandProcessor::DelSvcDowntime(double, const vector<String>& arguments)
631 if (arguments.size() < 1)
632 throw_exception(invalid_argument("Expected 1 argument."));
634 int id = Convert::ToLong(arguments[0]);
635 Logger::Write(LogInformation, "icinga", "Removing downtime ID " + arguments[0]);
636 String rid = DowntimeProcessor::GetIDFromLegacyID(id);
637 DowntimeProcessor::RemoveDowntime(rid);
640 void ExternalCommandProcessor::ScheduleHostDowntime(double, const vector<String>& arguments)
642 if (arguments.size() < 8)
643 throw_exception(invalid_argument("Expected 8 arguments."));
645 if (!Host::Exists(arguments[0]))
646 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
648 Host::Ptr host = Host::GetByName(arguments[0]);
651 int triggeredByLegacy = Convert::ToLong(arguments[4]);
652 if (triggeredByLegacy != 0)
653 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
655 Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName());
656 (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7],
657 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
658 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
661 void ExternalCommandProcessor::DelHostDowntime(double, const vector<String>& arguments)
663 if (arguments.size() < 1)
664 throw_exception(invalid_argument("Expected 1 argument."));
666 int id = Convert::ToLong(arguments[0]);
667 Logger::Write(LogInformation, "icinga", "Removing downtime ID " + arguments[0]);
668 String rid = DowntimeProcessor::GetIDFromLegacyID(id);
669 DowntimeProcessor::RemoveDowntime(rid);
672 void ExternalCommandProcessor::ScheduleHostSvcDowntime(double, const vector<String>& arguments)
674 if (arguments.size() < 8)
675 throw_exception(invalid_argument("Expected 8 argument."));
677 if (!Host::Exists(arguments[0]))
678 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
680 Host::Ptr host = Host::GetByName(arguments[0]);
683 int triggeredByLegacy = Convert::ToLong(arguments[4]);
684 if (triggeredByLegacy != 0)
685 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
687 Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName());
688 (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7],
689 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
690 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
692 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
693 Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName());
694 (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7],
695 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
696 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
700 void ExternalCommandProcessor::ScheduleHostgroupHostDowntime(double, const vector<String>& arguments)
702 if (arguments.size() < 8)
703 throw_exception(invalid_argument("Expected 8 arguments."));
705 if (!HostGroup::Exists(arguments[0]))
706 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
708 HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
711 int triggeredByLegacy = Convert::ToLong(arguments[4]);
712 if (triggeredByLegacy != 0)
713 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
715 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
716 Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName());
717 (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7],
718 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
719 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
723 void ExternalCommandProcessor::ScheduleHostgroupSvcDowntime(double, const vector<String>& arguments)
725 if (arguments.size() < 8)
726 throw_exception(invalid_argument("Expected 8 arguments."));
728 if (!HostGroup::Exists(arguments[0]))
729 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
731 HostGroup::Ptr hg = HostGroup::GetByName(arguments[0]);
734 int triggeredByLegacy = Convert::ToLong(arguments[4]);
735 if (triggeredByLegacy != 0)
736 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
738 /* Note: we can't just directly create downtimes for all the services by iterating
739 * over all hosts in the host group - otherwise we might end up creating multiple
740 * downtimes for some services. */
742 set<Service::Ptr> services;
744 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
745 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
746 services.insert(service);
750 BOOST_FOREACH(const Service::Ptr& service, services) {
751 Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName());
752 (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7],
753 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
754 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
758 void ExternalCommandProcessor::ScheduleServicegroupHostDowntime(double, const vector<String>& arguments)
760 if (arguments.size() < 8)
761 throw_exception(invalid_argument("Expected 8 arguments."));
763 if (!ServiceGroup::Exists(arguments[0]))
764 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
766 ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
769 int triggeredByLegacy = Convert::ToLong(arguments[4]);
770 if (triggeredByLegacy != 0)
771 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
773 /* Note: we can't just directly create downtimes for all the hosts by iterating
774 * over all services in the service group - otherwise we might end up creating multiple
775 * downtimes for some hosts. */
777 set<Host::Ptr> hosts;
779 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
780 hosts.insert(service->GetHost());
783 BOOST_FOREACH(const Host::Ptr& host, hosts) {
784 Logger::Write(LogInformation, "icinga", "Creating downtime for host " + host->GetName());
785 (void) DowntimeProcessor::AddDowntime(host, arguments[6], arguments[7],
786 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
787 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
791 void ExternalCommandProcessor::ScheduleServicegroupSvcDowntime(double, const vector<String>& arguments)
793 if (arguments.size() < 8)
794 throw_exception(invalid_argument("Expected 8 arguments."));
796 if (!ServiceGroup::Exists(arguments[0]))
797 throw_exception(invalid_argument("The host group '" + arguments[0] + "' does not exist."));
799 ServiceGroup::Ptr sg = ServiceGroup::GetByName(arguments[0]);
802 int triggeredByLegacy = Convert::ToLong(arguments[4]);
803 if (triggeredByLegacy != 0)
804 triggeredBy = DowntimeProcessor::GetIDFromLegacyID(triggeredByLegacy);
806 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
807 Logger::Write(LogInformation, "icinga", "Creating downtime for service " + service->GetName());
808 (void) DowntimeProcessor::AddDowntime(service, arguments[6], arguments[7],
809 Convert::ToDouble(arguments[1]), Convert::ToDouble(arguments[2]),
810 Convert::ToBool(arguments[3]), triggeredBy, Convert::ToDouble(arguments[5]));
814 void ExternalCommandProcessor::AddHostComment(double, const vector<String>& arguments)
816 if (arguments.size() < 4)
817 throw_exception(invalid_argument("Expected 4 arguments."));
819 if (!Host::Exists(arguments[0]))
820 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
822 Host::Ptr host = Host::GetByName(arguments[0]);
824 Logger::Write(LogInformation, "icinga", "Creating comment for host " + host->GetName());
825 (void) CommentProcessor::AddComment(host, Comment_User, arguments[2], arguments[3], 0);
828 void ExternalCommandProcessor::DelHostComment(double, const vector<String>& arguments)
830 if (arguments.size() < 1)
831 throw_exception(invalid_argument("Expected 1 argument."));
833 int id = Convert::ToLong(arguments[0]);
834 Logger::Write(LogInformation, "icinga", "Removing comment ID " + arguments[0]);
835 String rid = CommentProcessor::GetIDFromLegacyID(id);
836 CommentProcessor::RemoveComment(rid);
839 void ExternalCommandProcessor::AddSvcComment(double, const vector<String>& arguments)
841 if (arguments.size() < 5)
842 throw_exception(invalid_argument("Expected 5 arguments."));
844 if (!Service::Exists(arguments[1]))
845 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
847 Service::Ptr service = Service::GetByName(arguments[1]);
849 Logger::Write(LogInformation, "icinga", "Creating comment for service " + service->GetName());
850 (void) CommentProcessor::AddComment(service, Comment_User, arguments[3], arguments[4], 0);
853 void ExternalCommandProcessor::DelSvcComment(double, const vector<String>& arguments)
855 if (arguments.size() < 1)
856 throw_exception(invalid_argument("Expected 1 argument."));
858 int id = Convert::ToLong(arguments[0]);
859 Logger::Write(LogInformation, "icinga", "Removing comment ID " + arguments[0]);
861 String rid = CommentProcessor::GetIDFromLegacyID(id);
862 CommentProcessor::RemoveComment(rid);
865 void ExternalCommandProcessor::DelAllHostComments(double, const vector<String>& arguments)
867 if (arguments.size() < 1)
868 throw_exception(invalid_argument("Expected 1 argument."));
870 if (!Host::Exists(arguments[0]))
871 throw_exception(invalid_argument("The host '" + arguments[0] + "' does not exist."));
873 Host::Ptr host = Host::GetByName(arguments[0]);
875 Logger::Write(LogInformation, "icinga", "Removing all comments for host " + host->GetName());
876 CommentProcessor::RemoveAllComments(host);
879 void ExternalCommandProcessor::DelAllSvcComments(double, const vector<String>& arguments)
881 if (arguments.size() < 2)
882 throw_exception(invalid_argument("Expected 2 arguments."));
884 if (!Service::Exists(arguments[1]))
885 throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist."));
887 Service::Ptr service = Service::GetByName(arguments[1]);
889 Logger::Write(LogInformation, "icinga", "Removing all comments for service " + service->GetName());
890 CommentProcessor::RemoveAllComments(service);