Merge pull request #8916 from Icinga/feature/icingadb-last_comment_id

Icinga DB: introduce Checkable#last_comment_id
This commit is contained in:
Alexander Aleksandrovič Klimov 2021-07-29 17:29:51 +02:00 committed by GitHub
commit afca6c001e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 22 deletions

View File

@ -7,6 +7,7 @@
#include "base/timer.hpp" #include "base/timer.hpp"
#include "base/utility.hpp" #include "base/utility.hpp"
#include "base/logger.hpp" #include "base/logger.hpp"
#include <utility>
using namespace icinga; using namespace icinga;
@ -42,6 +43,20 @@ std::set<Comment::Ptr> Checkable::GetComments() const
return m_Comments; return m_Comments;
} }
Comment::Ptr Checkable::GetLastComment() const
{
std::unique_lock<std::mutex> lock (m_CommentMutex);
Comment::Ptr lastComment;
for (auto& comment : m_Comments) {
if (!lastComment || comment->GetEntryTime() > lastComment->GetEntryTime()) {
lastComment = comment;
}
}
return std::move(lastComment);
}
void Checkable::RegisterComment(const Comment::Ptr& comment) void Checkable::RegisterComment(const Comment::Ptr& comment)
{ {
std::unique_lock<std::mutex> lock(m_CommentMutex); std::unique_lock<std::mutex> lock(m_CommentMutex);

View File

@ -152,6 +152,7 @@ public:
void RemoveCommentsByType(int type, const String& removedBy = String()); void RemoveCommentsByType(int type, const String& removedBy = String());
std::set<Comment::Ptr> GetComments() const; std::set<Comment::Ptr> GetComments() const;
Comment::Ptr GetLastComment() const;
void RegisterComment(const Comment::Ptr& comment); void RegisterComment(const Comment::Ptr& comment);
void UnregisterComment(const Comment::Ptr& comment); void UnregisterComment(const Comment::Ptr& comment);

View File

@ -1515,7 +1515,32 @@ unsigned short GetPreviousState(const Checkable::Ptr& checkable, const Service::
} }
} }
void IcingaDB::SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type) void IcingaDB::SendStatusUpdate(const Checkable::Ptr& checkable)
{
if (!m_Rcon || !m_Rcon->IsConnected())
return;
Host::Ptr host;
Service::Ptr service;
Dictionary::Ptr objectAttrs = SerializeState(checkable);
std::vector<String> streamadd({"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*"});
ObjectLock olock(objectAttrs);
tie(host, service) = GetHostService(checkable);
objectAttrs->Set("redis_key", service ? "icinga:service:state" : "icinga:host:state");
objectAttrs->Set("runtime_type", "upsert");
objectAttrs->Set("checksum", HashValue(objectAttrs));
for (const Dictionary::Pair& kv : objectAttrs) {
streamadd.emplace_back(kv.first);
streamadd.emplace_back(IcingaToStreamValue(kv.second));
}
m_Rcon->FireAndForgetQuery(std::move(streamadd), Prio::RuntimeStateStream);
}
void IcingaDB::SendStateChange(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type)
{ {
if (!m_Rcon || !m_Rcon->IsConnected()) if (!m_Rcon || !m_Rcon->IsConnected())
return; return;
@ -1529,25 +1554,7 @@ void IcingaDB::SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResu
tie(host, service) = GetHostService(checkable); tie(host, service) = GetHostService(checkable);
String redisKey; SendStatusUpdate(checkable);
if (service)
redisKey = "icinga:service:state";
else
redisKey = "icinga:host:state";
Dictionary::Ptr objectAttrs = SerializeState(checkable);
objectAttrs->Set("redis_key", redisKey);
objectAttrs->Set("runtime_type", "upsert");
objectAttrs->Set("checksum", HashValue(objectAttrs));
std::vector<String> streamadd({"XADD", "icinga:runtime:state", "MAXLEN", "~", "1000000", "*"});
ObjectLock olock(objectAttrs);
for (const Dictionary::Pair& kv : objectAttrs) {
streamadd.emplace_back(kv.first);
streamadd.emplace_back(IcingaToStreamValue(kv.second));
}
m_Rcon->FireAndForgetQuery(std::move(streamadd), Prio::RuntimeStateStream);
int hard_state; int hard_state;
if (!cr) { if (!cr) {
@ -1890,6 +1897,8 @@ void IcingaDB::SendAddedComment(const Comment::Ptr& comment)
} }
m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History); m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History);
UpdateState(checkable);
SendStatusUpdate(checkable);
} }
void IcingaDB::SendRemovedComment(const Comment::Ptr& comment) void IcingaDB::SendRemovedComment(const Comment::Ptr& comment)
@ -1957,6 +1966,8 @@ void IcingaDB::SendRemovedComment(const Comment::Ptr& comment)
} }
m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History); m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History);
UpdateState(checkable);
SendStatusUpdate(checkable);
} }
void IcingaDB::SendFlappingChange(const Checkable::Ptr& checkable, double changeTime, double flappingLastChange) void IcingaDB::SendFlappingChange(const Checkable::Ptr& checkable, double changeTime, double flappingLastChange)
@ -2248,6 +2259,14 @@ Dictionary::Ptr IcingaDB::SerializeState(const Checkable::Ptr& checkable)
} }
} }
{
auto lastComment (checkable->GetLastComment());
if (lastComment) {
attrs->Set("last_comment_id", GetObjectIdentifier(lastComment));
}
}
attrs->Set("in_downtime", checkable->IsInDowntime()); attrs->Set("in_downtime", checkable->IsInDowntime());
if (checkable->GetCheckTimeout().IsEmpty()) if (checkable->GetCheckTimeout().IsEmpty())
@ -2321,7 +2340,7 @@ void IcingaDB::StateChangeHandler(const ConfigObject::Ptr& object)
void IcingaDB::StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type) void IcingaDB::StateChangeHandler(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type)
{ {
for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) { for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
rw->SendStatusUpdate(object, cr, type); rw->SendStateChange(object, cr, type);
} }
} }

View File

@ -73,7 +73,8 @@ private:
void CreateConfigUpdate(const ConfigObject::Ptr& object, const String type, std::map<String, std::vector<String>>& hMSets, void CreateConfigUpdate(const ConfigObject::Ptr& object, const String type, std::map<String, std::vector<String>>& hMSets,
std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate); std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate);
void SendConfigDelete(const ConfigObject::Ptr& object); void SendConfigDelete(const ConfigObject::Ptr& object);
void SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type); void SendStatusUpdate(const Checkable::Ptr& checkable);
void SendStateChange(const ConfigObject::Ptr& object, const CheckResult::Ptr& cr, StateType type);
void AddObjectDataToRuntimeUpdates(std::vector<Dictionary::Ptr>& runtimeUpdates, const String& objectKey, void AddObjectDataToRuntimeUpdates(std::vector<Dictionary::Ptr>& runtimeUpdates, const String& objectKey,
const String& redisKey, const Dictionary::Ptr& data); const String& redisKey, const Dictionary::Ptr& data);