void DbObject::StateChangedHandler(const ConfigObject::Ptr& object) { DbObject::Ptr dbobj = GetOrCreateByObject(object); if (!dbobj) return; dbobj->SendStatusUpdate(); }
void DbObject::VersionChangedHandler(const ConfigObject::Ptr& object) { DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object); if (dbobj) { dbobj->SendConfigUpdate(); dbobj->SendStatusUpdate(); } }
void DbObject::VarsChangedHandler(const CustomVarObject::Ptr& object) { DbObject::Ptr dbobj = GetOrCreateByObject(object); Log(LogDebug, "DbObject") << "Vars changed for object '" << object->GetName() << "'"; if (!dbobj) return; dbobj->SendVarsStatusUpdate(); }
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) { if (!GetObjectActive(dbobj)) ActivateObject(dbobj); dbobj->SendConfigUpdate(); dbobj->SendStatusUpdate(); } } }
void DbConnection::UpdateObject(const ConfigObject::Ptr& object) { if (!GetConnected()) return; DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object); if (dbobj) { bool active = object->IsActive(); if (active) { ActivateObject(dbobj); dbobj->SendConfigUpdate(); dbobj->SendStatusUpdate(); } else DeactivateObject(dbobj); } }
DbObject::Ptr DbObject::GetOrCreateByObject(const ConfigObject::Ptr& object) { boost::mutex::scoped_lock lock(GetStaticMutex()); DbObject::Ptr dbobj = object->GetExtension("DbObject"); if (dbobj) return dbobj; DbType::Ptr dbtype = DbType::GetByName(object->GetType()->GetName()); if (!dbtype) return DbObject::Ptr(); Service::Ptr service; String name1, name2; service = dynamic_pointer_cast<Service>(object); if (service) { Host::Ptr host = service->GetHost(); name1 = service->GetHost()->GetName(); name2 = service->GetShortName(); } else { if (object->GetType() == ConfigType::GetByName("CheckCommand") || object->GetType() == ConfigType::GetByName("EventCommand") || object->GetType() == ConfigType::GetByName("NotificationCommand")) { Command::Ptr command = dynamic_pointer_cast<Command>(object); name1 = CompatUtility::GetCommandName(command); } else name1 = object->GetName(); } dbobj = dbtype->GetOrCreateObjectByName(name1, name2); dbobj->SetObject(object); object->SetExtension("DbObject", dbobj); return dbobj; }
void IdoPgsqlConnection::InternalActivateObject(const DbObject::Ptr& dbobj) { AssertOnWorkQueue(); if (!GetConnected()) return; DbReference dbref = GetObjectID(dbobj); std::ostringstream qbuf; if (!dbref.IsValid()) { if (!dbobj->GetName2().IsEmpty()) { qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, name2, is_active) VALUES (" << static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", " << "E'" << Escape(dbobj->GetName1()) << "', E'" << Escape(dbobj->GetName2()) << "', 1)"; } else { qbuf << "INSERT INTO " + GetTablePrefix() + "objects (instance_id, objecttype_id, name1, is_active) VALUES (" << static_cast<long>(m_InstanceID) << ", " << dbobj->GetType()->GetTypeID() << ", " << "E'" << Escape(dbobj->GetName1()) << "', 1)"; } Query(qbuf.str()); SetObjectID(dbobj, GetSequenceValue(GetTablePrefix() + "objects", "object_id")); } else { qbuf << "UPDATE " + GetTablePrefix() + "objects SET is_active = 1 WHERE object_id = " << static_cast<long>(dbref); Query(qbuf.str()); } }
DbObject::Ptr DbObject::GetOrCreateByObject(const DynamicObject::Ptr& object) { DbObject::Ptr dbobj = static_pointer_cast<DbObject>(object->GetExtension("DbObject")); if (dbobj) return dbobj; DbType::Ptr dbtype = DbType::GetByName(object->GetType()->GetName()); if (!dbtype) return DbObject::Ptr(); Service::Ptr service; String name1, name2; service = dynamic_pointer_cast<Service>(object); if (service) { Host::Ptr host = service->GetHost(); if (!host) return DbObject::Ptr(); name1 = service->GetHost()->GetName(); name2 = service->GetShortName(); } else { name1 = object->GetName(); } dbobj = dbtype->GetOrCreateObjectByName(name1, name2); { ObjectLock olock(object); dbobj->SetObject(object); object->SetExtension("DbObject", dbobj); } return dbobj; }
void DbConnection::UpdateObject(const ConfigObject::Ptr& object) { if (!GetConnected()) return; DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object); if (dbobj) { bool dbActive = GetObjectActive(dbobj); bool active = object->IsActive(); if (active && !dbActive) { ActivateObject(dbobj); dbobj->SendConfigUpdate(); dbobj->SendStatusUpdate(); } else if (!active) { /* Deactivate the deleted object no matter * which state it had in the database. */ DeactivateObject(dbobj); } } }
void DbConnection::UpdateObject(const ConfigObject::Ptr& object) { if (!GetConnected() || Application::IsShuttingDown()) return; DbObject::Ptr dbobj = DbObject::GetOrCreateByObject(object); if (dbobj) { bool dbActive = GetObjectActive(dbobj); bool active = object->IsActive(); if (active) { if (!dbActive) ActivateObject(dbobj); Dictionary::Ptr configFields = dbobj->GetConfigFields(); String configHash = dbobj->CalculateConfigHash(configFields); ASSERT(configHash.GetLength() <= 64); configFields->Set("config_hash", configHash); String cachedHash = GetConfigHash(dbobj); if (cachedHash != configHash) { dbobj->SendConfigUpdateHeavy(configFields); dbobj->SendStatusUpdate(); } else { dbobj->SendConfigUpdateLight(); } } else if (!active) { /* Deactivate the deleted object no matter * which state it had in the database. */ DeactivateObject(dbobj); } } }
DbReference DbConnection::GetInsertID(const DbObject::Ptr& dbobj) const { return GetInsertID(dbobj->GetType(), GetObjectID(dbobj)); }
void DbConnection::SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref) { SetInsertID(dbobj->GetType(), GetObjectID(dbobj), dbref); }
String DbConnection::GetConfigHash(const DbObject::Ptr& dbobj) const { return GetConfigHash(dbobj->GetType(), GetObjectID(dbobj)); }
void DbConnection::SetConfigHash(const DbObject::Ptr& dbobj, const String& hash) { SetConfigHash(dbobj->GetType(), GetObjectID(dbobj), hash); }