2013-01-29 16:29:09 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-01-29 16:29:09 +01:00
|
|
|
* *
|
|
|
|
* 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/foreach.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 Timer::Ptr l_CommentsExpireTimer;
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
boost::signals2::signal<void (const Service::Ptr&, const Comment::Ptr&, const String&)> Service::OnCommentAdded;
|
|
|
|
boost::signals2::signal<void (const Service::Ptr&, const Comment::Ptr&, const String&)> Service::OnCommentRemoved;
|
2013-08-07 15:39:09 +02: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 01:16:43 +01:00
|
|
|
String Service::AddComment(CommentType entryType, const String& author,
|
2013-08-28 14:59:41 +02:00
|
|
|
const String& text, double expireTime, const String& id, const String& authority)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-08-28 14:59:41 +02:00
|
|
|
String uid;
|
|
|
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
uid = Utility::NewUniqueID();
|
|
|
|
else
|
|
|
|
uid = id;
|
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
Comment::Ptr comment = make_shared<Comment>();
|
|
|
|
comment->SetId(uid);;
|
|
|
|
comment->SetEntryTime(Utility::GetTime());
|
|
|
|
comment->SetEntryType(entryType);
|
|
|
|
comment->SetAuthor(author);
|
|
|
|
comment->SetText(text);
|
|
|
|
comment->SetExpireTime(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
|
|
|
}
|
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
comment->SetLegacyId(legacy_id);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
GetComments()->Set(uid, comment);
|
2013-01-29 16:29:09 +01:00
|
|
|
|
2013-07-11 08:58:11 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
2013-08-28 14:59:41 +02:00
|
|
|
l_LegacyCommentsCache[legacy_id] = uid;
|
|
|
|
l_CommentsCache[uid] = GetSelf();
|
2013-07-11 08:58:11 +02:00
|
|
|
}
|
|
|
|
|
2013-11-10 16:53:57 +01:00
|
|
|
OnCommentAdded(GetSelf(), comment, authority);
|
2013-08-07 15:39:09 +02:00
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
return uid;
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-02-09 01:16:43 +01:00
|
|
|
void Service::RemoveAllComments(void)
|
2013-01-29 16:29:09 +01:00
|
|
|
{
|
2013-08-28 14:59:41 +02:00
|
|
|
std::vector<String> ids;
|
2013-10-26 09:41:45 +02:00
|
|
|
Dictionary::Ptr comments = GetComments();
|
2013-08-28 14:59:41 +02:00
|
|
|
|
2014-03-07 09:53:21 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(comments);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
|
|
|
|
ids.push_back(kv.first);
|
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const String& id, ids) {
|
2013-08-28 14:59:41 +02:00
|
|
|
RemoveComment(id);
|
|
|
|
}
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
void Service::RemoveComment(const String& id, const String& authority)
|
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
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
ObjectLock olock(owner);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
Comment::Ptr comment = comments->Get(id);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
if (!comment)
|
|
|
|
return;
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
int legacy_id = comment->GetLegacyId();
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
comments->Remove(id);
|
2013-08-07 15:39:09 +02:00
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
|
|
|
l_LegacyCommentsCache.erase(legacy_id);
|
|
|
|
l_CommentsCache.erase(id);
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
2013-08-28 14:59:41 +02:00
|
|
|
|
2013-11-10 16:53:57 +01:00
|
|
|
OnCommentRemoved(owner, comment, authority);
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
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-11-09 21:19:52 +01:00
|
|
|
Comment::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-11-09 21:19:52 +01:00
|
|
|
return Comment::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
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
return Comment::Ptr();
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Service::AddCommentsToCache(void)
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
2013-11-13 14:56:31 +01:00
|
|
|
#ifdef _DEBUG
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "icinga", "Updating Service comments cache.");
|
2013-11-13 14:56:31 +01:00
|
|
|
#endif /* _DEBUG */
|
2013-03-06 13:01:51 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Dictionary::Ptr comments = GetComments();
|
2013-06-19 10:42:28 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
ObjectLock olock(comments);
|
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-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
|
|
|
|
Comment::Ptr comment = kv.second;
|
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
int legacy_id = comment->GetLegacyId();
|
2013-08-20 11:06:04 +02:00
|
|
|
|
|
|
|
if (legacy_id >= l_NextCommentID)
|
|
|
|
l_NextCommentID = legacy_id + 1;
|
2013-01-30 14:28:13 +01:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
l_LegacyCommentsCache[legacy_id] = kv.first;
|
|
|
|
l_CommentsCache[kv.first] = GetSelf();
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-19 10:57:07 +02:00
|
|
|
void Service::RemoveCommentsByType(int type)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr comments = GetComments();
|
|
|
|
|
|
|
|
std::vector<String> removedComments;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(comments);
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
|
|
|
|
Comment::Ptr comment = kv.second;
|
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
if (comment->GetEntryType() == type)
|
2013-11-30 17:42:50 +01:00
|
|
|
removedComments.push_back(kv.first);
|
2013-06-19 10:57:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
BOOST_FOREACH(const String& id, removedComments) {
|
|
|
|
RemoveComment(id);
|
2013-06-19 10:57:07 +02: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
|
|
|
|
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-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
|
|
|
|
Comment::Ptr comment = kv.second;
|
|
|
|
|
2013-11-09 22:16:53 +01:00
|
|
|
if (comment->IsExpired())
|
2013-11-30 17:42:50 +01:00
|
|
|
expiredComments.push_back(kv.first);
|
2013-03-04 15:52:42 +01:00
|
|
|
}
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
|
|
|
|
2013-08-28 14:59:41 +02:00
|
|
|
BOOST_FOREACH(const String& id, expiredComments) {
|
|
|
|
RemoveComment(id);
|
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-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
|
2013-02-09 01:16:43 +01:00
|
|
|
service->RemoveExpiredComments();
|
2013-01-30 14:28:13 +01:00
|
|
|
}
|
2013-01-29 16:29:09 +01:00
|
|
|
}
|