2017-03-15 18:18:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "redis/rediswriter.hpp"
|
2017-03-16 09:44:09 +01:00
|
|
|
#include "icinga/customvarobject.hpp"
|
2017-03-20 09:56:54 +01:00
|
|
|
#include "icinga/host.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
2017-03-15 18:18:01 +01:00
|
|
|
#include "base/json.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/serializer.hpp"
|
2017-03-20 09:56:54 +01:00
|
|
|
#include "base/tlsutility.hpp"
|
2017-03-16 09:44:09 +01:00
|
|
|
#include "base/initialize.hpp"
|
2017-03-15 18:18:01 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
/*
|
|
|
|
- icinga:config:<type> as hash
|
|
|
|
key: sha1 checksum(name)
|
|
|
|
value: JsonEncode(Serialize(object, FAConfig)) + config_checksum
|
|
|
|
|
|
|
|
Diff between calculated config_checksum and Redis json config_checksum
|
|
|
|
Alternative: Replace into.
|
|
|
|
|
|
|
|
|
|
|
|
- icinga:status:<type> as hash
|
|
|
|
key: sha1 checksum(name)
|
|
|
|
value: JsonEncode(Serialize(object, FAState))
|
|
|
|
*/
|
|
|
|
|
2017-03-16 09:44:09 +01:00
|
|
|
INITIALIZE_ONCE(&RedisWriter::ConfigStaticInitialize);
|
|
|
|
|
|
|
|
void RedisWriter::ConfigStaticInitialize(void)
|
|
|
|
{
|
|
|
|
/* triggered in ProcessCheckResult(), requires UpdateNextCheck() to be called before */
|
|
|
|
ConfigObject::OnStateChanged.connect(boost::bind(&RedisWriter::StateChangedHandler, _1));
|
|
|
|
CustomVarObject::OnVarsChanged.connect(boost::bind(&RedisWriter::VarsChangedHandler, _1));
|
|
|
|
|
|
|
|
/* triggered on create, update and delete objects */
|
2017-03-21 16:11:35 +01:00
|
|
|
ConfigObject::OnActiveChanged.connect(boost::bind(&RedisWriter::VersionChangedHandler, _1));
|
2017-03-16 09:44:09 +01:00
|
|
|
ConfigObject::OnVersionChanged.connect(boost::bind(&RedisWriter::VersionChangedHandler, _1));
|
|
|
|
}
|
|
|
|
|
2017-03-15 18:18:01 +01:00
|
|
|
void RedisWriter::UpdateAllConfigObjects(void)
|
|
|
|
{
|
2017-03-16 14:35:29 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2017-03-24 14:39:24 +01:00
|
|
|
double startTime = Utility::GetTime();
|
|
|
|
|
2017-03-21 11:16:04 +01:00
|
|
|
//TODO: "Publish" the config dump by adding another event, globally or by object
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "MULTI" });
|
2017-03-20 13:44:10 +01:00
|
|
|
|
2017-03-15 18:18:01 +01:00
|
|
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
2017-03-16 09:14:45 +01:00
|
|
|
if (!ConfigObject::TypeInstance->IsAssignableFrom(type))
|
|
|
|
continue;
|
2017-03-15 18:18:01 +01:00
|
|
|
|
|
|
|
String typeName = type->GetName();
|
|
|
|
|
|
|
|
/* replace into aka delete insert is faster than a full diff */
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "DEL", "icinga:config:" + typeName, "icinga:config:" + typeName + ":checksum", "icinga:status:" + typeName });
|
2017-03-15 18:18:01 +01:00
|
|
|
|
|
|
|
/* fetch all objects and dump them */
|
|
|
|
ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
|
2017-03-16 09:46:31 +01:00
|
|
|
VERIFY(ctype);
|
2017-03-15 18:18:01 +01:00
|
|
|
|
2017-03-16 09:46:31 +01:00
|
|
|
for (const ConfigObject::Ptr& object : ctype->GetObjects()) {
|
|
|
|
SendConfigUpdate(object, typeName);
|
|
|
|
SendStatusUpdate(object, typeName);
|
2017-03-15 18:18:01 +01:00
|
|
|
}
|
2017-03-21 16:11:35 +01:00
|
|
|
|
|
|
|
/* publish config type dump finished */
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "PUBLISH", "icinga:config:dump", typeName });
|
2017-03-20 13:44:10 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "EXEC" });
|
2017-03-24 14:39:24 +01:00
|
|
|
|
|
|
|
Log(LogInformation, "RedisWriter")
|
|
|
|
<< "Initial config/status dump finished in " << Utility::GetTime() - startTime << " seconds.";
|
2017-03-15 18:18:01 +01:00
|
|
|
}
|
|
|
|
|
2017-03-21 16:11:35 +01:00
|
|
|
void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String& typeName, bool runtimeUpdate)
|
2017-03-15 18:18:01 +01:00
|
|
|
{
|
2017-03-16 14:35:29 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2017-03-21 16:11:35 +01:00
|
|
|
/* during startup we might send duplicated object config, ignore them without any connection */
|
|
|
|
if (!m_Context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* TODO: This isn't essentially correct as we don't keep track of config objects ourselves. This would avoid duplicated config updates at startup.
|
|
|
|
if (!runtimeUpdate && m_ConfigDumpInProgress)
|
|
|
|
return;
|
|
|
|
*/
|
|
|
|
|
2017-03-15 18:18:01 +01:00
|
|
|
/* Serialize config object attributes */
|
|
|
|
Dictionary::Ptr objectAttrs = SerializeObjectAttrs(object, FAConfig);
|
|
|
|
|
|
|
|
String jsonBody = JsonEncode(objectAttrs);
|
|
|
|
|
|
|
|
String objectName = object->GetName();
|
|
|
|
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "HSET", "icinga:config:" + typeName, objectName, jsonBody });
|
2017-03-20 09:56:54 +01:00
|
|
|
|
|
|
|
/* check sums */
|
|
|
|
/* hset icinga:config:Host:checksums localhost { "name_checksum": "...", "properties_checksum": "...", "groups_checksum": "...", "vars_checksum": null } */
|
|
|
|
Dictionary::Ptr checkSum = new Dictionary();
|
|
|
|
|
|
|
|
checkSum->Set("name_checksum", CalculateCheckSumString(object->GetName()));
|
|
|
|
|
2017-03-21 10:21:51 +01:00
|
|
|
// TODO: move this elsewhere
|
|
|
|
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
|
|
|
|
|
|
|
|
if (checkable) {
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
if (service)
|
|
|
|
checkSum->Set("groups_checksum", CalculateCheckSumGroups(service->GetGroups()));
|
|
|
|
else
|
|
|
|
checkSum->Set("groups_checksum", CalculateCheckSumGroups(host->GetGroups()));
|
2017-03-20 09:56:54 +01:00
|
|
|
}
|
|
|
|
|
2017-03-21 11:16:04 +01:00
|
|
|
checkSum->Set("properties_checksum", CalculateCheckSumProperties(object));
|
|
|
|
checkSum->Set("vars_checksum", CalculateCheckSumVars(object));
|
|
|
|
|
2017-03-20 09:56:54 +01:00
|
|
|
String checkSumBody = JsonEncode(checkSum);
|
|
|
|
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "HSET", "icinga:config:" + typeName + ":checksum", objectName, checkSumBody });
|
2017-03-21 16:11:35 +01:00
|
|
|
|
|
|
|
/* publish runtime updated objects immediately */
|
|
|
|
if (!runtimeUpdate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
PUBLISH "icinga:config:dump" "Host"
|
|
|
|
PUBLISH "icinga:config:update" "Host:__name!checksumBody"
|
|
|
|
PUBLISH "icinga:config:delete" "Host:__name"
|
|
|
|
*/
|
|
|
|
|
2017-03-22 09:14:01 +01:00
|
|
|
ExecuteQuery({ "PUBLISH", "icinga:config:update", typeName + ":" + objectName + "!" + checkSumBody });
|
2017-03-21 16:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedisWriter::SendConfigDelete(const ConfigObject::Ptr& object, const String& typeName)
|
|
|
|
{
|
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
|
|
|
/* during startup we might send duplicated object config, ignore them without any connection */
|
|
|
|
if (!m_Context)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String objectName = object->GetName();
|
|
|
|
|
2017-03-22 09:28:28 +01:00
|
|
|
ExecuteQuery({ "HDEL", "icinga:config:" + typeName, objectName });
|
|
|
|
ExecuteQuery({ "HDEL", "icinga:config:" + typeName + ":checksum", objectName });
|
|
|
|
ExecuteQuery({ "HDEL", "icinga:status:" + typeName, objectName });
|
2017-03-21 16:11:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
PUBLISH "icinga:config:dump" "Host"
|
|
|
|
PUBLISH "icinga:config:update" "Host:__name!checksumBody"
|
|
|
|
PUBLISH "icinga:config:delete" "Host:__name"
|
|
|
|
*/
|
|
|
|
|
2017-03-22 09:28:28 +01:00
|
|
|
ExecuteQuery({ "PUBLISH", "icinga:config:delete", typeName + ":" + objectName });
|
2017-03-15 18:18:01 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 09:44:09 +01:00
|
|
|
void RedisWriter::SendStatusUpdate(const ConfigObject::Ptr& object, const String& typeName)
|
|
|
|
{
|
2017-03-16 14:35:29 +01:00
|
|
|
AssertOnWorkQueue();
|
|
|
|
|
2017-03-24 14:11:20 +01:00
|
|
|
/* during startup we might receive check results, ignore them without any connection */
|
|
|
|
if (!m_Context)
|
|
|
|
return;
|
|
|
|
|
2017-03-16 09:44:09 +01:00
|
|
|
/* Serialize config object attributes */
|
|
|
|
Dictionary::Ptr objectAttrs = SerializeObjectAttrs(object, FAState);
|
|
|
|
|
|
|
|
String jsonBody = JsonEncode(objectAttrs);
|
|
|
|
|
|
|
|
String objectName = object->GetName();
|
|
|
|
|
2017-03-22 09:28:28 +01:00
|
|
|
ExecuteQuery({ "HSET", "icinga:status:" + typeName, objectName, jsonBody });
|
2017-03-16 09:44:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RedisWriter::StateChangedHandler(const ConfigObject::Ptr& object)
|
|
|
|
{
|
|
|
|
Type::Ptr type = object->GetReflectionType();
|
|
|
|
|
|
|
|
for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
|
2017-03-16 15:33:59 +01:00
|
|
|
rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendStatusUpdate, rw, object, type->GetName()));
|
2017-03-16 09:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedisWriter::VarsChangedHandler(const ConfigObject::Ptr& object)
|
|
|
|
{
|
|
|
|
Type::Ptr type = object->GetReflectionType();
|
|
|
|
|
|
|
|
for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
|
2017-03-21 16:11:35 +01:00
|
|
|
rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigUpdate, rw, object, type->GetName(), true));
|
2017-03-16 09:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RedisWriter::VersionChangedHandler(const ConfigObject::Ptr& object)
|
|
|
|
{
|
|
|
|
Type::Ptr type = object->GetReflectionType();
|
|
|
|
|
2017-03-21 16:11:35 +01:00
|
|
|
if (object->IsActive()) {
|
|
|
|
/* Create or update the object config */
|
|
|
|
for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
|
|
|
|
rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigUpdate, rw.get(), object, type->GetName(), true));
|
|
|
|
}
|
2017-03-28 10:23:46 +02:00
|
|
|
} else if (!object->IsActive() && object->GetExtension("ConfigObjectDeleted")) { /* same as in apilistener-configsync.cpp */
|
2017-03-21 16:11:35 +01:00
|
|
|
/* Delete object config */
|
|
|
|
for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
|
|
|
|
rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigDelete, rw.get(), object, type->GetName()));
|
|
|
|
}
|
2017-03-16 09:44:09 +01:00
|
|
|
}
|
|
|
|
}
|