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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
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"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/timer.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-03-18 11:02:18 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
static int l_NextCommentID = 1;
|
|
|
|
static boost::mutex l_CommentMutex;
|
|
|
|
static std::map<int, String> l_LegacyCommentsCache;
|
|
|
|
static std::map<String, Service::WeakPtr> l_CommentsCache;
|
|
|
|
static bool l_CommentsCacheNeedsUpdate = false;
|
|
|
|
static Timer::Ptr l_CommentsCacheTimer;
|
|
|
|
static Timer::Ptr l_CommentsExpireTimer;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
int Service::GetNextCommentID(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
return l_NextCommentID;
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 18:39:43 +01:00
|
|
|
Dictionary::Ptr Service::GetComments(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Comments;
|
2013-02-09 18:39:43 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
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);
|
2013-02-27 12:44:51 +01:00
|
|
|
|
|
|
|
int legacy_id;
|
|
|
|
|
|
|
|
{
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
|
|
|
legacy_id = l_NextCommentID++;
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-01-30 09:59:22 +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-02-09 01:16:43 +01:00
|
|
|
void Service::RemoveAllComments(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-27 12:44:51 +01:00
|
|
|
m_Comments = Empty;
|
|
|
|
Touch("comments");
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
void Service::RemoveComment(const String& id)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::Ptr owner = GetOwnerByCommentID(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
if (!owner)
|
2013-01-31 16:23:02 +01:00
|
|
|
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) {
|
2013-01-30 09:59:22 +01:00
|
|
|
comments->Remove(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
owner->Touch("comments");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
String Service::GetCommentIDFromLegacyID(int id)
|
2013-01-30 09:59:22 +01:00
|
|
|
{
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
std::map<int, String>::iterator it = l_LegacyCommentsCache.find(id);
|
2013-01-30 13:02:20 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (it == l_LegacyCommentsCache.end())
|
2013-01-31 16:23:02 +01:00
|
|
|
return Empty;
|
2013-01-30 13:02:20 +01:00
|
|
|
|
|
|
|
return it->second;
|
2013-01-30 09:59:22 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::Ptr Service::GetOwnerByCommentID(const String& id)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
return l_CommentsCache[id].lock();
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
Dictionary::Ptr Service::GetCommentByID(const String& id)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-09 01:16:43 +01:00
|
|
|
Service::Ptr owner = GetOwnerByCommentID(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
|
|
|
if (!owner)
|
2013-01-31 16:23:02 +01:00
|
|
|
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-02-09 01:16:43 +01:00
|
|
|
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-02-27 15:23:25 +01:00
|
|
|
void Service::InvalidateCommentsCache(void)
|
|
|
|
{
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (l_CommentsCacheNeedsUpdate)
|
2013-03-06 11:03:50 +01:00
|
|
|
return; /* Someone else has already requested a refresh. */
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (!l_CommentsCacheTimer) {
|
|
|
|
l_CommentsCacheTimer = boost::make_shared<Timer>();
|
|
|
|
l_CommentsCacheTimer->SetInterval(0.5);
|
|
|
|
l_CommentsCacheTimer->OnTimerExpired.connect(boost::bind(&Service::RefreshCommentsCache));
|
|
|
|
l_CommentsCacheTimer->Start();
|
2013-03-06 11:03:50 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
l_CommentsCacheNeedsUpdate = true;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
void Service::RefreshCommentsCache(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-02-27 15:23:25 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (!l_CommentsCacheNeedsUpdate)
|
2013-03-06 13:01:51 +01:00
|
|
|
return;
|
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
l_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-06 13:01:51 +01:00
|
|
|
|
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
|
|
|
|
2013-02-27 12:44:51 +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();
|
2013-01-30 13:02:20 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
if (!comments)
|
|
|
|
continue;
|
2013-01-30 09:59:22 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
ObjectLock olock(comments);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-27 12:44:51 +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
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (legacy_id >= l_NextCommentID)
|
|
|
|
l_NextCommentID = legacy_id + 1;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-02-27 12:44:51 +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
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
legacy_id = l_NextCommentID++;
|
2013-02-27 12:44:51 +01:00
|
|
|
comment->Set("legacy_id", legacy_id);
|
|
|
|
service->Touch("comments");
|
|
|
|
}
|
|
|
|
|
|
|
|
newLegacyCommentsCache[legacy_id] = id;
|
|
|
|
newCommentsCache[id] = service;
|
|
|
|
}
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
l_CommentsCache.swap(newCommentsCache);
|
|
|
|
l_LegacyCommentsCache.swap(newLegacyCommentsCache);
|
2013-01-30 14:28:13 +01:00
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
if (!l_CommentsExpireTimer) {
|
|
|
|
l_CommentsExpireTimer = boost::make_shared<Timer>();
|
|
|
|
l_CommentsExpireTimer->SetInterval(300);
|
|
|
|
l_CommentsExpireTimer->OnTimerExpired.connect(boost::bind(&Service::CommentsExpireTimerHandler));
|
|
|
|
l_CommentsExpireTimer->Start();
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
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-02-27 12:44:51 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
Touch("comments");
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 22:36:24 +01:00
|
|
|
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")) {
|
2013-02-09 01:16:43 +01:00
|
|
|
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
|
|
|
}
|