mirror of https://github.com/Icinga/icinga2.git
Remove expired comments and downtimes.
This commit is contained in:
parent
4b5d91caad
commit
49cc9f5898
|
@ -181,6 +181,9 @@ void CompatComponent::DumpComments(ofstream& fp, const DynamicObject::Ptr& owner
|
|||
String id;
|
||||
Dictionary::Ptr comment;
|
||||
BOOST_FOREACH(tie(id, comment), comments) {
|
||||
if (CommentProcessor::IsCommentExpired(comment))
|
||||
continue;
|
||||
|
||||
/* There's no way for us to dump comments that haven't been
|
||||
* assigned a legacy ID yet. */
|
||||
if (!comment->Contains("legacy_id"))
|
||||
|
@ -228,6 +231,9 @@ void CompatComponent::DumpDowntimes(ofstream& fp, const DynamicObject::Ptr& owne
|
|||
String id;
|
||||
Dictionary::Ptr downtime;
|
||||
BOOST_FOREACH(tie(id, downtime), downtimes) {
|
||||
if (DowntimeProcessor::IsDowntimeExpired(downtime))
|
||||
continue;
|
||||
|
||||
/* There's no way for us to dump downtimes that haven't been
|
||||
* assigned a legacy ID yet. */
|
||||
if (!downtime->Contains("legacy_id"))
|
||||
|
|
|
@ -25,6 +25,7 @@ int CommentProcessor::m_NextCommentID = 1;
|
|||
map<int, String> CommentProcessor::m_LegacyCommentCache;
|
||||
map<String, DynamicObject::WeakPtr> CommentProcessor::m_CommentCache;
|
||||
bool CommentProcessor::m_CommentCacheValid;
|
||||
Timer::Ptr CommentProcessor::m_CommentExpireTimer;
|
||||
|
||||
int CommentProcessor::GetNextCommentID(void)
|
||||
{
|
||||
|
@ -108,6 +109,13 @@ Dictionary::Ptr CommentProcessor::GetCommentByID(const String& id)
|
|||
return Dictionary::Ptr();
|
||||
}
|
||||
|
||||
bool CommentProcessor::IsCommentExpired(const Dictionary::Ptr& comment)
|
||||
{
|
||||
double expire_time = comment->Get("expire_time");
|
||||
|
||||
return (expire_time != 0 && expire_time < Utility::GetTime());
|
||||
}
|
||||
|
||||
void CommentProcessor::InvalidateCommentCache(void)
|
||||
{
|
||||
m_CommentCacheValid = false;
|
||||
|
@ -170,5 +178,49 @@ void CommentProcessor::ValidateCommentCache(void)
|
|||
}
|
||||
|
||||
m_CommentCacheValid = true;
|
||||
|
||||
if (!m_CommentExpireTimer) {
|
||||
m_CommentExpireTimer = boost::make_shared<Timer>();
|
||||
m_CommentExpireTimer->SetInterval(300);
|
||||
m_CommentExpireTimer->OnTimerExpired.connect(boost::bind(&CommentProcessor::CommentExpireTimerHandler));
|
||||
m_CommentExpireTimer->Start();
|
||||
}
|
||||
}
|
||||
|
||||
void CommentProcessor::RemoveExpiredComments(const DynamicObject::Ptr& owner)
|
||||
{
|
||||
Dictionary::Ptr comments = owner->Get("comments");
|
||||
|
||||
if (!comments)
|
||||
return;
|
||||
|
||||
vector<String> expiredComments;
|
||||
|
||||
String id;
|
||||
Dictionary::Ptr comment;
|
||||
BOOST_FOREACH(tie(id, comment), comments) {
|
||||
if (IsCommentExpired(comment))
|
||||
expiredComments.push_back(id);
|
||||
}
|
||||
|
||||
if (expiredComments.size() > 0) {
|
||||
BOOST_FOREACH(id, expiredComments) {
|
||||
comments->Remove(id);
|
||||
}
|
||||
|
||||
owner->Touch("comments");
|
||||
}
|
||||
}
|
||||
|
||||
void CommentProcessor::CommentExpireTimerHandler(void)
|
||||
{
|
||||
DynamicObject::Ptr object;
|
||||
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) {
|
||||
RemoveExpiredComments(object);
|
||||
}
|
||||
|
||||
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
|
||||
RemoveExpiredComments(object);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,8 @@ public:
|
|||
static DynamicObject::Ptr GetOwnerByCommentID(const String& id);
|
||||
static Dictionary::Ptr GetCommentByID(const String& id);
|
||||
|
||||
static bool IsCommentExpired(const Dictionary::Ptr& comment);
|
||||
|
||||
static void InvalidateCommentCache(void);
|
||||
static void ValidateCommentCache(void);
|
||||
|
||||
|
@ -61,10 +63,14 @@ private:
|
|||
static map<int, String> m_LegacyCommentCache;
|
||||
static map<String, DynamicObject::WeakPtr> m_CommentCache;
|
||||
static bool m_CommentCacheValid;
|
||||
static Timer::Ptr m_CommentExpireTimer;
|
||||
|
||||
CommentProcessor(void);
|
||||
|
||||
static void CommentExpireTimerHandler(void);
|
||||
|
||||
static void AddCommentsToCache(const DynamicObject::Ptr& owner);
|
||||
static void RemoveExpiredComments(const DynamicObject::Ptr& owner);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ int DowntimeProcessor::m_NextDowntimeID = 1;
|
|||
map<int, String> DowntimeProcessor::m_LegacyDowntimeCache;
|
||||
map<String, DynamicObject::WeakPtr> DowntimeProcessor::m_DowntimeCache;
|
||||
bool DowntimeProcessor::m_DowntimeCacheValid;
|
||||
Timer::Ptr DowntimeProcessor::m_DowntimeExpireTimer;
|
||||
|
||||
int DowntimeProcessor::GetNextDowntimeID(void)
|
||||
{
|
||||
|
@ -109,19 +110,24 @@ bool DowntimeProcessor::IsDowntimeActive(const Dictionary::Ptr& downtime)
|
|||
{
|
||||
double now = Utility::GetTime();
|
||||
|
||||
if (now < static_cast<double>(downtime->Get("start_time")) ||
|
||||
now > static_cast<double>(downtime->Get("end_time")))
|
||||
if (now < downtime->Get("start_time") ||
|
||||
now > downtime->Get("end_time"))
|
||||
return false;
|
||||
|
||||
if (static_cast<bool>(downtime->Get("fixed")))
|
||||
return true;
|
||||
|
||||
double triggerTime = static_cast<double>(downtime->Get("trigger_time"));
|
||||
double triggerTime = downtime->Get("trigger_time");
|
||||
|
||||
if (triggerTime == 0)
|
||||
return false;
|
||||
|
||||
return (triggerTime + static_cast<double>(downtime->Get("duration")) < now);
|
||||
return (triggerTime + downtime->Get("duration") < now);
|
||||
}
|
||||
|
||||
bool DowntimeProcessor::IsDowntimeExpired(const Dictionary::Ptr& downtime)
|
||||
{
|
||||
return (downtime->Get("end_time") < Utility::GetTime());
|
||||
}
|
||||
|
||||
void DowntimeProcessor::InvalidateDowntimeCache(void)
|
||||
|
@ -141,6 +147,8 @@ void DowntimeProcessor::AddDowntimesToCache(const DynamicObject::Ptr& owner)
|
|||
String id;
|
||||
Dictionary::Ptr downtime;
|
||||
BOOST_FOREACH(tie(id, downtime), downtimes) {
|
||||
double end_time = downtime->Get("end_time");
|
||||
|
||||
int legacy_id;
|
||||
|
||||
if (!downtime->Contains("legacy_id")) {
|
||||
|
@ -185,5 +193,49 @@ void DowntimeProcessor::ValidateDowntimeCache(void)
|
|||
}
|
||||
|
||||
m_DowntimeCacheValid = true;
|
||||
|
||||
if (!m_DowntimeExpireTimer) {
|
||||
m_DowntimeExpireTimer = boost::make_shared<Timer>();
|
||||
m_DowntimeExpireTimer->SetInterval(300);
|
||||
m_DowntimeExpireTimer->OnTimerExpired.connect(boost::bind(&DowntimeProcessor::DowntimeExpireTimerHandler));
|
||||
m_DowntimeExpireTimer->Start();
|
||||
}
|
||||
}
|
||||
|
||||
void DowntimeProcessor::RemoveExpiredDowntimes(const DynamicObject::Ptr& owner)
|
||||
{
|
||||
Dictionary::Ptr downtimes = owner->Get("downtimes");
|
||||
|
||||
if (!downtimes)
|
||||
return;
|
||||
|
||||
vector<String> expiredDowntimes;
|
||||
|
||||
String id;
|
||||
Dictionary::Ptr downtime;
|
||||
BOOST_FOREACH(tie(id, downtime), downtimes) {
|
||||
if (IsDowntimeExpired(downtime))
|
||||
expiredDowntimes.push_back(id);
|
||||
}
|
||||
|
||||
if (expiredDowntimes.size() > 0) {
|
||||
BOOST_FOREACH(id, expiredDowntimes) {
|
||||
downtimes->Remove(id);
|
||||
}
|
||||
|
||||
owner->Touch("downtimes");
|
||||
}
|
||||
}
|
||||
|
||||
void DowntimeProcessor::DowntimeExpireTimerHandler(void)
|
||||
{
|
||||
DynamicObject::Ptr object;
|
||||
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Host")->GetObjects()) {
|
||||
RemoveExpiredDowntimes(object);
|
||||
}
|
||||
|
||||
BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
|
||||
RemoveExpiredDowntimes(object);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -45,6 +45,7 @@ public:
|
|||
static Dictionary::Ptr GetDowntimeByID(const String& id);
|
||||
|
||||
static bool IsDowntimeActive(const Dictionary::Ptr& downtime);
|
||||
static bool IsDowntimeExpired(const Dictionary::Ptr& downtime);
|
||||
|
||||
static void InvalidateDowntimeCache(void);
|
||||
static void ValidateDowntimeCache(void);
|
||||
|
@ -55,10 +56,14 @@ private:
|
|||
static map<int, String> m_LegacyDowntimeCache;
|
||||
static map<String, DynamicObject::WeakPtr> m_DowntimeCache;
|
||||
static bool m_DowntimeCacheValid;
|
||||
static Timer::Ptr m_DowntimeExpireTimer;
|
||||
|
||||
DowntimeProcessor(void);
|
||||
|
||||
static void DowntimeExpireTimerHandler(void);
|
||||
|
||||
static void AddDowntimesToCache(const DynamicObject::Ptr& owner);
|
||||
static void RemoveExpiredDowntimes(const DynamicObject::Ptr& owner);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue