2013-07-17 14:10:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-07-17 14:10:28 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/servicedbobject.hpp"
|
|
|
|
#include "db_ido/dbtype.hpp"
|
|
|
|
#include "db_ido/dbvalue.hpp"
|
|
|
|
#include "db_ido/dbevents.hpp"
|
|
|
|
#include "icinga/notification.hpp"
|
|
|
|
#include "icinga/dependency.hpp"
|
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "icinga/eventcommand.hpp"
|
|
|
|
#include "icinga/externalcommandprocessor.hpp"
|
|
|
|
#include "icinga/compatutility.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2013-08-05 17:25:40 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2013-09-30 19:32:32 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2013-07-17 14:10:28 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-02 17:12:07 +02:00
|
|
|
REGISTER_DBTYPE(Service, "service", DbObjectTypeService, "service_object_id", ServiceDbObject);
|
2013-07-17 14:10:28 +02:00
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
|
|
|
|
: DbObject(type, name1, name2)
|
2013-07-17 14:10:28 +02:00
|
|
|
{ }
|
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
Dictionary::Ptr ServiceDbObject::GetConfigFields(void) const
|
2013-07-17 14:10:28 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2013-07-17 14:10:28 +02:00
|
|
|
Service::Ptr service = static_pointer_cast<Service>(GetObject());
|
2013-07-23 11:02:47 +02:00
|
|
|
Host::Ptr host = service->GetHost();
|
|
|
|
|
|
|
|
fields->Set("host_object_id", host);
|
2013-07-17 14:10:28 +02:00
|
|
|
fields->Set("display_name", service->GetDisplayName());
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("check_command_object_id", service->GetCheckCommand());
|
2014-05-03 03:18:37 +02:00
|
|
|
fields->Set("check_command_args", CompatUtility::GetCheckableCommandArgs(service));
|
2013-07-31 18:06:54 +02:00
|
|
|
fields->Set("eventhandler_command_object_id", service->GetEventCommand());
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("eventhandler_command_args", Empty);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(service)));
|
2013-07-31 18:06:54 +02:00
|
|
|
fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("failure_prediction_options", Empty);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(service));
|
|
|
|
fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(service));
|
2013-12-05 14:54:16 +01:00
|
|
|
fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("first_notification_delay", Empty);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(service));
|
|
|
|
fields->Set("notify_on_warning", CompatUtility::GetCheckableNotifyOnWarning(service));
|
|
|
|
fields->Set("notify_on_unknown", CompatUtility::GetCheckableNotifyOnUnknown(service));
|
|
|
|
fields->Set("notify_on_critical", CompatUtility::GetCheckableNotifyOnCritical(service));
|
|
|
|
fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(service));
|
|
|
|
fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(service));
|
|
|
|
fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(service));
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("stalk_on_ok", 0);
|
|
|
|
fields->Set("stalk_on_warning", 0);
|
|
|
|
fields->Set("stalk_on_unknown", 0);
|
|
|
|
fields->Set("stalk_on_critical", 0);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("is_volatile", CompatUtility::GetCheckableIsVolatile(service));
|
|
|
|
fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service));
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("flap_detection_on_ok", Empty);
|
|
|
|
fields->Set("flap_detection_on_warning", Empty);
|
|
|
|
fields->Set("flap_detection_on_unknown", Empty);
|
|
|
|
fields->Set("flap_detection_on_critical", Empty);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(service));
|
|
|
|
fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(service));
|
|
|
|
fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service));
|
|
|
|
fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(service));
|
|
|
|
fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(service));
|
|
|
|
fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service));
|
|
|
|
fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service));
|
|
|
|
fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service));
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("retain_status_information", Empty);
|
|
|
|
fields->Set("retain_nonstatus_information", Empty);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service));
|
2013-07-22 12:08:49 +02:00
|
|
|
fields->Set("obsess_over_service", Empty);
|
|
|
|
fields->Set("failure_prediction_enabled", Empty);
|
2014-04-17 13:49:45 +02:00
|
|
|
fields->Set("notes", service->GetNotes());
|
|
|
|
fields->Set("notes_url", service->GetNotesUrl());
|
|
|
|
fields->Set("action_url", service->GetActionUrl());
|
|
|
|
fields->Set("icon_image", service->GetIconImage());
|
|
|
|
fields->Set("icon_image_alt", service->GetIconImageAlt());
|
2013-07-17 14:10:28 +02:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
2013-07-22 12:08:49 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr ServiceDbObject::GetStatusFields(void) const
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2013-07-22 12:08:49 +02:00
|
|
|
Service::Ptr service = static_pointer_cast<Service>(GetObject());
|
2013-12-05 14:54:16 +01:00
|
|
|
CheckResult::Ptr cr = service->GetLastCheckResult();
|
2013-07-23 09:50:42 +02:00
|
|
|
|
2013-12-05 14:54:16 +01:00
|
|
|
if (cr) {
|
|
|
|
fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
|
|
|
|
fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
|
|
|
|
fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
|
2014-03-21 10:09:34 +01:00
|
|
|
fields->Set("check_source", cr->GetCheckSource());
|
2013-07-23 09:50:42 +02:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("current_state", service->GetState());
|
|
|
|
fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(service));
|
2014-04-08 13:34:48 +02:00
|
|
|
fields->Set("should_be_scheduled", service->GetEnableActiveChecks());
|
2013-12-05 14:54:16 +01:00
|
|
|
fields->Set("current_check_attempt", service->GetCheckAttempt());
|
|
|
|
fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
|
|
|
|
|
|
|
|
if (cr)
|
|
|
|
fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
|
|
|
|
|
|
|
|
fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("check_type", CompatUtility::GetCheckableCheckType(service));
|
2013-12-05 14:54:16 +01:00
|
|
|
fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
|
|
|
|
fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
|
2014-12-12 13:25:10 +01:00
|
|
|
fields->Set("last_hard_state", service->GetLastHardState());
|
2013-12-05 14:54:16 +01:00
|
|
|
fields->Set("last_time_ok", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateOK())));
|
|
|
|
fields->Set("last_time_warning", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateWarning())));
|
|
|
|
fields->Set("last_time_critical", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateCritical())));
|
|
|
|
fields->Set("last_time_unknown", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateUnknown())));
|
|
|
|
fields->Set("state_type", service->GetStateType());
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(service)));
|
|
|
|
fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(service)));
|
2013-07-31 18:06:54 +02:00
|
|
|
fields->Set("no_more_notifications", Empty);
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service));
|
2014-11-11 16:35:58 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(service);
|
|
|
|
fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(service));
|
|
|
|
fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(service));
|
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(service));
|
|
|
|
fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service));
|
|
|
|
fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service));
|
|
|
|
fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service));
|
|
|
|
fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service));
|
|
|
|
fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(service));
|
|
|
|
fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(service));
|
2013-12-05 14:54:16 +01:00
|
|
|
|
|
|
|
if (cr) {
|
2014-01-10 00:38:16 +01:00
|
|
|
fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
|
|
|
|
fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
|
2013-12-05 14:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
|
2014-04-03 15:36:13 +02:00
|
|
|
fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service));
|
|
|
|
fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(service));
|
|
|
|
fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(service));
|
|
|
|
fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(service));
|
|
|
|
fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(service));
|
2013-07-31 18:06:54 +02:00
|
|
|
fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
|
2013-12-05 14:54:16 +01:00
|
|
|
fields->Set("modified_service_attributes", service->GetModifiedAttributes());
|
2014-05-10 21:03:47 +02:00
|
|
|
fields->Set("is_reachable", CompatUtility::GetCheckableIsReachable(service));
|
2013-07-22 12:08:49 +02:00
|
|
|
|
|
|
|
return fields;
|
2013-07-24 10:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ServiceDbObject::IsStatusAttribute(const String& attribute) const
|
|
|
|
{
|
|
|
|
return (attribute == "last_result");
|
2013-07-25 08:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceDbObject::OnConfigUpdate(void)
|
|
|
|
{
|
|
|
|
Service::Ptr service = static_pointer_cast<Service>(GetObject());
|
2013-08-05 17:25:40 +02:00
|
|
|
|
|
|
|
/* service dependencies */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "service dependencies for '" << service->GetName() << "'";
|
2013-08-05 17:25:40 +02:00
|
|
|
|
2014-04-23 10:33:30 +02:00
|
|
|
BOOST_FOREACH(const Dependency::Ptr& dep, service->GetDependencies()) {
|
|
|
|
Checkable::Ptr parent = dep->GetParent();
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2014-05-03 21:02:19 +02:00
|
|
|
if (!parent) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "Missing parent for dependency '" << dep->GetName() << "'.";
|
2014-04-03 15:36:13 +02:00
|
|
|
continue;
|
2014-05-03 21:02:19 +02:00
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "service parents: " << parent->GetName();
|
2013-08-05 17:25:40 +02:00
|
|
|
|
2014-04-23 10:33:30 +02:00
|
|
|
int state_filter = dep->GetStateFilter();
|
|
|
|
|
|
|
|
/* service dependencies */
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields1 = new Dictionary();
|
2014-04-23 10:33:30 +02:00
|
|
|
fields1->Set("service_object_id", parent);
|
|
|
|
fields1->Set("dependent_service_object_id", service);
|
|
|
|
fields1->Set("inherits_parent", 1);
|
|
|
|
fields1->Set("timeperiod_object_id", dep->GetPeriod());
|
|
|
|
fields1->Set("fail_on_ok", (state_filter & StateFilterOK) ? 1 : 0);
|
|
|
|
fields1->Set("fail_on_warning", (state_filter & StateFilterWarning) ? 1 : 0);
|
|
|
|
fields1->Set("fail_on_critical", (state_filter & StateFilterCritical) ? 1 : 0);
|
|
|
|
fields1->Set("fail_on_unknown", (state_filter & StateFilterUnknown) ? 1 : 0);
|
|
|
|
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
DbQuery query1;
|
|
|
|
query1.Table = GetType()->GetTable() + "dependencies";
|
|
|
|
query1.Type = DbQueryInsert;
|
2013-10-29 15:54:43 +01:00
|
|
|
query1.Category = DbCatConfig;
|
2014-04-23 10:33:30 +02:00
|
|
|
query1.Fields = fields1;
|
|
|
|
OnQuery(query1);
|
2013-08-05 17:25:40 +02:00
|
|
|
}
|
|
|
|
|
2013-10-01 16:43:25 +02:00
|
|
|
/* service contacts, contactgroups */
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "service contacts: " << service->GetName();
|
2013-10-01 16:43:25 +02:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(service)) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "service contacts: " << user->GetName();
|
2013-10-02 21:09:13 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields_contact = new Dictionary();
|
2013-10-02 21:09:13 +02:00
|
|
|
fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
|
|
|
|
fields_contact->Set("contact_object_id", user);
|
|
|
|
fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
DbQuery query_contact;
|
|
|
|
query_contact.Table = GetType()->GetTable() + "_contacts";
|
|
|
|
query_contact.Type = DbQueryInsert;
|
2013-10-29 15:54:43 +01:00
|
|
|
query_contact.Category = DbCatConfig;
|
2013-10-02 21:09:13 +02:00
|
|
|
query_contact.Fields = fields_contact;
|
|
|
|
OnQuery(query_contact);
|
2013-10-01 16:43:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "service contactgroups: " << service->GetName();
|
2013-10-02 21:09:13 +02:00
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(service)) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ServiceDbObject")
|
|
|
|
<< "service contactgroups: " << usergroup->GetName();
|
2013-10-02 21:09:13 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields_contact = new Dictionary();
|
2013-10-02 21:09:13 +02:00
|
|
|
fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
|
|
|
|
fields_contact->Set("contactgroup_object_id", usergroup);
|
|
|
|
fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
DbQuery query_contact;
|
|
|
|
query_contact.Table = GetType()->GetTable() + "_contactgroups";
|
|
|
|
query_contact.Type = DbQueryInsert;
|
2013-10-29 15:54:43 +01:00
|
|
|
query_contact.Category = DbCatConfig;
|
2013-10-02 21:09:13 +02:00
|
|
|
query_contact.Fields = fields_contact;
|
|
|
|
OnQuery(query_contact);
|
2013-10-01 16:43:25 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 11:08:29 +02:00
|
|
|
/* update comments and downtimes on config change */
|
2014-04-03 15:36:13 +02:00
|
|
|
DbEvents::AddComments(service);
|
|
|
|
DbEvents::AddDowntimes(service);
|
2013-07-25 08:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceDbObject::OnStatusUpdate(void)
|
|
|
|
{
|
2013-10-03 03:41:47 +02:00
|
|
|
}
|