2013-07-05 09:37:04 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-07-05 09:37:04 +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/dbconnection.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "db_ido/dbconnection.tcpp"
|
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
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
DbConnection::DbConnection(void)
|
2015-12-16 08:49:47 +01:00
|
|
|
: m_QueryStats(15 * 60), m_PendingQueries(0), m_PendingQueriesTimestamp(0)
|
2015-03-12 09:39:53 +01:00
|
|
|
{ }
|
|
|
|
|
2014-08-13 20:30:28 +02:00
|
|
|
void DbConnection::OnConfigLoaded(void)
|
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::OnConfigLoaded();
|
2014-08-13 20:30:28 +02:00
|
|
|
|
|
|
|
if (!GetEnableHa()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "DbConnection")
|
|
|
|
<< "HA functionality disabled. Won't pause IDO connection: " << GetName();
|
|
|
|
|
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
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
DbObject::OnQuery.connect(boost::bind(&DbConnection::ExecuteQuery, this, _1));
|
2015-12-14 11:36:03 +01:00
|
|
|
DbObject::OnMultipleQueries.connect(boost::bind(&DbConnection::ExecuteMultipleQueries, this, _1));
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::OnActiveChanged.connect(boost::bind(&DbConnection::UpdateObject, this, _1));
|
2014-05-09 13:02:30 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 08:49:47 +01:00
|
|
|
void DbConnection::StatsLoggerTimerHandler(void)
|
|
|
|
{
|
2015-12-17 12:05:35 +01:00
|
|
|
if (!GetConnected())
|
|
|
|
return;
|
|
|
|
|
2015-12-16 08:49:47 +01:00
|
|
|
int pending = GetPendingQueryCount();
|
|
|
|
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
double gradient = (pending - m_PendingQueries) / (now - m_PendingQueriesTimestamp);
|
|
|
|
double timeToZero = -pending / gradient;
|
|
|
|
|
|
|
|
String timeInfo;
|
|
|
|
|
|
|
|
if (pending > GetQueryCount(5)) {
|
|
|
|
timeInfo = " empty in ";
|
|
|
|
if (timeToZero < 0)
|
|
|
|
timeInfo += "infinite time, your database isn't able to keep up";
|
|
|
|
else
|
|
|
|
timeInfo += Utility::FormatDuration(timeToZero);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_PendingQueries = pending;
|
|
|
|
m_PendingQueriesTimestamp = now;
|
|
|
|
|
|
|
|
Log(LogInformation, GetReflectionType()->GetName())
|
|
|
|
<< "Query queue items: " << pending
|
|
|
|
<< ", query rate: " << std::setw(2) << GetQueryCount(60) / 60.0 << "/s"
|
|
|
|
<< " (" << GetQueryCount(60) << "/min " << GetQueryCount(5 * 60) << "/5min " << GetQueryCount(15 * 60) << "/15min);"
|
|
|
|
<< timeInfo;
|
|
|
|
}
|
|
|
|
|
2014-05-09 13:02:30 +02:00
|
|
|
void DbConnection::Resume(void)
|
|
|
|
{
|
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")
|
|
|
|
<< "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);
|
|
|
|
m_CleanUpTimer->OnTimerExpired.connect(boost::bind(&DbConnection::CleanUpHandler, this));
|
|
|
|
m_CleanUpTimer->Start();
|
2015-12-16 08:49:47 +01:00
|
|
|
|
|
|
|
m_StatsLoggerTimer = new Timer();
|
|
|
|
m_StatsLoggerTimer->SetInterval(15);
|
|
|
|
m_StatsLoggerTimer->OnTimerExpired.connect(boost::bind(&DbConnection::StatsLoggerTimerHandler, this));
|
|
|
|
m_StatsLoggerTimer->Start();
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 13:02:30 +02:00
|
|
|
void DbConnection::Pause(void)
|
|
|
|
{
|
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")
|
|
|
|
<< "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;
|
|
|
|
query1.WhereCriteria = new Dictionary();
|
|
|
|
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
query1.Fields = new Dictionary();
|
|
|
|
query1.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
query1.Fields->Set("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
|
|
|
}
|
|
|
|
|
2015-01-27 09:53:07 +01:00
|
|
|
void DbConnection::InitializeDbTimer(void)
|
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);
|
|
|
|
m_ProgramStatusTimer->OnTimerExpired.connect(boost::bind(&DbConnection::ProgramStatusHandler));
|
|
|
|
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;
|
2014-11-08 21:17:16 +01:00
|
|
|
query.Fields = new Dictionary();
|
2013-08-08 08:47:29 +02:00
|
|
|
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
query.Fields->Set("varname", key);
|
|
|
|
query.Fields->Set("varvalue", value);
|
|
|
|
DbObject::OnQuery(query);
|
|
|
|
}
|
|
|
|
|
2013-07-25 09:00:23 +02:00
|
|
|
void DbConnection::ProgramStatusHandler(void)
|
|
|
|
{
|
2015-10-15 18:43:30 +02:00
|
|
|
Log(LogNotice, "DbConnection")
|
|
|
|
<< "Updating programstatus table.";
|
|
|
|
|
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";
|
2013-07-25 09:00:23 +02:00
|
|
|
query1.Type = DbQueryDelete;
|
2013-10-29 16:08:52 +01:00
|
|
|
query1.Category = DbCatProgramStatus;
|
2014-11-08 21:17:16 +01:00
|
|
|
query1.WhereCriteria = new Dictionary();
|
2013-07-25 09:00:23 +02:00
|
|
|
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
2015-12-10 16:54:43 +01:00
|
|
|
query1.Priority = PriorityHigh;
|
2015-12-14 11:36:03 +01:00
|
|
|
queries.push_back(query1);
|
2013-07-25 09:00:23 +02:00
|
|
|
|
|
|
|
DbQuery query2;
|
2013-08-01 09:46:48 +02:00
|
|
|
query2.Table = "programstatus";
|
2013-10-30 15:32:33 +01:00
|
|
|
query2.IdColumn = "programstatus_id";
|
2013-07-25 09:00:23 +02:00
|
|
|
query2.Type = DbQueryInsert;
|
2013-10-29 15:54:43 +01:00
|
|
|
query2.Category = DbCatProgramStatus;
|
2013-07-25 09:00:23 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
query2.Fields = new Dictionary();
|
2013-07-25 09:00:23 +02:00
|
|
|
query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
2015-08-20 16:43:03 +02:00
|
|
|
query2.Fields->Set("program_version", Application::GetAppVersion());
|
2013-07-25 09:00:23 +02:00
|
|
|
query2.Fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
|
2013-10-26 09:41:45 +02:00
|
|
|
query2.Fields->Set("program_start_time", DbValue::FromTimestamp(Application::GetStartTime()));
|
2013-07-25 09:00:23 +02:00
|
|
|
query2.Fields->Set("is_currently_running", 1);
|
2014-08-13 17:46:43 +02:00
|
|
|
query2.Fields->Set("endpoint_name", IcingaApplication::GetInstance()->GetNodeName());
|
2013-07-25 09:00:23 +02:00
|
|
|
query2.Fields->Set("process_id", Utility::GetPid());
|
|
|
|
query2.Fields->Set("daemon_mode", 1);
|
|
|
|
query2.Fields->Set("last_command_check", DbValue::FromTimestamp(Utility::GetTime()));
|
2014-03-20 11:33:59 +01:00
|
|
|
query2.Fields->Set("notifications_enabled", (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0));
|
2014-04-17 11:29:47 +02:00
|
|
|
query2.Fields->Set("active_host_checks_enabled", (IcingaApplication::GetInstance()->GetEnableHostChecks() ? 1 : 0));
|
|
|
|
query2.Fields->Set("passive_host_checks_enabled", 1);
|
|
|
|
query2.Fields->Set("active_service_checks_enabled", (IcingaApplication::GetInstance()->GetEnableServiceChecks() ? 1 : 0));
|
2013-07-25 09:00:23 +02:00
|
|
|
query2.Fields->Set("passive_service_checks_enabled", 1);
|
2014-03-20 11:33:59 +01:00
|
|
|
query2.Fields->Set("event_handlers_enabled", (IcingaApplication::GetInstance()->GetEnableEventHandlers() ? 1 : 0));
|
|
|
|
query2.Fields->Set("flap_detection_enabled", (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0));
|
2014-03-12 15:21:56 +01:00
|
|
|
query2.Fields->Set("process_performance_data", (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0));
|
2015-12-10 16:54:43 +01:00
|
|
|
query2.Priority = PriorityHigh;
|
2015-12-14 11:36:03 +01:00
|
|
|
queries.push_back(query2);
|
|
|
|
|
|
|
|
DbObject::OnMultipleQueries(queries);
|
2013-08-08 08:47:29 +02:00
|
|
|
|
|
|
|
DbQuery query3;
|
|
|
|
query3.Table = "runtimevariables";
|
2013-08-08 08:52:20 +02:00
|
|
|
query3.Type = DbQueryDelete;
|
2013-10-29 15:54:43 +01:00
|
|
|
query3.Category = DbCatProgramStatus;
|
2014-11-08 21:17:16 +01:00
|
|
|
query3.WhereCriteria = new Dictionary();
|
2013-08-08 08:52:20 +02:00
|
|
|
query3.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
DbObject::OnQuery(query3);
|
2013-08-08 08:47:29 +02:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
InsertRuntimeVariable("total_services", std::distance(ConfigType::GetObjectsByType<Service>().first, ConfigType::GetObjectsByType<Service>().second));
|
|
|
|
InsertRuntimeVariable("total_scheduled_services", std::distance(ConfigType::GetObjectsByType<Service>().first, ConfigType::GetObjectsByType<Service>().second));
|
|
|
|
InsertRuntimeVariable("total_hosts", std::distance(ConfigType::GetObjectsByType<Host>().first, ConfigType::GetObjectsByType<Host>().second));
|
|
|
|
InsertRuntimeVariable("total_scheduled_hosts", std::distance(ConfigType::GetObjectsByType<Host>().first, ConfigType::GetObjectsByType<Host>().second));
|
2013-07-25 09:00:23 +02:00
|
|
|
}
|
|
|
|
|
2013-09-26 15:22:21 +02:00
|
|
|
void DbConnection::CleanUpHandler(void)
|
|
|
|
{
|
|
|
|
long now = static_cast<long>(Utility::GetTime());
|
|
|
|
|
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" },
|
|
|
|
{ "flappinghistory" "event_time" },
|
|
|
|
{ "hostchecks", "start_time" },
|
|
|
|
{ "logentries", "logentry_time" },
|
|
|
|
{ "notifications", "start_time" },
|
|
|
|
{ "processevents", "event_time" },
|
|
|
|
{ "statehistory", "state_time" },
|
|
|
|
{ "servicechecks", "start_time" },
|
|
|
|
{ "systemcommands", "start_time" }
|
|
|
|
};
|
|
|
|
|
2014-05-23 11:05:25 +02:00
|
|
|
for (size_t i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) {
|
2013-10-30 14:08:09 +01:00
|
|
|
double max_age = GetCleanup()->Get(tables[i].name + "_age");
|
|
|
|
|
|
|
|
if (max_age == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CleanUpExecuteQuery(tables[i].name, tables[i].time_column, now - max_age);
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "DbConnection")
|
|
|
|
<< "Cleanup (" << tables[i].name << "): " << max_age
|
|
|
|
<< " 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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
std::map<DbObject::Ptr, DbReference>::const_iterator it;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
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())
|
2013-07-05 09:37:04 +02:00
|
|
|
return DbReference();
|
|
|
|
|
|
|
|
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())
|
|
|
|
return DbReference();
|
|
|
|
|
|
|
|
std::map<std::pair<DbType::Ptr, DbReference>, DbReference>::const_iterator it;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:45:25 +02:00
|
|
|
void DbConnection::SetNotificationInsertID(const CustomVarObject::Ptr& obj, const DbReference& dbref)
|
2014-01-29 10:30:33 +01:00
|
|
|
{
|
|
|
|
if (dbref.IsValid())
|
|
|
|
m_NotificationInsertIDs[obj] = dbref;
|
|
|
|
else
|
|
|
|
m_NotificationInsertIDs.erase(obj);
|
|
|
|
}
|
|
|
|
|
2014-05-12 16:45:25 +02:00
|
|
|
DbReference DbConnection::GetNotificationInsertID(const CustomVarObject::Ptr& obj) const
|
2014-01-29 10:30:33 +01:00
|
|
|
{
|
2014-05-12 16:45:25 +02:00
|
|
|
std::map<CustomVarObject::Ptr, DbReference>::const_iterator it;
|
2014-01-29 10:30:33 +01:00
|
|
|
|
|
|
|
it = m_NotificationInsertIDs.find(obj);
|
|
|
|
|
|
|
|
if (it == m_NotificationInsertIDs.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());
|
|
|
|
}
|
|
|
|
|
2013-11-20 09:32:30 +01:00
|
|
|
void DbConnection::ClearIDCache(void)
|
|
|
|
{
|
|
|
|
m_ObjectIDs.clear();
|
|
|
|
m_InsertIDs.clear();
|
2014-01-29 10:30:33 +01:00
|
|
|
m_NotificationInsertIDs.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();
|
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
|
|
|
{
|
|
|
|
if (!GetConnected())
|
|
|
|
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();
|
|
|
|
|
2015-12-15 13:44:58 +01:00
|
|
|
if (active && !dbActive) {
|
2015-08-04 14:47:44 +02:00
|
|
|
ActivateObject(dbobj);
|
|
|
|
dbobj->SendConfigUpdate();
|
|
|
|
dbobj->SendStatusUpdate();
|
2015-12-15 13:44:58 +01:00
|
|
|
} else if (!active && dbActive)
|
2015-08-04 14:47:44 +02:00
|
|
|
DeactivateObject(dbobj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
void DbConnection::UpdateAllObjects(void)
|
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigType::Ptr type;
|
|
|
|
BOOST_FOREACH(const ConfigType::Ptr& dt, ConfigType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const ConfigObject::Ptr& object, dt->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
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
void DbConnection::PrepareDatabase(void)
|
2014-01-28 11:09:13 +01:00
|
|
|
{
|
2014-08-13 17:46:43 +02:00
|
|
|
/*
|
2014-03-09 18:20:45 +01:00
|
|
|
* only clear tables on reconnect which
|
|
|
|
* cannot be updated by their existing ids
|
|
|
|
* for details check https://dev.icinga.org/issues/5565
|
|
|
|
*/
|
|
|
|
|
2014-01-31 08:28:00 +01:00
|
|
|
//ClearConfigTable("commands");
|
2015-12-15 15:29:48 +01:00
|
|
|
//ClearConfigTable("comments");
|
2014-01-28 11:09:13 +01:00
|
|
|
ClearConfigTable("contact_addresses");
|
|
|
|
ClearConfigTable("contact_notificationcommands");
|
|
|
|
ClearConfigTable("contactgroup_members");
|
2014-01-31 08:28:00 +01:00
|
|
|
//ClearConfigTable("contactgroups");
|
|
|
|
//ClearConfigTable("contacts");
|
|
|
|
//ClearConfigTable("contactstatus");
|
2015-09-28 13:19:13 +02:00
|
|
|
//ClearConfigTable("customvariables");
|
|
|
|
//ClearConfigTable("customvariablestatus");
|
2015-07-08 13:58:12 +02:00
|
|
|
//ClearConfigTable("endpoints");
|
|
|
|
//ClearConfigTable("endpointstatus");
|
2014-01-28 11:09:13 +01:00
|
|
|
ClearConfigTable("host_contactgroups");
|
|
|
|
ClearConfigTable("host_contacts");
|
|
|
|
ClearConfigTable("host_parenthosts");
|
|
|
|
ClearConfigTable("hostdependencies");
|
|
|
|
ClearConfigTable("hostgroup_members");
|
2014-01-31 08:28:00 +01:00
|
|
|
//ClearConfigTable("hostgroups");
|
|
|
|
//ClearConfigTable("hosts");
|
|
|
|
//ClearConfigTable("hoststatus");
|
2015-12-15 15:29:48 +01:00
|
|
|
//ClearConfigTable("scheduleddowntime");
|
2014-04-23 22:41:51 +02:00
|
|
|
ClearConfigTable("service_contactgroups");
|
|
|
|
ClearConfigTable("service_contacts");
|
2014-03-06 09:37:26 +01:00
|
|
|
ClearConfigTable("servicedependencies");
|
2014-04-23 22:41:51 +02:00
|
|
|
ClearConfigTable("servicegroup_members");
|
2014-01-31 08:28:00 +01:00
|
|
|
//ClearConfigTable("servicegroups");
|
|
|
|
//ClearConfigTable("services");
|
|
|
|
//ClearConfigTable("servicestatus");
|
2014-04-23 22:41:51 +02:00
|
|
|
ClearConfigTable("timeperiod_timeranges");
|
2014-01-31 08:28:00 +01:00
|
|
|
//ClearConfigTable("timeperiods");
|
|
|
|
|
|
|
|
BOOST_FOREACH(const DbType::Ptr& type, DbType::GetAllTypes()) {
|
|
|
|
FillIDCache(type);
|
|
|
|
}
|
2014-01-28 11:09:13 +01:00
|
|
|
}
|
2014-08-15 17:13:32 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
void DbConnection::ValidateFailoverTimeout(double value, const ValidationUtils& utils)
|
2014-08-15 17:13:32 +02:00
|
|
|
{
|
2014-11-30 23:32:13 +01:00
|
|
|
ObjectImpl<DbConnection>::ValidateFailoverTimeout(value, utils);
|
|
|
|
|
|
|
|
if (value < 60)
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("failover_timeout"), "Failover timeout minimum is 60s."));
|
2014-08-15 17:13:32 +02:00
|
|
|
}
|
2015-03-12 09:39:53 +01:00
|
|
|
|
|
|
|
void DbConnection::IncreaseQueryCount(void)
|
|
|
|
{
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_StatsMutex);
|
|
|
|
m_QueryStats.InsertValue(now, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DbConnection::GetQueryCount(RingBuffer::SizeType span) const
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_StatsMutex);
|
|
|
|
return m_QueryStats.GetValues(span);
|
|
|
|
}
|