icinga2/lib/db_ido/servicedbobject.cpp

390 lines
16 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2018-01-02 12:06:00 +01:00
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
* *
* 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/servicegroupdbobject.hpp"
2014-05-25 16:23:35 +02:00
#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/pluginutility.hpp"
2014-05-25 16:23:35 +02:00
#include "icinga/icingaapplication.hpp"
#include "remote/endpoint.hpp"
#include "base/convert.hpp"
#include "base/objectlock.hpp"
#include "base/initialize.hpp"
#include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
#include "base/utility.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
#include "base/json.hpp"
#include <boost/algorithm/string/join.hpp>
using namespace icinga;
REGISTER_DBTYPE(Service, "service", DbObjectTypeService, "service_object_id", ServiceDbObject);
2013-08-01 13:20:30 +02:00
ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)
{ }
Dictionary::Ptr ServiceDbObject::GetConfigFields() const
{
Dictionary::Ptr fields = new Dictionary();
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);
fields->Set("display_name", service->GetDisplayName());
fields->Set("check_command_object_id", service->GetCheckCommand());
fields->Set("eventhandler_command_object_id", service->GetEventCommand());
fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
fields->Set("check_interval", service->GetCheckInterval() / 60.0);
fields->Set("retry_interval", service->GetRetryInterval() / 60.0);
fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
fields->Set("is_volatile", service->GetVolatile());
fields->Set("flap_detection_enabled", service->GetEnableFlapping());
fields->Set("low_flap_threshold", service->GetFlappingThresholdLow());
fields->Set("high_flap_threshold", service->GetFlappingThresholdLow());
fields->Set("process_performance_data", service->GetEnablePerfdata());
fields->Set("freshness_checks_enabled", 1);
fields->Set("freshness_threshold", Convert::ToLong(service->GetCheckInterval()));
fields->Set("event_handler_enabled", service->GetEnableEventHandler());
fields->Set("passive_checks_enabled", service->GetEnablePassiveChecks());
fields->Set("active_checks_enabled", service->GetEnableActiveChecks());
fields->Set("notifications_enabled", service->GetEnableNotifications());
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());
fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(service));
unsigned long notificationStateFilter = CompatUtility::GetCheckableNotificationTypeFilter(service);
unsigned long notificationTypeFilter = CompatUtility::GetCheckableNotificationTypeFilter(service);
fields->Set("notify_on_warning", notificationStateFilter & ServiceWarning);
fields->Set("notify_on_unknown", notificationStateFilter & ServiceUnknown);
fields->Set("notify_on_critical", notificationStateFilter & ServiceCritical);
fields->Set("notify_on_recovery", notificationTypeFilter & NotificationRecovery);
fields->Set("notify_on_flapping", (notificationTypeFilter & NotificationFlappingStart) ||
(notificationTypeFilter & NotificationFlappingEnd));
fields->Set("notify_on_downtime", (notificationTypeFilter & NotificationDowntimeStart) ||
(notificationTypeFilter & NotificationDowntimeEnd) || (notificationTypeFilter & NotificationDowntimeRemoved));
return fields;
}
Dictionary::Ptr ServiceDbObject::GetStatusFields() const
{
Dictionary::Ptr fields = new Dictionary();
Service::Ptr service = static_pointer_cast<Service>(GetObject());
CheckResult::Ptr cr = service->GetLastCheckResult();
if (cr) {
fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
fields->Set("perfdata", PluginUtility::FormatPerfdata(cr->GetPerformanceData()));
fields->Set("check_source", cr->GetCheckSource());
fields->Set("latency", cr->CalculateLatency());
fields->Set("execution_time", cr->CalculateExecutionTime());
}
2014-04-03 15:36:13 +02:00
fields->Set("current_state", service->GetState());
fields->Set("has_been_checked", service->HasBeenChecked());
fields->Set("should_be_scheduled", service->GetEnableActiveChecks());
fields->Set("current_check_attempt", service->GetCheckAttempt());
fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
fields->Set("last_check", DbValue::FromTimestamp(service->GetLastCheck()));
fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
fields->Set("check_type", !service->GetEnableActiveChecks()); /* 0 .. active, 1 .. passive */
fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
fields->Set("last_hard_state", service->GetLastHardState());
fields->Set("last_time_ok", DbValue::FromTimestamp(service->GetLastStateOK()));
fields->Set("last_time_warning", DbValue::FromTimestamp(service->GetLastStateWarning()));
fields->Set("last_time_critical", DbValue::FromTimestamp(service->GetLastStateCritical()));
fields->Set("last_time_unknown", DbValue::FromTimestamp(service->GetLastStateUnknown()));
fields->Set("state_type", service->GetStateType());
fields->Set("notifications_enabled", service->GetEnableNotifications());
fields->Set("problem_has_been_acknowledged", service->GetAcknowledgement() != AcknowledgementNone);
fields->Set("acknowledgement_type", service->GetAcknowledgement());
fields->Set("passive_checks_enabled", service->GetEnablePassiveChecks());
fields->Set("active_checks_enabled", service->GetEnableActiveChecks());
fields->Set("event_handler_enabled", service->GetEnableEventHandler());
fields->Set("flap_detection_enabled", service->GetEnableFlapping());
2017-12-05 18:53:22 +01:00
fields->Set("is_flapping", service->IsFlapping());
fields->Set("percent_state_change", service->GetFlappingCurrent());
fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
fields->Set("process_performance_data", service->GetEnablePerfdata());
fields->Set("normal_check_interval", service->GetCheckInterval() / 60.0);
fields->Set("retry_check_interval", service->GetRetryInterval() / 60.0);
fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
2017-12-05 18:53:22 +01:00
fields->Set("is_reachable", service->IsReachable());
fields->Set("original_attributes", JsonEncode(service->GetOriginalAttributes()));
fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(service));
fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(service)));
fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(service)));
2017-12-05 19:30:45 +01:00
EventCommand::Ptr eventCommand = service->GetEventCommand();
if (eventCommand)
fields->Set("event_handler", eventCommand->GetName());
CheckCommand::Ptr checkCommand = service->GetCheckCommand();
if (checkCommand)
fields->Set("check_command", checkCommand->GetName());
return fields;
2013-07-24 10:55:04 +02:00
}
void ServiceDbObject::OnConfigUpdateHeavy()
{
Service::Ptr service = static_pointer_cast<Service>(GetObject());
/* groups */
Array::Ptr groups = service->GetGroups();
std::vector<DbQuery> queries;
DbQuery query1;
query1.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members";
query1.Type = DbQueryDelete;
query1.Category = DbCatConfig;
query1.WhereCriteria = new Dictionary();
query1.WhereCriteria->Set("service_object_id", service);
queries.emplace_back(std::move(query1));
if (groups) {
ObjectLock olock(groups);
for (const String& groupName : groups) {
ServiceGroup::Ptr group = ServiceGroup::GetByName(groupName);
DbQuery query2;
query2.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members";
query2.Type = DbQueryInsert;
query2.Category = DbCatConfig;
query2.Fields = new Dictionary();
query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query2.Fields->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
query2.Fields->Set("service_object_id", service);
query2.WhereCriteria = new Dictionary();
query2.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
query2.WhereCriteria->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
query2.WhereCriteria->Set("service_object_id", service);
queries.emplace_back(std::move(query2));
}
}
DbObject::OnMultipleQueries(queries);
/* service dependencies */
2014-10-19 17:52:17 +02:00
Log(LogDebug, "ServiceDbObject")
<< "service dependencies for '" << service->GetName() << "'";
queries.clear();
DbQuery query2;
query2.Table = GetType()->GetTable() + "dependencies";
query2.Type = DbQueryDelete;
query2.Category = DbCatConfig;
query2.WhereCriteria = new Dictionary();
query2.WhereCriteria->Set("dependent_service_object_id", service);
queries.emplace_back(std::move(query2));
for (const Dependency::Ptr& dep : service->GetDependencies()) {
Checkable::Ptr parent = dep->GetParent();
2014-04-03 15:36:13 +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-04-03 15:36:13 +02:00
2014-10-19 17:52:17 +02:00
Log(LogDebug, "ServiceDbObject")
<< "service parents: " << parent->GetName();
int stateFilter = dep->GetStateFilter();
/* service dependencies */
Dictionary::Ptr fields1 = new Dictionary();
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", stateFilter & StateFilterOK);
fields1->Set("fail_on_warning", stateFilter & StateFilterWarning);
fields1->Set("fail_on_critical", stateFilter & StateFilterCritical);
fields1->Set("fail_on_unknown", stateFilter & StateFilterUnknown);
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
DbQuery query1;
query1.Table = GetType()->GetTable() + "dependencies";
query1.Type = DbQueryInsert;
query1.Category = DbCatConfig;
query1.Fields = fields1;
queries.emplace_back(std::move(query1));
}
DbObject::OnMultipleQueries(queries);
/* service contacts, contactgroups */
2014-10-19 17:52:17 +02:00
Log(LogDebug, "ServiceDbObject")
<< "service contacts: " << service->GetName();
queries.clear();
DbQuery query3;
query3.Table = GetType()->GetTable() + "_contacts";
query3.Type = DbQueryDelete;
query3.Category = DbCatConfig;
query3.WhereCriteria = new Dictionary();
query3.WhereCriteria->Set("service_id", DbValue::FromObjectInsertID(service));
queries.emplace_back(std::move(query3));
for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
2014-10-19 17:52:17 +02:00
Log(LogDebug, "ServiceDbObject")
<< "service contacts: " << user->GetName();
Dictionary::Ptr fields_contact = new Dictionary();
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;
query_contact.Category = DbCatConfig;
query_contact.Fields = fields_contact;
queries.emplace_back(std::move(query_contact));
}
DbObject::OnMultipleQueries(queries);
2014-10-20 10:09:57 +02:00
Log(LogDebug, "ServiceDbObject")
<< "service contactgroups: " << service->GetName();
queries.clear();
DbQuery query4;
query4.Table = GetType()->GetTable() + "_contactgroups";
query4.Type = DbQueryDelete;
query4.Category = DbCatConfig;
query4.WhereCriteria = new Dictionary();
query4.WhereCriteria->Set("service_id", DbValue::FromObjectInsertID(service));
queries.emplace_back(std::move(query4));
for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
2014-10-19 17:52:17 +02:00
Log(LogDebug, "ServiceDbObject")
<< "service contactgroups: " << usergroup->GetName();
Dictionary::Ptr fields_contact = new Dictionary();
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;
query_contact.Category = DbCatConfig;
query_contact.Fields = fields_contact;
queries.emplace_back(std::move(query_contact));
}
DbObject::OnMultipleQueries(queries);
DoCommonConfigUpdate();
}
void ServiceDbObject::OnConfigUpdateLight()
{
DoCommonConfigUpdate();
}
void ServiceDbObject::DoCommonConfigUpdate()
{
Service::Ptr service = static_pointer_cast<Service>(GetObject());
/* update comments and downtimes on config change */
2014-04-03 15:36:13 +02:00
DbEvents::AddComments(service);
DbEvents::AddDowntimes(service);
}
String ServiceDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
{
String hashData = DbObject::CalculateConfigHash(configFields);
Service::Ptr service = static_pointer_cast<Service>(GetObject());
Array::Ptr groups = service->GetGroups();
if (groups)
hashData += DbObject::HashValue(groups);
Array::Ptr dependencies = new Array();
/* dependencies */
for (const Dependency::Ptr& dep : service->GetDependencies()) {
Checkable::Ptr parent = dep->GetParent();
if (!parent)
continue;
Array::Ptr depInfo = new Array();
depInfo->Add(parent->GetName());
depInfo->Add(dep->GetStateFilter());
depInfo->Add(dep->GetPeriodRaw());
dependencies->Add(depInfo);
}
dependencies->Sort();
hashData += DbObject::HashValue(dependencies);
Array::Ptr users = new Array();
for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
users->Add(user->GetName());
}
users->Sort();
hashData += DbObject::HashValue(users);
Array::Ptr userGroups = new Array();
for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
userGroups->Add(usergroup->GetName());
}
userGroups->Sort();
hashData += DbObject::HashValue(userGroups);
return SHA256(hashData);
}