icinga2/lib/icinga/service-comment.cpp

305 lines
7.3 KiB
C++
Raw Normal View History

2013-01-29 16:29:09 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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 "i2-icinga.h"
2013-03-16 21:18:53 +01:00
#include "icinga/service.h"
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/foreach.hpp>
2013-01-29 16:29:09 +01:00
using namespace icinga;
int Service::m_NextCommentID = 1;
boost::mutex Service::m_CommentMutex;
2013-03-16 21:18:53 +01:00
std::map<int, String> Service::m_LegacyCommentsCache;
std::map<String, Service::WeakPtr> Service::m_CommentsCache;
2013-03-06 11:03:50 +01:00
bool Service::m_CommentsCacheNeedsUpdate = false;
Timer::Ptr Service::m_CommentsCacheTimer;
Timer::Ptr Service::m_CommentsExpireTimer;
2013-01-29 16:29:09 +01:00
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
int Service::GetNextCommentID(void)
2013-01-29 16:29:09 +01:00
{
2013-03-02 09:07:47 +01:00
boost::mutex::scoped_lock lock(m_CommentMutex);
2013-01-29 16:29:09 +01:00
return m_NextCommentID;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
Dictionary::Ptr Service::GetComments(void) const
{
2013-02-26 10:13:54 +01:00
return m_Comments;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
String Service::AddComment(CommentType entryType, const String& author,
const String& text, double expireTime)
2013-01-29 16:29:09 +01:00
{
Dictionary::Ptr comment = boost::make_shared<Dictionary>();
comment->Set("entry_time", Utility::GetTime());
comment->Set("entry_type", entryType);
comment->Set("author", author);
comment->Set("text", text);
comment->Set("expire_time", expireTime);
int legacy_id;
{
boost::mutex::scoped_lock lock(m_CommentMutex);
legacy_id = m_NextCommentID++;
}
comment->Set("legacy_id", legacy_id);
2013-01-29 16:29:09 +01:00
2013-03-04 15:52:42 +01:00
Dictionary::Ptr comments;
2013-03-02 09:07:47 +01:00
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(this);
2013-01-29 16:29:09 +01:00
2013-03-04 15:52:42 +01:00
comments = GetComments();
if (!comments)
comments = boost::make_shared<Dictionary>();
m_Comments = comments;
}
2013-01-29 16:29:09 +01:00
String id = Utility::NewUUID();
comments->Set(id, comment);
2013-02-26 10:13:54 +01:00
Touch("comments");
2013-01-29 16:29:09 +01:00
return id;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::RemoveAllComments(void)
2013-01-29 16:29:09 +01:00
{
m_Comments = Empty;
Touch("comments");
2013-01-29 16:29:09 +01:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::RemoveComment(const String& id)
2013-01-29 16:29:09 +01:00
{
Service::Ptr owner = GetOwnerByCommentID(id);
2013-01-29 16:29:09 +01:00
if (!owner)
return;
2013-01-29 16:29:09 +01:00
2013-03-02 09:07:47 +01:00
Dictionary::Ptr comments = owner->GetComments();
2013-01-29 16:29:09 +01:00
if (comments) {
comments->Remove(id);
2013-01-29 16:29:09 +01:00
owner->Touch("comments");
}
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
String Service::GetCommentIDFromLegacyID(int id)
{
boost::mutex::scoped_lock lock(m_CommentMutex);
2013-03-16 21:18:53 +01:00
std::map<int, String>::iterator it = m_LegacyCommentsCache.find(id);
if (it == m_LegacyCommentsCache.end())
return Empty;
return it->second;
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
Service::Ptr Service::GetOwnerByCommentID(const String& id)
2013-01-29 16:29:09 +01:00
{
boost::mutex::scoped_lock lock(m_CommentMutex);
2013-01-29 16:29:09 +01:00
return m_CommentsCache[id].lock();
2013-01-29 16:29:09 +01:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
Dictionary::Ptr Service::GetCommentByID(const String& id)
2013-01-29 16:29:09 +01:00
{
Service::Ptr owner = GetOwnerByCommentID(id);
2013-01-29 16:29:09 +01:00
if (!owner)
return Dictionary::Ptr();
2013-01-29 16:29:09 +01:00
2013-03-02 09:07:47 +01:00
Dictionary::Ptr comments = owner->GetComments();
2013-01-29 16:29:09 +01:00
2013-03-02 09:07:47 +01:00
if (comments)
return comments->Get(id);
2013-01-29 16:29:09 +01:00
return Dictionary::Ptr();
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
bool Service::IsCommentExpired(const Dictionary::Ptr& comment)
2013-01-30 14:28:13 +01:00
{
double expire_time = comment->Get("expire_time");
return (expire_time != 0 && expire_time < Utility::GetTime());
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
2013-02-27 15:23:25 +01:00
void Service::InvalidateCommentsCache(void)
{
2013-03-02 09:07:47 +01:00
boost::mutex::scoped_lock lock(m_CommentMutex);
2013-02-27 16:04:49 +01:00
2013-03-06 11:03:50 +01:00
if (m_CommentsCacheNeedsUpdate)
return; /* Someone else has already requested a refresh. */
2013-02-27 16:04:49 +01:00
2013-03-06 11:03:50 +01:00
if (!m_CommentsCacheTimer) {
m_CommentsCacheTimer = boost::make_shared<Timer>();
m_CommentsCacheTimer->SetInterval(0.5);
m_CommentsCacheTimer->OnTimerExpired.connect(boost::bind(&Service::RefreshCommentsCache));
m_CommentsCacheTimer->Start();
2013-03-06 11:03:50 +01:00
}
m_CommentsCacheNeedsUpdate = true;
2013-02-27 15:23:25 +01:00
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::RefreshCommentsCache(void)
2013-01-29 16:29:09 +01:00
{
2013-02-27 15:23:25 +01:00
{
boost::mutex::scoped_lock lock(m_CommentMutex);
if (!m_CommentsCacheNeedsUpdate)
return;
2013-03-06 11:03:50 +01:00
m_CommentsCacheNeedsUpdate = false;
2013-02-27 15:23:25 +01:00
}
2013-03-16 21:18:53 +01:00
Log(LogDebug, "icinga", "Updating Service comments cache.");
2013-03-16 21:18:53 +01:00
std::map<int, String> newLegacyCommentsCache;
std::map<String, Service::WeakPtr> newCommentsCache;
2013-01-29 16:29:09 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
2013-01-29 16:29:09 +01:00
2013-03-04 15:52:42 +01:00
Dictionary::Ptr comments = service->GetComments();
if (!comments)
continue;
ObjectLock olock(comments);
2013-01-29 16:29:09 +01:00
String id;
Dictionary::Ptr comment;
BOOST_FOREACH(tie(id, comment), comments) {
int legacy_id = comment->Get("legacy_id");
2013-01-29 16:29:09 +01:00
if (legacy_id >= m_NextCommentID)
m_NextCommentID = legacy_id + 1;
2013-01-29 16:29:09 +01:00
if (newLegacyCommentsCache.find(legacy_id) != newLegacyCommentsCache.end()) {
/* The legacy_id is already in use by another comment;
* this shouldn't usually happen - assign it a new ID */
2013-01-29 16:29:09 +01:00
legacy_id = m_NextCommentID++;
comment->Set("legacy_id", legacy_id);
service->Touch("comments");
}
newLegacyCommentsCache[legacy_id] = id;
newCommentsCache[id] = service;
}
2013-01-29 16:29:09 +01:00
}
boost::mutex::scoped_lock lock(m_CommentMutex);
m_CommentsCache.swap(newCommentsCache);
m_LegacyCommentsCache.swap(newLegacyCommentsCache);
2013-01-30 14:28:13 +01:00
if (!m_CommentsExpireTimer) {
m_CommentsExpireTimer = boost::make_shared<Timer>();
m_CommentsExpireTimer->SetInterval(300);
m_CommentsExpireTimer->OnTimerExpired.connect(boost::bind(&Service::CommentsExpireTimerHandler));
m_CommentsExpireTimer->Start();
2013-01-30 14:28:13 +01:00
}
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::RemoveExpiredComments(void)
2013-01-30 14:28:13 +01:00
{
2013-03-02 09:07:47 +01:00
Dictionary::Ptr comments = GetComments();
2013-01-30 14:28:13 +01:00
if (!comments)
return;
2013-03-16 21:18:53 +01:00
std::vector<String> expiredComments;
2013-01-30 14:28:13 +01:00
2013-03-04 15:52:42 +01:00
{
ObjectLock olock(comments);
2013-03-04 15:52:42 +01:00
String id;
Dictionary::Ptr comment;
BOOST_FOREACH(tie(id, comment), comments) {
if (IsCommentExpired(comment))
expiredComments.push_back(id);
}
2013-01-30 14:28:13 +01:00
}
2013-03-06 15:41:13 +01:00
if (!expiredComments.empty()) {
2013-03-04 15:52:42 +01:00
BOOST_FOREACH(const String& id, expiredComments) {
2013-01-30 14:28:13 +01:00
comments->Remove(id);
}
Touch("comments");
2013-01-30 14:28:13 +01:00
}
}
2013-03-02 09:07:47 +01:00
/**
* @threadsafety Always.
*/
void Service::CommentsExpireTimerHandler(void)
2013-01-30 14:28:13 +01:00
{
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
Service::Ptr service = dynamic_pointer_cast<Service>(object);
service->RemoveExpiredComments();
2013-01-30 14:28:13 +01:00
}
2013-01-29 16:29:09 +01:00
}