mirror of https://github.com/Icinga/icinga2.git
Merge pull request #8916 from Icinga/feature/icingadb-last_comment_id
Icinga DB: introduce Checkable#last_comment_id
This commit is contained in:
commit
afca6c001e
|
@ -7,6 +7,7 @@
|
|||
#include "base/timer.hpp"
|
||||
#include "base/utility.hpp"
|
||||
#include "base/logger.hpp"
|
||||
#include <utility>
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
|
@ -42,6 +43,20 @@ std::set<Comment::Ptr> Checkable::GetComments() const
|
|||
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)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_CommentMutex);
|
||||
|
|
|
@ -152,6 +152,7 @@ public:
|
|||
void RemoveCommentsByType(int type, const String& removedBy = String());
|
||||
|
||||
std::set<Comment::Ptr> GetComments() const;
|
||||
Comment::Ptr GetLastComment() const;
|
||||
void RegisterComment(const Comment::Ptr& comment);
|
||||
void UnregisterComment(const Comment::Ptr& comment);
|
||||
|
||||
|
|
|
@ -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())
|
||||
return;
|
||||
|
@ -1529,25 +1554,7 @@ void IcingaDB::SendStatusUpdate(const ConfigObject::Ptr& object, const CheckResu
|
|||
|
||||
tie(host, service) = GetHostService(checkable);
|
||||
|
||||
String redisKey;
|
||||
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);
|
||||
SendStatusUpdate(checkable);
|
||||
|
||||
int hard_state;
|
||||
if (!cr) {
|
||||
|
@ -1890,6 +1897,8 @@ void IcingaDB::SendAddedComment(const Comment::Ptr& comment)
|
|||
}
|
||||
|
||||
m_Rcon->FireAndForgetQuery(std::move(xAdd), Prio::History);
|
||||
UpdateState(checkable);
|
||||
SendStatusUpdate(checkable);
|
||||
}
|
||||
|
||||
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);
|
||||
UpdateState(checkable);
|
||||
SendStatusUpdate(checkable);
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
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)
|
||||
{
|
||||
for (const IcingaDB::Ptr& rw : ConfigType::GetObjectsByType<IcingaDB>()) {
|
||||
rw->SendStatusUpdate(object, cr, type);
|
||||
rw->SendStateChange(object, cr, type);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,8 @@ private:
|
|||
void CreateConfigUpdate(const ConfigObject::Ptr& object, const String type, std::map<String, std::vector<String>>& hMSets,
|
||||
std::vector<Dictionary::Ptr>& runtimeUpdates, bool runtimeUpdate);
|
||||
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,
|
||||
const String& redisKey, const Dictionary::Ptr& data);
|
||||
|
||||
|
|
Loading…
Reference in New Issue