icinga2/lib/db_ido/dbconnection.cpp

379 lines
14 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2013-09-25 07:43:57 +02:00
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
2013-09-25 10:41:59 +02:00
#include "db_ido/dbconnection.h"
#include "db_ido/dbvalue.h"
#include "icinga/icingaapplication.h"
#include "icinga/host.h"
#include "icinga/service.h"
#include "base/dynamictype.h"
#include "base/convert.h"
2013-07-30 22:38:33 +02:00
#include "base/utility.h"
2013-08-01 11:07:26 +02:00
#include "base/initialize.h"
#include "base/logger_fwd.h"
#include <boost/foreach.hpp>
using namespace icinga;
Timer::Ptr DbConnection::m_ProgramStatusTimer;
2013-08-01 11:14:33 +02:00
INITIALIZE_ONCE(DbConnection, &DbConnection::StaticInitialize);
2013-08-01 11:07:26 +02:00
void DbConnection::Start(void)
{
DynamicObject::Start();
DbObject::OnQuery.connect(boost::bind(&DbConnection::ExecuteQuery, this, _1));
m_CleanUpTimer = boost::make_shared<Timer>();
m_CleanUpTimer->SetInterval(60);
m_CleanUpTimer->OnTimerExpired.connect(boost::bind(&DbConnection::CleanUpHandler, this));
m_CleanUpTimer->Start();
}
void DbConnection::StaticInitialize(void)
{
m_ProgramStatusTimer = boost::make_shared<Timer>();
m_ProgramStatusTimer->SetInterval(10);
m_ProgramStatusTimer->OnTimerExpired.connect(boost::bind(&DbConnection::ProgramStatusHandler));
m_ProgramStatusTimer->Start();
}
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;
query.Category = DbCatProgramStatus;
query.Fields = boost::make_shared<Dictionary>();
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);
}
void DbConnection::ProgramStatusHandler(void)
{
DbQuery query1;
query1.Table = "programstatus";
query1.Type = DbQueryDelete;
query1.Type = DbCatProgramStatus;
query1.WhereCriteria = boost::make_shared<Dictionary>();
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
DbObject::OnQuery(query1);
DbQuery query2;
query2.Table = "programstatus";
query2.Type = DbQueryInsert;
query2.Category = DbCatProgramStatus;
query2.Fields = boost::make_shared<Dictionary>();
query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
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()));
query2.Fields->Set("is_currently_running", 1);
query2.Fields->Set("process_id", Utility::GetPid());
query2.Fields->Set("daemon_mode", 1);
query2.Fields->Set("last_command_check", DbValue::FromTimestamp(Utility::GetTime()));
query2.Fields->Set("notifications_enabled", 1);
query2.Fields->Set("active_service_checks_enabled", 1);
query2.Fields->Set("passive_service_checks_enabled", 1);
query2.Fields->Set("event_handlers_enabled", 1);
query2.Fields->Set("flap_detection_enabled", 1);
query2.Fields->Set("failure_prediction_enabled", 1);
query2.Fields->Set("process_performance_data", 1);
DbObject::OnQuery(query2);
DbQuery query3;
query3.Table = "runtimevariables";
2013-08-08 08:52:20 +02:00
query3.Type = DbQueryDelete;
query3.Category = DbCatProgramStatus;
2013-08-08 08:52:20 +02:00
query3.WhereCriteria = boost::make_shared<Dictionary>();
query3.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
DbObject::OnQuery(query3);
2013-10-10 23:46:45 +02:00
InsertRuntimeVariable("total_services", static_cast<long>(DynamicType::GetObjects<Service>().size()));
InsertRuntimeVariable("total_scheduled_services", static_cast<long>(DynamicType::GetObjects<Service>().size()));
InsertRuntimeVariable("total_hosts", static_cast<long>(DynamicType::GetObjects<Host>().size()));
InsertRuntimeVariable("total_scheduled_hosts", static_cast<long>(DynamicType::GetObjects<Host>().size()));
}
void DbConnection::CleanUpHandler(void)
{
long now = static_cast<long>(Utility::GetTime());
2013-10-26 09:41:45 +02:00
if (GetCleanupAcknowledgementsAge() > 0) {
CleanUpExecuteQuery("acknowledgements", "entry_time", now - GetCleanupAcknowledgementsAge());
Log(LogDebug, "db_ido", "GetCleanupAcknowledgementsAge: " + Convert::ToString(GetCleanupAcknowledgementsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupAcknowledgementsAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupCommentHistoryAge() > 0) {
CleanUpExecuteQuery("commenthistory", "entry_time", now - GetCleanupCommentHistoryAge());
Log(LogDebug, "db_ido", "GetCleanupCommentHistoryAge: " + Convert::ToString(GetCleanupCommentHistoryAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupCommentHistoryAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupContactNotificationsAge() > 0) {
CleanUpExecuteQuery("contactnotifications", "start_time", now - GetCleanupContactNotificationsAge());
Log(LogDebug, "db_ido", "GetCleanupContactNotificationsAge: " + Convert::ToString(GetCleanupContactNotificationsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupContactNotificationsAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupContactNotificationMethodsAge() > 0) {
CleanUpExecuteQuery("contactnotificationmethods", "start_time", now - GetCleanupContactNotificationMethodsAge());
Log(LogDebug, "db_ido", "GetCleanupContactNotificationMethodsAge: " + Convert::ToString(GetCleanupContactNotificationMethodsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupContactNotificationMethodsAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupDowntimeHistoryAge() > 0) {
CleanUpExecuteQuery("downtimehistory", "entry_time", now - GetCleanupDowntimeHistoryAge());
Log(LogDebug, "db_ido", "CleanUpDowntimeHistoryAge: " + Convert::ToString(GetCleanupDowntimeHistoryAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupDowntimeHistoryAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupEventHandlersAge() > 0) {
CleanUpExecuteQuery("eventhandlers", "start_time", now - GetCleanupEventHandlersAge());
Log(LogDebug, "db_ido", "GetCleanupEventHandlersAge: " + Convert::ToString(GetCleanupEventHandlersAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupEventHandlersAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupExternalCommandsAge() > 0) {
CleanUpExecuteQuery("externalcommands", "entry_time", now - GetCleanupExternalCommandsAge());
Log(LogDebug, "db_ido", "GetCleanupExternalCommandsAge: " + Convert::ToString(GetCleanupExternalCommandsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupExternalCommandsAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupFlappingHistoryAge() > 0) {
CleanUpExecuteQuery("flappinghistory", "event_time", now - GetCleanupFlappingHistoryAge());
Log(LogDebug, "db_ido", "GetCleanupFlappingHistoryAge: " + Convert::ToString(GetCleanupFlappingHistoryAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupFlappingHistoryAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupHostChecksAge() > 0) {
CleanUpExecuteQuery("hostchecks", "start_time", now - GetCleanupHostChecksAge());
Log(LogDebug, "db_ido", "GetCleanupHostChecksAge: " + Convert::ToString(GetCleanupHostChecksAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupHostChecksAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupLogEntriesAge() > 0) {
CleanUpExecuteQuery("logentries", "logentry_time", now - GetCleanupLogEntriesAge());
Log(LogDebug, "db_ido", "GetCleanupLogEntriesAge: " + Convert::ToString(GetCleanupLogEntriesAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupLogEntriesAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupNotificationsAge() > 0) {
CleanUpExecuteQuery("notifications", "start_time", now - GetCleanupNotificationsAge());
Log(LogDebug, "db_ido", "GetCleanupNotificationsAge: " + Convert::ToString(GetCleanupNotificationsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupNotificationsAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupProcessEventsAge() > 0) {
CleanUpExecuteQuery("processevents", "event_time", now - GetCleanupProcessEventsAge());
Log(LogDebug, "db_ido", "GetCleanupProcessEventsAge: " + Convert::ToString(GetCleanupProcessEventsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupProcessEventsAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupStateHistoryAge() > 0) {
CleanUpExecuteQuery("statehistory", "state_time", now - GetCleanupStateHistoryAge());
Log(LogDebug, "db_ido", "GetCleanupStateHistoryAge: " + Convert::ToString(GetCleanupStateHistoryAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupStateHistoryAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupServiceChecksAge() > 0) {
CleanUpExecuteQuery("servicechecks", "start_time", now - GetCleanupServiceChecksAge());
Log(LogDebug, "db_ido", "GetCleanupServiceChecksAge: " + Convert::ToString(GetCleanupServiceChecksAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupServiceChecksAge()));
}
2013-10-26 09:41:45 +02:00
if (GetCleanupSystemCommandsAge() > 0) {
CleanUpExecuteQuery("systemcommands", "start_time", now - GetCleanupSystemCommandsAge());
Log(LogDebug, "db_ido", "GetCleanupSystemCommandsAge: " + Convert::ToString(GetCleanupSystemCommandsAge()) +
" now: " + Convert::ToString(now) +
2013-10-26 09:41:45 +02:00
" old: " + Convert::ToString(now - GetCleanupSystemCommandsAge()));
}
}
void DbConnection::CleanUpExecuteQuery(const String& table, const String& time_key, double time_value)
{
/* Default handler does nothing. */
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupAcknowledgementsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("acknowledgement_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupCommentHistoryAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("commenthistory_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupContactNotificationsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("contactnotifications_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupContactNotificationMethodsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("contactnotificationmethods_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupDowntimeHistoryAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("downtimehistory_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupEventHandlersAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("eventhandlers_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupExternalCommandsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("externalcommands_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupFlappingHistoryAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("flappinghistory_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupHostChecksAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("hostchecks_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupLogEntriesAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("logentries_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupNotificationsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("notifications_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupProcessEventsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("processevents_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupStateHistoryAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("statehistory_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupServiceChecksAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("servicechecks_age");
}
2013-10-26 09:41:45 +02:00
double DbConnection::GetCleanupSystemCommandsAge(void) const
{
2013-10-26 09:41:45 +02:00
return GetCleanup()->Get("systemcommands_age");
}
2013-08-02 08:56:36 +02:00
void DbConnection::SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref)
{
if (dbref.IsValid())
2013-08-02 08:56:36 +02:00
m_ObjectIDs[dbobj] = dbref;
else
2013-08-02 08:56:36 +02:00
m_ObjectIDs.erase(dbobj);
}
2013-08-02 08:56:36 +02:00
DbReference DbConnection::GetObjectID(const DbObject::Ptr& dbobj) const
{
std::map<DbObject::Ptr, DbReference>::const_iterator it;
2013-08-02 08:56:36 +02:00
it = m_ObjectIDs.find(dbobj);
2013-08-02 08:56:36 +02:00
if (it == m_ObjectIDs.end())
return DbReference();
return it->second;
}
2013-08-02 08:56:36 +02:00
void DbConnection::SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref)
{
if (dbref.IsValid())
m_InsertIDs[dbobj] = dbref;
else
m_InsertIDs.erase(dbobj);
}
DbReference DbConnection::GetInsertID(const DbObject::Ptr& dbobj) const
{
std::map<DbObject::Ptr, DbReference>::const_iterator it;
it = m_InsertIDs.find(dbobj);
if (it == m_InsertIDs.end())
return DbReference();
return it->second;
}
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
void DbConnection::ExecuteQuery(const DbQuery&)
{
/* Default handler does nothing. */
}
void DbConnection::UpdateAllObjects(void)
{
DynamicType::Ptr type;
BOOST_FOREACH(const DynamicType::Ptr& dt, DynamicType::GetTypes()) {
BOOST_FOREACH(const DynamicObject::Ptr& object, dt->GetObjects()) {
DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object);
if (dbobj) {
ActivateObject(dbobj);
dbobj->SendConfigUpdate();
dbobj->SendStatusUpdate();
}
}
}
}