1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #include "icinga/service.hpp"
4 #include "remote/configobjectutility.hpp"
5 #include "base/configtype.hpp"
6 #include "base/objectlock.hpp"
7 #include "base/timer.hpp"
8 #include "base/utility.hpp"
9 #include "base/logger.hpp"
10 #include <utility>
11 
12 using namespace icinga;
13 
14 
RemoveAllComments()15 void Checkable::RemoveAllComments()
16 {
17 	for (const Comment::Ptr& comment : GetComments()) {
18 		Comment::RemoveComment(comment->GetName());
19 	}
20 }
21 
RemoveCommentsByType(int type,const String & removedBy)22 void Checkable::RemoveCommentsByType(int type, const String& removedBy)
23 {
24 	for (const Comment::Ptr& comment : GetComments()) {
25 		/* Do not remove persistent comments from an acknowledgement */
26 		if (comment->GetEntryType() == CommentAcknowledgement && comment->GetPersistent())
27 			continue;
28 
29 		if (comment->GetEntryType() == type) {
30 			{
31 				ObjectLock oLock (comment);
32 				comment->SetRemovedBy(removedBy);
33 			}
34 
35 			Comment::RemoveComment(comment->GetName());
36 		}
37 	}
38 }
39 
GetComments() const40 std::set<Comment::Ptr> Checkable::GetComments() const
41 {
42 	std::unique_lock<std::mutex> lock(m_CommentMutex);
43 	return m_Comments;
44 }
45 
GetLastComment() const46 Comment::Ptr Checkable::GetLastComment() const
47 {
48 	std::unique_lock<std::mutex> lock (m_CommentMutex);
49 	Comment::Ptr lastComment;
50 
51 	for (auto& comment : m_Comments) {
52 		if (!lastComment || comment->GetEntryTime() > lastComment->GetEntryTime()) {
53 			lastComment = comment;
54 		}
55 	}
56 
57 	return std::move(lastComment);
58 }
59 
RegisterComment(const Comment::Ptr & comment)60 void Checkable::RegisterComment(const Comment::Ptr& comment)
61 {
62 	std::unique_lock<std::mutex> lock(m_CommentMutex);
63 	m_Comments.insert(comment);
64 }
65 
UnregisterComment(const Comment::Ptr & comment)66 void Checkable::UnregisterComment(const Comment::Ptr& comment)
67 {
68 	std::unique_lock<std::mutex> lock(m_CommentMutex);
69 	m_Comments.erase(comment);
70 }
71