icinga2/lib/icingadb/icingadb.cpp

194 lines
4.9 KiB
C++
Raw Normal View History

2019-11-02 14:00:06 +01:00
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2017-09-25 14:41:43 +02:00
2019-10-29 18:36:16 +01:00
#include "icingadb/icingadb.hpp"
#include "icingadb/icingadb-ti.cpp"
#include "icingadb/redisconnection.hpp"
2017-09-25 14:41:43 +02:00
#include "remote/eventqueue.hpp"
#include "base/json.hpp"
#include "icinga/checkable.hpp"
#include "icinga/host.hpp"
2017-10-12 17:46:06 +02:00
#include <boost/algorithm/string.hpp>
2019-11-02 18:01:31 +01:00
#include <memory>
#include <utility>
2017-09-25 14:41:43 +02:00
using namespace icinga;
2017-10-12 17:46:06 +02:00
#define MAX_EVENTS_DEFAULT 5000
using Prio = RedisConnection::QueryPriority;
String IcingaDB::m_EnvironmentId;
boost::once_flag IcingaDB::m_EnvironmentIdOnce = BOOST_ONCE_INIT;
REGISTER_TYPE(IcingaDB);
2017-09-25 14:41:43 +02:00
IcingaDB::IcingaDB()
2019-11-02 18:01:31 +01:00
: m_Rcon(nullptr)
2017-10-12 11:47:49 +02:00
{
m_Rcon = nullptr;
m_WorkQueue.SetName("IcingaDB");
m_PrefixConfigObject = "icinga:";
2019-06-14 11:52:34 +02:00
m_PrefixConfigCheckSum = "icinga:checksum:";
2017-10-12 11:47:49 +02:00
}
2017-09-25 14:41:43 +02:00
2021-07-22 14:34:07 +02:00
void IcingaDB::Validate(int types, const ValidationUtils& utils)
{
ObjectImpl<IcingaDB>::Validate(types, utils);
if (!(types & FAConfig))
return;
if (GetEnableTls() && GetCertPath().IsEmpty() != GetKeyPath().IsEmpty()) {
BOOST_THROW_EXCEPTION(ValidationError(this, std::vector<String>(), "Validation failed: Either both a client certificate (cert_path) and its private key (key_path) or none of them must be given."));
}
}
2017-09-25 14:41:43 +02:00
/**
* Starts the component.
*/
void IcingaDB::Start(bool runtimeCreated)
2017-09-25 14:41:43 +02:00
{
ObjectImpl<IcingaDB>::Start(runtimeCreated);
2017-09-25 14:41:43 +02:00
boost::call_once([]() {
m_EnvironmentId = SHA1(GetEnvironment());
}, m_EnvironmentIdOnce);
Log(LogInformation, "IcingaDB")
2018-05-15 14:43:01 +02:00
<< "'" << GetName() << "' started.";
2017-09-25 14:41:43 +02:00
m_ConfigDumpInProgress = false;
m_ConfigDumpDone = false;
m_WorkQueue.SetExceptionCallback([this](boost::exception_ptr exp) { ExceptionHandler(std::move(exp)); });
2017-09-25 14:41:43 +02:00
2021-07-22 14:34:07 +02:00
m_Rcon = new RedisConnection(GetHost(), GetPort(), GetPath(), GetPassword(), GetDbIndex(),
GetEnableTls(), GetInsecureNoverify(), GetCertPath(), GetKeyPath(), GetCaPath(), GetCrlPath(),
GetTlsProtocolmin(), GetCipherList(), GetDebugInfo());
auto connectedCallback ([this](boost::asio::yield_context& yc) {
m_WorkQueue.Enqueue([this]() { OnConnectedHandler(); });
});
m_Rcon->SetConnectedCallback([this, connectedCallback](boost::asio::yield_context& yc) {
for (auto& kv : m_Rcons) {
kv.second->Start();
}
m_Rcon->SetConnectedCallback(connectedCallback);
connectedCallback(yc);
});
m_Rcon->Start();
2017-09-25 14:41:43 +02:00
for (const Type::Ptr& type : GetTypes()) {
auto ctype (dynamic_cast<ConfigType*>(type.get()));
if (!ctype)
continue;
m_Rcons[ctype] = new RedisConnection(GetHost(), GetPort(), GetPath(), GetPassword(), GetDbIndex(),
2021-07-22 14:34:07 +02:00
GetEnableTls(), GetInsecureNoverify(), GetCertPath(), GetKeyPath(), GetCaPath(), GetCrlPath(),
GetTlsProtocolmin(), GetCipherList(), GetDebugInfo(), m_Rcon);
}
2017-09-25 14:41:43 +02:00
m_StatsTimer = new Timer();
m_StatsTimer->SetInterval(1);
m_StatsTimer->OnTimerExpired.connect([this](const Timer * const&) { PublishStatsTimerHandler(); });
2017-09-25 14:41:43 +02:00
m_StatsTimer->Start();
m_WorkQueue.SetName("IcingaDB");
2017-10-02 09:59:11 +02:00
m_Rcon->SuppressQueryKind(Prio::CheckResult);
m_Rcon->SuppressQueryKind(Prio::RuntimeStateSync);
2017-09-25 14:41:43 +02:00
}
void IcingaDB::ExceptionHandler(boost::exception_ptr exp)
2017-09-25 14:41:43 +02:00
{
Log(LogCritical, "IcingaDB", "Exception during redis query. Verify that Redis is operational.");
2017-09-25 14:41:43 +02:00
Log(LogDebug, "IcingaDB")
2018-05-15 14:43:01 +02:00
<< "Exception during redis operation: " << DiagnosticInformation(exp);
2017-09-25 14:41:43 +02:00
}
void IcingaDB::OnConnectedHandler()
2017-09-25 14:41:43 +02:00
{
AssertOnWorkQueue();
if (m_ConfigDumpInProgress || m_ConfigDumpDone)
2017-09-25 14:41:43 +02:00
return;
2017-10-12 11:47:49 +02:00
2017-09-25 14:41:43 +02:00
/* Config dump */
m_ConfigDumpInProgress = true;
2019-01-28 15:19:10 +01:00
PublishStats();
2017-09-25 14:41:43 +02:00
UpdateAllConfigObjects();
m_ConfigDumpDone = true;
2017-09-25 14:41:43 +02:00
m_ConfigDumpInProgress = false;
}
void IcingaDB::PublishStatsTimerHandler(void)
2017-09-25 14:41:43 +02:00
{
PublishStats();
2017-09-25 14:41:43 +02:00
}
void IcingaDB::PublishStats()
2017-09-25 14:41:43 +02:00
{
if (!m_Rcon || !m_Rcon->IsConnected())
2018-10-26 14:07:07 +02:00
return;
2018-06-08 11:38:36 +02:00
Dictionary::Ptr status = GetStats();
2019-01-28 15:19:10 +01:00
status->Set("config_dump_in_progress", m_ConfigDumpInProgress);
status->Set("timestamp", TimestampToMilliseconds(Utility::GetTime()));
2017-09-25 14:41:43 +02:00
std::vector<String> query {"XADD", "icinga:stats", "MAXLEN", "1", "*"};
{
ObjectLock statusLock (status);
for (auto& kv : status) {
query.emplace_back(kv.first);
query.emplace_back(JsonEncode(kv.second));
}
}
m_Rcon->FireAndForgetQuery(std::move(query), Prio::Heartbeat);
2017-09-25 14:41:43 +02:00
}
void IcingaDB::Stop(bool runtimeRemoved)
2017-09-25 14:41:43 +02:00
{
Log(LogInformation, "IcingaDB")
2018-05-15 14:43:01 +02:00
<< "'" << GetName() << "' stopped.";
2017-09-25 14:41:43 +02:00
ObjectImpl<IcingaDB>::Stop(runtimeRemoved);
2017-09-25 14:41:43 +02:00
}
2021-07-22 14:34:07 +02:00
void IcingaDB::ValidateTlsProtocolmin(const Lazy<String>& lvalue, const ValidationUtils& utils)
{
ObjectImpl<IcingaDB>::ValidateTlsProtocolmin(lvalue, utils);
try {
ResolveTlsProtocolVersion(lvalue());
} catch (const std::exception& ex) {
BOOST_THROW_EXCEPTION(ValidationError(this, { "tls_protocolmin" }, ex.what()));
}
}
void IcingaDB::AssertOnWorkQueue()
2017-09-25 14:41:43 +02:00
{
ASSERT(m_WorkQueue.IsWorkerThread());
}
void IcingaDB::DumpedGlobals::Reset()
{
std::lock_guard<std::mutex> l (m_Mutex);
m_Ids.clear();
}
bool IcingaDB::DumpedGlobals::IsNew(const String& id)
{
std::lock_guard<std::mutex> l (m_Mutex);
return m_Ids.emplace(id).second;
}