2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/dbconnection.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "db_ido/dbconnection-ti.cpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/dbvalue.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
#include "icinga/host.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-12-18 15:43:01 +01:00
|
|
|
#include "base/exception.hpp"
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(DbConnection);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2013-07-25 09:00:23 +02:00
|
|
|
Timer::Ptr DbConnection::m_ProgramStatusTimer;
|
2015-01-27 09:53:07 +01:00
|
|
|
boost::once_flag DbConnection::m_OnceFlag = BOOST_ONCE_INIT;
|
2013-08-01 11:07:26 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::OnConfigLoaded()
|
2014-08-13 20:30:28 +02:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::OnConfigLoaded();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
2016-06-22 16:11:15 +02:00
|
|
|
Value categories = GetCategories();
|
|
|
|
|
2017-05-10 14:31:34 +02:00
|
|
|
SetCategoryFilter(FilterArrayToInt(categories, DbQuery::GetCategoryFilterMap(), DbCatEverything));
|
2016-06-22 16:11:15 +02:00
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
if (!GetEnableHa()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "DbConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "HA functionality disabled. Won't pause IDO connection: " << GetName();
|
2014-10-19 17:52:17 +02:00
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
SetHAMode(HARunEverywhere);
|
|
|
|
}
|
2015-01-27 09:53:07 +01:00
|
|
|
|
|
|
|
boost::call_once(m_OnceFlag, InitializeDbTimer);
|
2014-08-13 20:30:28 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void DbConnection::Start(bool runtimeCreated)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<DbConnection>::Start(runtimeCreated);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "DbConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' started.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
DbObject::OnQuery.connect(std::bind(&DbConnection::ExecuteQuery, this, _1));
|
|
|
|
DbObject::OnMultipleQueries.connect(std::bind(&DbConnection::ExecuteMultipleQueries, this, _1));
|
2016-08-13 22:54:22 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
void DbConnection::Stop(bool runtimeRemoved)
|
|
|
|
{
|
|
|
|
Log(LogInformation, "DbConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' stopped.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
|
|
|
ObjectImpl<DbConnection>::Stop(runtimeRemoved);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::EnableActiveChangedHandler()
|
2016-08-13 22:54:22 +02:00
|
|
|
{
|
|
|
|
if (!m_ActiveChangedHandler) {
|
2017-11-21 11:52:55 +01:00
|
|
|
ConfigObject::OnActiveChanged.connect(std::bind(&DbConnection::UpdateObject, this, _1));
|
2016-08-13 22:54:22 +02:00
|
|
|
m_ActiveChangedHandler = true;
|
|
|
|
}
|
2014-05-09 13:02:30 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::Resume()
|
2014-05-09 13:02:30 +02:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Resume();
|
2014-05-09 13:02:30 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "DbConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Resuming IDO connection: " << GetName();
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_CleanUpTimer = new Timer();
|
2013-09-26 15:22:21 +02:00
|
|
|
m_CleanUpTimer->SetInterval(60);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_CleanUpTimer->OnTimerExpired.connect(std::bind(&DbConnection::CleanUpHandler, this));
|
2013-09-26 15:22:21 +02:00
|
|
|
m_CleanUpTimer->Start();
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::Pause()
|
2014-05-09 13:02:30 +02:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Pause();
|
2014-05-09 13:02:30 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "DbConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Pausing IDO connection: " << GetName();
|
2014-05-09 13:02:30 +02:00
|
|
|
|
|
|
|
m_CleanUpTimer.reset();
|
2015-02-04 15:25:10 +01:00
|
|
|
|
|
|
|
DbQuery query1;
|
|
|
|
query1.Table = "programstatus";
|
|
|
|
query1.IdColumn = "programstatus_id";
|
|
|
|
query1.Type = DbQueryUpdate;
|
|
|
|
query1.Category = DbCatProgramStatus;
|
2018-01-11 11:17:38 +01:00
|
|
|
query1.WhereCriteria = new Dictionary({
|
|
|
|
{ "instance_id", 0 } /* DbConnection class fills in real ID */
|
|
|
|
});
|
2015-02-04 15:25:10 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
query1.Fields = new Dictionary({
|
|
|
|
{ "instance_id", 0 }, /* DbConnection class fills in real ID */
|
|
|
|
{ "program_end_time", DbValue::FromTimestamp(Utility::GetTime()) }
|
|
|
|
});
|
2015-12-10 16:54:43 +01:00
|
|
|
|
|
|
|
query1.Priority = PriorityHigh;
|
|
|
|
|
2015-02-04 15:25:10 +01:00
|
|
|
ExecuteQuery(query1);
|
|
|
|
|
|
|
|
NewTransaction();
|
2014-05-09 13:02:30 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::InitializeDbTimer()
|
2013-07-25 09:00:23 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
m_ProgramStatusTimer = new Timer();
|
2013-07-25 09:00:23 +02:00
|
|
|
m_ProgramStatusTimer->SetInterval(10);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_ProgramStatusTimer->OnTimerExpired.connect(std::bind(&DbConnection::UpdateProgramStatus));
|
2013-07-25 09:00:23 +02:00
|
|
|
m_ProgramStatusTimer->Start();
|
|
|
|
}
|
|
|
|
|
2013-08-08 08:47:29 +02:00
|
|
|
void DbConnection::InsertRuntimeVariable(const String& key, const Value& value)
|
|
|
|
{
|
|
|
|
DbQuery query;
|
2013-08-08 08:52:20 +02:00
|
|
|
query.Table = "runtimevariables";
|
|
|
|
query.Type = DbQueryInsert;
|
2013-10-29 15:54:43 +01:00
|
|
|
query.Category = DbCatProgramStatus;
|
2018-01-11 11:17:38 +01:00
|
|
|
query.Fields = new Dictionary({
|
|
|
|
{ "instance_id", 0 }, /* DbConnection class fills in real ID */
|
|
|
|
{ "varname", key },
|
|
|
|
{ "varvalue", value }
|
|
|
|
});
|
2013-08-08 08:47:29 +02:00
|
|
|
DbObject::OnQuery(query);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::UpdateProgramStatus()
|
2013-07-25 09:00:23 +02:00
|
|
|
{
|
2019-07-11 15:50:52 +02:00
|
|
|
IcingaApplication::Ptr icingaApplication = IcingaApplication::GetInstance();
|
|
|
|
|
|
|
|
if (!icingaApplication)
|
|
|
|
return;
|
|
|
|
|
2015-10-15 18:43:30 +02:00
|
|
|
Log(LogNotice, "DbConnection")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Updating programstatus table.";
|
2015-10-15 18:43:30 +02:00
|
|
|
|
2015-12-14 11:36:03 +01:00
|
|
|
std::vector<DbQuery> queries;
|
|
|
|
|
2013-07-25 09:00:23 +02:00
|
|
|
DbQuery query1;
|
2013-08-01 09:46:48 +02:00
|
|
|
query1.Table = "programstatus";
|
2016-07-21 17:37:41 +02:00
|
|
|
query1.IdColumn = "programstatus_id";
|
|
|
|
query1.Type = DbQueryInsert | DbQueryUpdate;
|
2013-10-29 16:08:52 +01:00
|
|
|
query1.Category = DbCatProgramStatus;
|
2016-07-21 17:37:41 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
query1.Fields = new Dictionary({
|
|
|
|
{ "instance_id", 0 }, /* DbConnection class fills in real ID */
|
|
|
|
{ "program_version", Application::GetAppVersion() },
|
|
|
|
{ "status_update_time", DbValue::FromTimestamp(Utility::GetTime()) },
|
|
|
|
{ "program_start_time", DbValue::FromTimestamp(Application::GetStartTime()) },
|
|
|
|
{ "is_currently_running", 1 },
|
2019-07-11 15:50:52 +02:00
|
|
|
{ "endpoint_name", icingaApplication->GetNodeName() },
|
2018-01-11 11:17:38 +01:00
|
|
|
{ "process_id", Utility::GetPid() },
|
|
|
|
{ "daemon_mode", 1 },
|
|
|
|
{ "last_command_check", DbValue::FromTimestamp(Utility::GetTime()) },
|
2019-07-11 15:50:52 +02:00
|
|
|
{ "notifications_enabled", (icingaApplication->GetEnableNotifications() ? 1 : 0) },
|
|
|
|
{ "active_host_checks_enabled", (icingaApplication->GetEnableHostChecks() ? 1 : 0) },
|
2018-01-11 11:17:38 +01:00
|
|
|
{ "passive_host_checks_enabled", 1 },
|
2019-07-11 15:50:52 +02:00
|
|
|
{ "active_service_checks_enabled", (icingaApplication->GetEnableServiceChecks() ? 1 : 0) },
|
2018-01-11 11:17:38 +01:00
|
|
|
{ "passive_service_checks_enabled", 1 },
|
2019-07-11 15:50:52 +02:00
|
|
|
{ "event_handlers_enabled", (icingaApplication->GetEnableEventHandlers() ? 1 : 0) },
|
|
|
|
{ "flap_detection_enabled", (icingaApplication->GetEnableFlapping() ? 1 : 0) },
|
|
|
|
{ "process_performance_data", (icingaApplication->GetEnablePerfdata() ? 1 : 0) }
|
2018-01-11 11:17:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
query1.WhereCriteria = new Dictionary({
|
|
|
|
{ "instance_id", 0 } /* DbConnection class fills in real ID */
|
|
|
|
});
|
|
|
|
|
2015-12-10 16:54:43 +01:00
|
|
|
query1.Priority = PriorityHigh;
|
2017-11-30 08:19:58 +01:00
|
|
|
queries.emplace_back(std::move(query1));
|
2013-07-25 09:00:23 +02:00
|
|
|
|
|
|
|
DbQuery query2;
|
2016-07-21 17:37:41 +02:00
|
|
|
query2.Type = DbQueryNewTransaction;
|
2017-11-30 08:19:58 +01:00
|
|
|
queries.emplace_back(std::move(query2));
|
2015-12-14 11:36:03 +01:00
|
|
|
|
|
|
|
DbObject::OnMultipleQueries(queries);
|
2013-08-08 08:47:29 +02:00
|
|
|
|
2016-07-21 17:37:41 +02:00
|
|
|
DbQuery query3;
|
|
|
|
query3.Table = "runtimevariables";
|
|
|
|
query3.Type = DbQueryDelete;
|
|
|
|
query3.Category = DbCatProgramStatus;
|
2018-01-11 11:17:38 +01:00
|
|
|
query3.WhereCriteria = new Dictionary({
|
|
|
|
{ "instance_id", 0 } /* DbConnection class fills in real ID */
|
|
|
|
});
|
2016-07-21 17:37:41 +02:00
|
|
|
DbObject::OnQuery(query3);
|
2013-08-08 08:47:29 +02:00
|
|
|
|
2016-08-16 13:25:36 +02:00
|
|
|
InsertRuntimeVariable("total_services", ConfigType::Get<Service>()->GetObjectCount());
|
|
|
|
InsertRuntimeVariable("total_scheduled_services", ConfigType::Get<Service>()->GetObjectCount());
|
|
|
|
InsertRuntimeVariable("total_hosts", ConfigType::Get<Host>()->GetObjectCount());
|
|
|
|
InsertRuntimeVariable("total_scheduled_hosts", ConfigType::Get<Host>()->GetObjectCount());
|
2013-07-25 09:00:23 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::CleanUpHandler()
|
2013-09-26 15:22:21 +02:00
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto now = static_cast<long>(Utility::GetTime());
|
2013-09-26 15:22:21 +02:00
|
|
|
|
2013-10-30 14:08:09 +01:00
|
|
|
struct {
|
|
|
|
String name;
|
|
|
|
String time_column;
|
|
|
|
} tables[] = {
|
|
|
|
{ "acknowledgements", "entry_time" },
|
|
|
|
{ "commenthistory", "entry_time" },
|
|
|
|
{ "contactnotifications", "start_time" },
|
|
|
|
{ "contactnotificationmethods", "start_time" },
|
|
|
|
{ "downtimehistory", "entry_time" },
|
|
|
|
{ "eventhandlers", "start_time" },
|
|
|
|
{ "externalcommands", "entry_time" },
|
2016-08-24 19:59:13 +02:00
|
|
|
{ "flappinghistory", "event_time" },
|
2013-10-30 14:08:09 +01:00
|
|
|
{ "hostchecks", "start_time" },
|
|
|
|
{ "logentries", "logentry_time" },
|
|
|
|
{ "notifications", "start_time" },
|
|
|
|
{ "processevents", "event_time" },
|
|
|
|
{ "statehistory", "state_time" },
|
|
|
|
{ "servicechecks", "start_time" },
|
|
|
|
{ "systemcommands", "start_time" }
|
|
|
|
};
|
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& table : tables) {
|
|
|
|
double max_age = GetCleanup()->Get(table.name + "_age");
|
2013-10-30 14:08:09 +01:00
|
|
|
|
|
|
|
if (max_age == 0)
|
|
|
|
continue;
|
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
CleanUpExecuteQuery(table.name, table.time_column, now - max_age);
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "DbConnection")
|
2018-01-04 08:15:20 +01:00
|
|
|
<< "Cleanup (" << table.name << "): " << max_age
|
2017-12-19 15:50:05 +01:00
|
|
|
<< " now: " << now
|
|
|
|
<< " old: " << now - max_age;
|
2013-09-26 15:22:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-22 09:02:44 +02:00
|
|
|
void DbConnection::CleanUpExecuteQuery(const String&, const String&, double)
|
2013-09-26 15:22:21 +02:00
|
|
|
{
|
2013-10-30 14:08:09 +01:00
|
|
|
/* Default handler does nothing. */
|
2013-09-26 15:22:21 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
void DbConnection::SetConfigHash(const DbObject::Ptr& dbobj, const String& hash)
|
|
|
|
{
|
|
|
|
SetConfigHash(dbobj->GetType(), GetObjectID(dbobj), hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbConnection::SetConfigHash(const DbType::Ptr& type, const DbReference& objid, const String& hash)
|
|
|
|
{
|
|
|
|
if (!objid.IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!hash.IsEmpty())
|
|
|
|
m_ConfigHashes[std::make_pair(type, objid)] = hash;
|
|
|
|
else
|
|
|
|
m_ConfigHashes.erase(std::make_pair(type, objid));
|
|
|
|
}
|
|
|
|
|
|
|
|
String DbConnection::GetConfigHash(const DbObject::Ptr& dbobj) const
|
|
|
|
{
|
|
|
|
return GetConfigHash(dbobj->GetType(), GetObjectID(dbobj));
|
|
|
|
}
|
|
|
|
|
|
|
|
String DbConnection::GetConfigHash(const DbType::Ptr& type, const DbReference& objid) const
|
|
|
|
{
|
|
|
|
if (!objid.IsValid())
|
|
|
|
return String();
|
|
|
|
|
2016-08-27 08:33:15 +02:00
|
|
|
auto it = m_ConfigHashes.find(std::make_pair(type, objid));
|
2016-08-14 22:24:51 +02:00
|
|
|
|
|
|
|
if (it == m_ConfigHashes.end())
|
|
|
|
return String();
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
void DbConnection::SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
|
|
|
if (dbref.IsValid())
|
2013-08-02 08:56:36 +02:00
|
|
|
m_ObjectIDs[dbobj] = dbref;
|
2013-07-05 09:37:04 +02:00
|
|
|
else
|
2013-08-02 08:56:36 +02:00
|
|
|
m_ObjectIDs.erase(dbobj);
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
DbReference DbConnection::GetObjectID(const DbObject::Ptr& dbobj) const
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2016-08-27 08:33:15 +02:00
|
|
|
auto it = m_ObjectIDs.find(dbobj);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
if (it == m_ObjectIDs.end())
|
2018-01-04 09:28:24 +01:00
|
|
|
return {};
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
void DbConnection::SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref)
|
|
|
|
{
|
2014-01-31 08:28:00 +01:00
|
|
|
SetInsertID(dbobj->GetType(), GetObjectID(dbobj), dbref);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbConnection::SetInsertID(const DbType::Ptr& type, const DbReference& objid, const DbReference& dbref)
|
|
|
|
{
|
|
|
|
if (!objid.IsValid())
|
|
|
|
return;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
if (dbref.IsValid())
|
2014-01-31 08:28:00 +01:00
|
|
|
m_InsertIDs[std::make_pair(type, objid)] = dbref;
|
2013-08-02 08:56:36 +02:00
|
|
|
else
|
2014-01-31 08:28:00 +01:00
|
|
|
m_InsertIDs.erase(std::make_pair(type, objid));
|
2013-08-02 08:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DbReference DbConnection::GetInsertID(const DbObject::Ptr& dbobj) const
|
|
|
|
{
|
2014-01-31 08:28:00 +01:00
|
|
|
return GetInsertID(dbobj->GetType(), GetObjectID(dbobj));
|
|
|
|
}
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
DbReference DbConnection::GetInsertID(const DbType::Ptr& type, const DbReference& objid) const
|
|
|
|
{
|
|
|
|
if (!objid.IsValid())
|
2018-01-04 09:28:24 +01:00
|
|
|
return {};
|
2014-01-31 08:28:00 +01:00
|
|
|
|
2016-08-27 08:33:15 +02:00
|
|
|
auto it = m_InsertIDs.find(std::make_pair(type, objid));
|
2013-08-02 08:56:36 +02:00
|
|
|
|
|
|
|
if (it == m_InsertIDs.end())
|
|
|
|
return DbReference();
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-11-22 10:13:42 +01:00
|
|
|
void DbConnection::SetObjectActive(const DbObject::Ptr& dbobj, bool active)
|
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
m_ActiveObjects.insert(dbobj);
|
|
|
|
else
|
|
|
|
m_ActiveObjects.erase(dbobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DbConnection::GetObjectActive(const DbObject::Ptr& dbobj) const
|
|
|
|
{
|
|
|
|
return (m_ActiveObjects.find(dbobj) != m_ActiveObjects.end());
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::ClearIDCache()
|
2013-11-20 09:32:30 +01:00
|
|
|
{
|
2016-07-15 09:40:39 +02:00
|
|
|
SetIDCacheValid(false);
|
|
|
|
|
2013-11-20 09:32:30 +01:00
|
|
|
m_ObjectIDs.clear();
|
|
|
|
m_InsertIDs.clear();
|
2013-11-22 10:13:42 +01:00
|
|
|
m_ActiveObjects.clear();
|
2013-12-05 12:12:57 +01:00
|
|
|
m_ConfigUpdates.clear();
|
|
|
|
m_StatusUpdates.clear();
|
2016-08-14 22:24:51 +02:00
|
|
|
m_ConfigHashes.clear();
|
2013-11-20 09:32:30 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 15:45:50 +02:00
|
|
|
void DbConnection::SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate)
|
|
|
|
{
|
|
|
|
if (hasupdate)
|
|
|
|
m_ConfigUpdates.insert(dbobj);
|
|
|
|
else
|
|
|
|
m_ConfigUpdates.erase(dbobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DbConnection::GetConfigUpdate(const DbObject::Ptr& dbobj) const
|
|
|
|
{
|
|
|
|
return (m_ConfigUpdates.find(dbobj) != m_ConfigUpdates.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbConnection::SetStatusUpdate(const DbObject::Ptr& dbobj, bool hasupdate)
|
|
|
|
{
|
|
|
|
if (hasupdate)
|
|
|
|
m_StatusUpdates.insert(dbobj);
|
|
|
|
else
|
|
|
|
m_StatusUpdates.erase(dbobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DbConnection::GetStatusUpdate(const DbObject::Ptr& dbobj) const
|
|
|
|
{
|
|
|
|
return (m_StatusUpdates.find(dbobj) != m_StatusUpdates.end());
|
|
|
|
}
|
2013-08-02 08:56:36 +02:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void DbConnection::UpdateObject(const ConfigObject::Ptr& object)
|
2015-08-04 14:47:44 +02:00
|
|
|
{
|
2019-05-03 12:44:29 +02:00
|
|
|
bool isShuttingDown = Application::IsShuttingDown();
|
|
|
|
bool isRestarting = Application::IsRestarting();
|
|
|
|
|
|
|
|
#ifdef I2_DEBUG
|
|
|
|
if (isShuttingDown || isRestarting) {
|
|
|
|
//Log(LogDebug, "DbConnection")
|
|
|
|
// << "Updating object '" << object->GetName() << "' \t\t active '" << Convert::ToLong(object->IsActive())
|
|
|
|
// << "' shutting down '" << Convert::ToLong(isShuttingDown) << "' restarting '" << Convert::ToLong(isRestarting) << "'.";
|
|
|
|
}
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
|
|
|
/* Wait until a database connection is established on reconnect. */
|
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Don't update inactive objects during shutdown/reload/restart.
|
|
|
|
* They would be marked as deleted. This gets triggered with ConfigObject::StopObjects().
|
|
|
|
* During startup/reconnect this is fine, the handler is not active there.
|
|
|
|
*/
|
|
|
|
if (isShuttingDown || isRestarting)
|
2015-08-04 14:47:44 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object);
|
|
|
|
|
|
|
|
if (dbobj) {
|
2015-12-15 13:44:58 +01:00
|
|
|
bool dbActive = GetObjectActive(dbobj);
|
2015-08-04 14:47:44 +02:00
|
|
|
bool active = object->IsActive();
|
|
|
|
|
2016-08-13 22:54:22 +02:00
|
|
|
if (active) {
|
|
|
|
if (!dbActive)
|
|
|
|
ActivateObject(dbobj);
|
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
Dictionary::Ptr configFields = dbobj->GetConfigFields();
|
|
|
|
String configHash = dbobj->CalculateConfigHash(configFields);
|
2016-08-25 07:37:41 +02:00
|
|
|
ASSERT(configHash.GetLength() <= 64);
|
2016-08-14 22:24:51 +02:00
|
|
|
configFields->Set("config_hash", configHash);
|
|
|
|
|
|
|
|
String cachedHash = GetConfigHash(dbobj);
|
|
|
|
|
|
|
|
if (cachedHash != configHash) {
|
|
|
|
dbobj->SendConfigUpdateHeavy(configFields);
|
|
|
|
dbobj->SendStatusUpdate();
|
|
|
|
} else {
|
|
|
|
dbobj->SendConfigUpdateLight();
|
|
|
|
}
|
2016-02-05 12:37:00 +01:00
|
|
|
} else if (!active) {
|
2019-05-03 12:44:29 +02:00
|
|
|
/* This may happen on reload/restart actions too
|
|
|
|
* and is blocked above already.
|
|
|
|
*
|
|
|
|
* Deactivate the deleted object no matter
|
2016-02-05 12:37:00 +01:00
|
|
|
* which state it had in the database.
|
|
|
|
*/
|
2015-08-04 14:47:44 +02:00
|
|
|
DeactivateObject(dbobj);
|
2016-02-05 12:37:00 +01:00
|
|
|
}
|
2015-08-04 14:47:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::UpdateAllObjects()
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
|
2015-08-04 14:47:44 +02:00
|
|
|
UpdateObject(object);
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 11:09:13 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::PrepareDatabase()
|
2014-01-28 11:09:13 +01:00
|
|
|
{
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const DbType::Ptr& type : DbType::GetAllTypes()) {
|
2014-01-31 08:28:00 +01:00
|
|
|
FillIDCache(type);
|
|
|
|
}
|
2014-01-28 11:09:13 +01:00
|
|
|
}
|
2014-08-15 17:13:32 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void DbConnection::ValidateFailoverTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils)
|
2014-08-15 17:13:32 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<DbConnection>::ValidateFailoverTimeout(lvalue, utils);
|
2014-11-30 23:32:13 +01:00
|
|
|
|
2019-03-29 15:53:56 +01:00
|
|
|
if (lvalue() < 30)
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "failover_timeout" }, "Failover timeout minimum is 30s."));
|
2014-08-15 17:13:32 +02:00
|
|
|
}
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void DbConnection::ValidateCategories(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils)
|
2017-05-10 14:31:34 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<DbConnection>::ValidateCategories(lvalue, utils);
|
2017-05-10 14:31:34 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
int filter = FilterArrayToInt(lvalue(), DbQuery::GetCategoryFilterMap(), 0);
|
2017-05-10 14:31:34 +02:00
|
|
|
|
2017-08-09 16:30:01 +02:00
|
|
|
if (filter != DbCatEverything && (filter & ~(DbCatInvalid | DbCatEverything | DbCatConfig | DbCatState |
|
2017-12-19 15:50:05 +01:00
|
|
|
DbCatAcknowledgement | DbCatComment | DbCatDowntime | DbCatEventHandler | DbCatExternalCommand |
|
|
|
|
DbCatFlapping | DbCatLog | DbCatNotification | DbCatProgramStatus | DbCatRetention |
|
|
|
|
DbCatStateHistory)) != 0)
|
2017-11-30 18:09:38 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "categories" }, "categories filter is invalid."));
|
2017-05-10 14:31:34 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void DbConnection::IncreaseQueryCount()
|
2015-03-12 09:39:53 +01:00
|
|
|
{
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_StatsMutex);
|
|
|
|
m_QueryStats.InsertValue(now, 1);
|
|
|
|
}
|
|
|
|
|
2017-11-13 16:17:59 +01:00
|
|
|
int DbConnection::GetQueryCount(RingBuffer::SizeType span)
|
2015-03-12 09:39:53 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_StatsMutex);
|
2017-11-13 16:17:59 +01:00
|
|
|
return m_QueryStats.UpdateAndGetValues(Utility::GetTime(), span);
|
2015-03-12 09:39:53 +01:00
|
|
|
}
|
2016-07-15 09:40:39 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool DbConnection::IsIDCacheValid() const
|
2016-07-15 09:40:39 +02:00
|
|
|
{
|
|
|
|
return m_IDCacheValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbConnection::SetIDCacheValid(bool valid)
|
|
|
|
{
|
|
|
|
m_IDCacheValid = valid;
|
|
|
|
}
|
2016-07-29 08:40:10 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int DbConnection::GetSessionToken()
|
2016-07-29 08:40:10 +02:00
|
|
|
{
|
|
|
|
return Application::GetStartTime();
|
|
|
|
}
|