2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-11-09 21:19:52 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/comment.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "icinga/comment-ti.cpp"
|
2015-08-20 17:18:48 +02:00
|
|
|
#include "icinga/host.hpp"
|
|
|
|
#include "remote/configobjectutility.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2015-08-20 17:18:48 +02:00
|
|
|
#include "base/timer.hpp"
|
2017-11-21 14:07:44 +01:00
|
|
|
#include <boost/thread/once.hpp>
|
2013-11-09 21:19:52 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
static int l_NextCommentID = 1;
|
|
|
|
static boost::mutex l_CommentMutex;
|
|
|
|
static std::map<int, String> l_LegacyCommentsCache;
|
|
|
|
static Timer::Ptr l_CommentsExpireTimer;
|
|
|
|
|
|
|
|
boost::signals2::signal<void (const Comment::Ptr&)> Comment::OnCommentAdded;
|
|
|
|
boost::signals2::signal<void (const Comment::Ptr&)> Comment::OnCommentRemoved;
|
|
|
|
|
2013-11-09 21:19:52 +01:00
|
|
|
REGISTER_TYPE(Comment);
|
2013-11-09 22:16:53 +01:00
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
String CommentNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
|
|
|
|
{
|
|
|
|
Comment::Ptr comment = dynamic_pointer_cast<Comment>(context);
|
|
|
|
|
|
|
|
if (!comment)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
String name = comment->GetHostName();
|
|
|
|
|
|
|
|
if (!comment->GetServiceName().IsEmpty())
|
|
|
|
name += "!" + comment->GetServiceName();
|
|
|
|
|
|
|
|
name += "!" + shortName;
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr CommentNameComposer::ParseName(const String& name) const
|
|
|
|
{
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = name.Split("!");
|
2015-08-20 17:18:48 +02:00
|
|
|
|
|
|
|
if (tokens.size() < 2)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid Comment name."));
|
|
|
|
|
|
|
|
Dictionary::Ptr result = new Dictionary();
|
|
|
|
result->Set("host_name", tokens[0]);
|
|
|
|
|
|
|
|
if (tokens.size() > 2) {
|
|
|
|
result->Set("service_name", tokens[1]);
|
|
|
|
result->Set("name", tokens[2]);
|
|
|
|
} else {
|
|
|
|
result->Set("name", tokens[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Comment::OnAllConfigLoaded()
|
2015-08-20 17:18:48 +02:00
|
|
|
{
|
|
|
|
ConfigObject::OnAllConfigLoaded();
|
|
|
|
|
|
|
|
Host::Ptr host = Host::GetByName(GetHostName());
|
|
|
|
|
|
|
|
if (GetServiceName().IsEmpty())
|
|
|
|
m_Checkable = host;
|
|
|
|
else
|
|
|
|
m_Checkable = host->GetServiceByShortName(GetServiceName());
|
|
|
|
|
|
|
|
if (!m_Checkable)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Comment '" + GetName() + "' references a host/service which doesn't exist.", GetDebugInfo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Comment::Start(bool runtimeCreated)
|
|
|
|
{
|
|
|
|
ObjectImpl<Comment>::Start(runtimeCreated);
|
|
|
|
|
2017-11-21 14:07:44 +01:00
|
|
|
static boost::once_flag once = BOOST_ONCE_INIT;
|
|
|
|
|
2017-12-19 15:58:58 +01:00
|
|
|
boost::call_once(once, [this]() {
|
2017-11-21 14:07:44 +01:00
|
|
|
l_CommentsExpireTimer = new Timer();
|
|
|
|
l_CommentsExpireTimer->SetInterval(60);
|
|
|
|
l_CommentsExpireTimer->OnTimerExpired.connect(std::bind(&Comment::CommentsExpireTimerHandler));
|
|
|
|
l_CommentsExpireTimer->Start();
|
|
|
|
});
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
SetLegacyId(l_NextCommentID);
|
|
|
|
l_LegacyCommentsCache[l_NextCommentID] = GetName();
|
|
|
|
l_NextCommentID++;
|
2015-08-20 17:18:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GetCheckable()->RegisterComment(this);
|
|
|
|
|
|
|
|
if (runtimeCreated)
|
|
|
|
OnCommentAdded(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Comment::Stop(bool runtimeRemoved)
|
|
|
|
{
|
|
|
|
GetCheckable()->UnregisterComment(this);
|
|
|
|
|
|
|
|
if (runtimeRemoved)
|
|
|
|
OnCommentRemoved(this);
|
|
|
|
|
|
|
|
ObjectImpl<Comment>::Stop(runtimeRemoved);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Checkable::Ptr Comment::GetCheckable() const
|
2015-08-20 17:18:48 +02:00
|
|
|
{
|
2015-10-29 08:06:03 +01:00
|
|
|
return static_pointer_cast<Checkable>(m_Checkable);
|
2015-08-20 17:18:48 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool Comment::IsExpired() const
|
2013-11-09 22:16:53 +01:00
|
|
|
{
|
|
|
|
double expire_time = GetExpireTime();
|
|
|
|
|
|
|
|
return (expire_time != 0 && expire_time < Utility::GetTime());
|
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int Comment::GetNextCommentID()
|
2015-08-20 17:18:48 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
|
|
|
|
|
|
|
return l_NextCommentID;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Comment::AddComment(const Checkable::Ptr& checkable, CommentType entryType, const String& author,
|
2017-12-19 15:50:05 +01:00
|
|
|
const String& text, bool persistent, double expireTime, const String& id, const MessageOrigin::Ptr& origin)
|
2015-08-20 17:18:48 +02:00
|
|
|
{
|
|
|
|
String fullName;
|
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
if (id.IsEmpty())
|
|
|
|
fullName = checkable->GetName() + "!" + Utility::NewUniqueID();
|
|
|
|
else
|
|
|
|
fullName = id;
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
Dictionary::Ptr attrs = new Dictionary();
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
attrs->Set("author", author);
|
|
|
|
attrs->Set("text", text);
|
2017-01-25 21:21:22 +01:00
|
|
|
attrs->Set("persistent", persistent);
|
2015-10-29 08:06:03 +01:00
|
|
|
attrs->Set("expire_time", expireTime);
|
|
|
|
attrs->Set("entry_type", entryType);
|
2016-06-15 11:28:20 +02:00
|
|
|
attrs->Set("entry_time", Utility::GetTime());
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
attrs->Set("host_name", host->GetName());
|
|
|
|
if (service)
|
|
|
|
attrs->Set("service_name", service->GetShortName());
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2016-03-29 12:09:51 +02:00
|
|
|
String zone = checkable->GetZoneName();
|
|
|
|
|
|
|
|
if (!zone.IsEmpty())
|
|
|
|
attrs->Set("zone", zone);
|
|
|
|
|
2017-11-30 08:36:35 +01:00
|
|
|
String config = ConfigObjectUtility::CreateObjectConfig(Comment::TypeInstance, fullName, true, nullptr, attrs);
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
Array::Ptr errors = new Array();
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
if (!ConfigObjectUtility::CreateObject(Comment::TypeInstance, fullName, config, errors, nullptr)) {
|
2015-10-29 08:06:03 +01:00
|
|
|
ObjectLock olock(errors);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& error : errors) {
|
2015-10-29 08:06:03 +01:00
|
|
|
Log(LogCritical, "Comment", error);
|
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment."));
|
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
|
|
|
|
Comment::Ptr comment = Comment::GetByName(fullName);
|
|
|
|
|
2016-05-11 16:12:09 +02:00
|
|
|
if (!comment)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment."));
|
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
Log(LogNotice, "Comment")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Added comment '" << comment->GetName() << "'.";
|
2015-08-20 17:18:48 +02:00
|
|
|
|
|
|
|
return fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Comment::RemoveComment(const String& id, const MessageOrigin::Ptr& origin)
|
|
|
|
{
|
|
|
|
Comment::Ptr comment = Comment::GetByName(id);
|
|
|
|
|
2017-03-27 11:01:14 +02:00
|
|
|
if (!comment || comment->GetPackage() != "_api")
|
2015-08-20 17:18:48 +02:00
|
|
|
return;
|
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
Log(LogNotice, "Comment")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Removed comment '" << comment->GetName() << "' from object '" << comment->GetCheckable()->GetName() << "'.";
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
Array::Ptr errors = new Array();
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2018-04-06 12:26:49 +02:00
|
|
|
if (!ConfigObjectUtility::DeleteObject(comment, false, errors, nullptr)) {
|
2015-10-29 08:06:03 +01:00
|
|
|
ObjectLock olock(errors);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& error : errors) {
|
2015-10-29 08:06:03 +01:00
|
|
|
Log(LogCritical, "Comment", error);
|
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not remove comment."));
|
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String Comment::GetCommentIDFromLegacyID(int id)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_CommentMutex);
|
|
|
|
|
2016-08-27 19:56:12 +02:00
|
|
|
auto it = l_LegacyCommentsCache.find(id);
|
2015-08-20 17:18:48 +02:00
|
|
|
|
|
|
|
if (it == l_LegacyCommentsCache.end())
|
|
|
|
return Empty;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Comment::CommentsExpireTimerHandler()
|
2015-08-20 17:18:48 +02:00
|
|
|
{
|
2015-10-29 08:06:03 +01:00
|
|
|
std::vector<Comment::Ptr> comments;
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
|
2015-10-29 08:06:03 +01:00
|
|
|
comments.push_back(comment);
|
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Comment::Ptr& comment : comments) {
|
2017-01-25 21:21:22 +01:00
|
|
|
/* Only remove comments which are activated after daemon start. */
|
|
|
|
if (comment->IsActive() && comment->IsExpired()) {
|
|
|
|
/* Do not remove persistent comments from an acknowledgement */
|
|
|
|
if (comment->GetEntryType() == CommentAcknowledgement && comment->GetPersistent())
|
|
|
|
continue;
|
|
|
|
|
2015-10-29 08:06:03 +01:00
|
|
|
RemoveComment(comment->GetName());
|
2017-01-25 21:21:22 +01:00
|
|
|
}
|
2015-10-29 08:06:03 +01:00
|
|
|
}
|
2015-08-20 17:18:48 +02:00
|
|
|
}
|