mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-25 14:44:32 +02:00
parent
c4962559ff
commit
d0a45521a9
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#include "redis/rediswriter.hpp"
|
#include "redis/rediswriter.hpp"
|
||||||
#include "icinga/command.hpp"
|
#include "icinga/command.hpp"
|
||||||
|
#include "base/configtype.hpp"
|
||||||
|
#include "base/configobject.hpp"
|
||||||
#include "icinga/customvarobject.hpp"
|
#include "icinga/customvarobject.hpp"
|
||||||
#include "icinga/host.hpp"
|
#include "icinga/host.hpp"
|
||||||
#include "icinga/service.hpp"
|
#include "icinga/service.hpp"
|
||||||
@ -60,73 +62,18 @@ void RedisWriter::UpdateAllConfigObjects(void)
|
|||||||
|
|
||||||
double startTime = Utility::GetTime();
|
double startTime = Utility::GetTime();
|
||||||
|
|
||||||
std::map<Type*, std::vector<String>> deleteQueries;
|
|
||||||
long long cursor = 0;
|
|
||||||
|
|
||||||
std::map<String, String> lcTypes;
|
|
||||||
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
|
||||||
lcTypes.emplace(type->GetName().ToLower(), type->GetName());
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
std::shared_ptr<redisReply> reply = ExecuteQuery({ "SCAN", Convert::ToString(cursor), "MATCH", m_PrefixConfigObject + "*", "COUNT", "1000" });
|
|
||||||
|
|
||||||
VERIFY(reply->type == REDIS_REPLY_ARRAY);
|
|
||||||
VERIFY(reply->elements % 2 == 0);
|
|
||||||
|
|
||||||
redisReply *cursorReply = reply->element[0];
|
|
||||||
cursor = Convert::ToLong(cursorReply->str);
|
|
||||||
|
|
||||||
redisReply *keysReply = reply->element[1];
|
|
||||||
|
|
||||||
for (size_t i = 0; i < keysReply->elements; i++) {
|
|
||||||
redisReply *keyReply = keysReply->element[i];
|
|
||||||
VERIFY(keyReply->type == REDIS_REPLY_STRING);
|
|
||||||
|
|
||||||
String key = keyReply->str;
|
|
||||||
String namePair = key.SubStr(m_PrefixConfigObject.GetLength());
|
|
||||||
|
|
||||||
String::SizeType pos = namePair.FindFirstOf(":");
|
|
||||||
|
|
||||||
if (pos == String::NPos)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
String type = namePair.SubStr(0, pos);
|
|
||||||
String name = namePair.SubStr(pos + 1);
|
|
||||||
|
|
||||||
auto actualTypeName = lcTypes.find(type);
|
|
||||||
|
|
||||||
if (actualTypeName == lcTypes.end())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Type::Ptr ptype = Type::GetByName(actualTypeName->second);
|
|
||||||
auto& deleteQuery = deleteQueries[ptype.get()];
|
|
||||||
|
|
||||||
if (deleteQuery.empty()) {
|
|
||||||
deleteQuery.emplace_back("DEL");
|
|
||||||
deleteQuery.emplace_back(m_PrefixConfigCheckSum + type);
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteQuery.push_back(m_PrefixConfigObject + type + ":" + name);
|
|
||||||
deleteQuery.push_back(m_PrefixStatusObject + type + ":" + name);
|
|
||||||
}
|
|
||||||
} while (cursor != 0);
|
|
||||||
|
|
||||||
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
||||||
ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
|
ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
|
||||||
|
|
||||||
if (!ctype)
|
if (!ctype)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
auto lcType (type->GetName().ToLower());
|
||||||
|
|
||||||
ExecuteQuery({ "MULTI" });
|
ExecuteQuery({ "MULTI" });
|
||||||
|
|
||||||
/* Delete obsolete object keys first. */
|
/* Delete obsolete object keys first. */
|
||||||
auto& deleteQuery = deleteQueries[type.get()];
|
ExecuteQuery({"DEL", m_PrefixConfigCheckSum + lcType, m_PrefixConfigObject + lcType, m_PrefixStatusObject + lcType});
|
||||||
|
|
||||||
if (deleteQuery.size() > 1)
|
|
||||||
ExecuteQuery(deleteQuery);
|
|
||||||
|
|
||||||
String typeName = type->GetName().ToLower();
|
|
||||||
|
|
||||||
/* fetch all objects and dump them */
|
/* fetch all objects and dump them */
|
||||||
for (const ConfigObject::Ptr& object : ctype->GetObjects()) {
|
for (const ConfigObject::Ptr& object : ctype->GetObjects()) {
|
||||||
@ -135,7 +82,7 @@ void RedisWriter::UpdateAllConfigObjects(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* publish config type dump finished */
|
/* publish config type dump finished */
|
||||||
ExecuteQuery({ "PUBLISH", "icinga:config:dump", typeName });
|
ExecuteQuery({ "PUBLISH", "icinga:config:dump", lcType });
|
||||||
|
|
||||||
ExecuteQuery({ "EXEC" });
|
ExecuteQuery({ "EXEC" });
|
||||||
}
|
}
|
||||||
@ -518,7 +465,7 @@ void RedisWriter::SendConfigDelete(const ConfigObject::Ptr& object)
|
|||||||
String objectKey = GetObjectIdentifier(object);
|
String objectKey = GetObjectIdentifier(object);
|
||||||
|
|
||||||
ExecuteQueries({
|
ExecuteQueries({
|
||||||
{ "DEL", m_PrefixConfigObject + typeName + ":" + objectKey },
|
{ "HDEL", m_PrefixConfigObject + typeName, objectKey },
|
||||||
{ "DEL", m_PrefixStatusObject + typeName + ":" + objectKey },
|
{ "DEL", m_PrefixStatusObject + typeName + ":" + objectKey },
|
||||||
{ "PUBLISH", "icinga:config:delete", typeName + ":" + objectKey }
|
{ "PUBLISH", "icinga:config:delete", typeName + ":" + objectKey }
|
||||||
});
|
});
|
||||||
@ -622,17 +569,7 @@ void RedisWriter::SendStatusUpdate(const ConfigObject::Ptr& object, bool useTran
|
|||||||
void RedisWriter::UpdateObjectAttrs(const String& keyPrefix, const ConfigObject::Ptr& object, int fieldType)
|
void RedisWriter::UpdateObjectAttrs(const String& keyPrefix, const ConfigObject::Ptr& object, int fieldType)
|
||||||
{
|
{
|
||||||
Type::Ptr type = object->GetReflectionType();
|
Type::Ptr type = object->GetReflectionType();
|
||||||
|
Dictionary::Ptr attrs (new Dictionary);
|
||||||
String typeName = type->GetName().ToLower();
|
|
||||||
|
|
||||||
/* Use the name checksum as unique key. */
|
|
||||||
String objectKey = GetObjectIdentifier(object);
|
|
||||||
|
|
||||||
std::vector<std::vector<String> > queries;
|
|
||||||
|
|
||||||
queries.push_back({ "DEL", keyPrefix + typeName + ":" + objectKey });
|
|
||||||
|
|
||||||
std::vector<String> hmsetCommand({ "HMSET", keyPrefix + typeName + ":" + objectKey });
|
|
||||||
|
|
||||||
for (int fid = 0; fid < type->GetFieldCount(); fid++) {
|
for (int fid = 0; fid < type->GetFieldCount(); fid++) {
|
||||||
Field field = type->GetFieldInfo(fid);
|
Field field = type->GetFieldInfo(fid);
|
||||||
@ -650,15 +587,11 @@ void RedisWriter::UpdateObjectAttrs(const String& keyPrefix, const ConfigObject:
|
|||||||
if (field.Attributes & FANavigation && !(field.Attributes & (FAConfig | FAState)))
|
if (field.Attributes & FANavigation && !(field.Attributes & (FAConfig | FAState)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
hmsetCommand.push_back(field.Name);
|
attrs->Set(field.Name, Serialize(val));
|
||||||
|
|
||||||
Value sval = Serialize(val);
|
|
||||||
hmsetCommand.push_back(JsonEncode(sval));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
queries.push_back(hmsetCommand);
|
/* Use the name checksum as unique key. */
|
||||||
|
ExecuteQuery({"HSET", keyPrefix + type->GetName().ToLower(), GetObjectIdentifier(object), JsonEncode(attrs)});
|
||||||
ExecuteQueries(queries);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RedisWriter::StateChangedHandler(const ConfigObject::Ptr& object)
|
void RedisWriter::StateChangedHandler(const ConfigObject::Ptr& object)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user