]> granicus.if.org Git - icinga2/blob - lib/icinga/comment.cpp
Fix spelling errors.
[icinga2] / lib / icinga / comment.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "icinga/comment.hpp"
4 #include "icinga/comment-ti.cpp"
5 #include "icinga/host.hpp"
6 #include "remote/configobjectutility.hpp"
7 #include "base/utility.hpp"
8 #include "base/configtype.hpp"
9 #include "base/timer.hpp"
10 #include <boost/thread/once.hpp>
11
12 using namespace icinga;
13
14 static int l_NextCommentID = 1;
15 static boost::mutex l_CommentMutex;
16 static std::map<int, String> l_LegacyCommentsCache;
17 static Timer::Ptr l_CommentsExpireTimer;
18
19 boost::signals2::signal<void (const Comment::Ptr&)> Comment::OnCommentAdded;
20 boost::signals2::signal<void (const Comment::Ptr&)> Comment::OnCommentRemoved;
21
22 REGISTER_TYPE(Comment);
23
24 String CommentNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const
25 {
26         Comment::Ptr comment = dynamic_pointer_cast<Comment>(context);
27
28         if (!comment)
29                 return "";
30
31         String name = comment->GetHostName();
32
33         if (!comment->GetServiceName().IsEmpty())
34                 name += "!" + comment->GetServiceName();
35
36         name += "!" + shortName;
37
38         return name;
39 }
40
41 Dictionary::Ptr CommentNameComposer::ParseName(const String& name) const
42 {
43         std::vector<String> tokens = name.Split("!");
44
45         if (tokens.size() < 2)
46                 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid Comment name."));
47
48         Dictionary::Ptr result = new Dictionary();
49         result->Set("host_name", tokens[0]);
50
51         if (tokens.size() > 2) {
52                 result->Set("service_name", tokens[1]);
53                 result->Set("name", tokens[2]);
54         } else {
55                 result->Set("name", tokens[1]);
56         }
57
58         return result;
59 }
60
61 void Comment::OnAllConfigLoaded()
62 {
63         ConfigObject::OnAllConfigLoaded();
64
65         Host::Ptr host = Host::GetByName(GetHostName());
66
67         if (GetServiceName().IsEmpty())
68                 m_Checkable = host;
69         else
70                 m_Checkable = host->GetServiceByShortName(GetServiceName());
71
72         if (!m_Checkable)
73                 BOOST_THROW_EXCEPTION(ScriptError("Comment '" + GetName() + "' references a host/service which doesn't exist.", GetDebugInfo()));
74 }
75
76 void Comment::Start(bool runtimeCreated)
77 {
78         ObjectImpl<Comment>::Start(runtimeCreated);
79
80         static boost::once_flag once = BOOST_ONCE_INIT;
81
82         boost::call_once(once, [this]() {
83                 l_CommentsExpireTimer = new Timer();
84                 l_CommentsExpireTimer->SetInterval(60);
85                 l_CommentsExpireTimer->OnTimerExpired.connect(std::bind(&Comment::CommentsExpireTimerHandler));
86                 l_CommentsExpireTimer->Start();
87         });
88
89         {
90                 boost::mutex::scoped_lock lock(l_CommentMutex);
91
92                 SetLegacyId(l_NextCommentID);
93                 l_LegacyCommentsCache[l_NextCommentID] = GetName();
94                 l_NextCommentID++;
95         }
96
97         GetCheckable()->RegisterComment(this);
98
99         if (runtimeCreated)
100                 OnCommentAdded(this);
101 }
102
103 void Comment::Stop(bool runtimeRemoved)
104 {
105         GetCheckable()->UnregisterComment(this);
106
107         if (runtimeRemoved)
108                 OnCommentRemoved(this);
109
110         ObjectImpl<Comment>::Stop(runtimeRemoved);
111 }
112
113 Checkable::Ptr Comment::GetCheckable() const
114 {
115         return static_pointer_cast<Checkable>(m_Checkable);
116 }
117
118 bool Comment::IsExpired() const
119 {
120         double expire_time = GetExpireTime();
121
122         return (expire_time != 0 && expire_time < Utility::GetTime());
123 }
124
125 int Comment::GetNextCommentID()
126 {
127         boost::mutex::scoped_lock lock(l_CommentMutex);
128
129         return l_NextCommentID;
130 }
131
132 String Comment::AddComment(const Checkable::Ptr& checkable, CommentType entryType, const String& author,
133         const String& text, bool persistent, double expireTime, const String& id, const MessageOrigin::Ptr& origin)
134 {
135         String fullName;
136
137         if (id.IsEmpty())
138                 fullName = checkable->GetName() + "!" + Utility::NewUniqueID();
139         else
140                 fullName = id;
141
142         Dictionary::Ptr attrs = new Dictionary();
143
144         attrs->Set("author", author);
145         attrs->Set("text", text);
146         attrs->Set("persistent", persistent);
147         attrs->Set("expire_time", expireTime);
148         attrs->Set("entry_type", entryType);
149         attrs->Set("entry_time", Utility::GetTime());
150
151         Host::Ptr host;
152         Service::Ptr service;
153         tie(host, service) = GetHostService(checkable);
154
155         attrs->Set("host_name", host->GetName());
156         if (service)
157                 attrs->Set("service_name", service->GetShortName());
158
159         String zone = checkable->GetZoneName();
160
161         if (!zone.IsEmpty())
162                 attrs->Set("zone", zone);
163
164         String config = ConfigObjectUtility::CreateObjectConfig(Comment::TypeInstance, fullName, true, nullptr, attrs);
165
166         Array::Ptr errors = new Array();
167
168         if (!ConfigObjectUtility::CreateObject(Comment::TypeInstance, fullName, config, errors, nullptr)) {
169                 ObjectLock olock(errors);
170                 for (const String& error : errors) {
171                         Log(LogCritical, "Comment", error);
172                 }
173
174                 BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment."));
175         }
176
177         Comment::Ptr comment = Comment::GetByName(fullName);
178
179         if (!comment)
180                 BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment."));
181
182         Log(LogNotice, "Comment")
183                 << "Added comment '" << comment->GetName() << "'.";
184
185         return fullName;
186 }
187
188 void Comment::RemoveComment(const String& id, const MessageOrigin::Ptr& origin)
189 {
190         Comment::Ptr comment = Comment::GetByName(id);
191
192         if (!comment || comment->GetPackage() != "_api")
193                 return;
194
195         Log(LogNotice, "Comment")
196                 << "Removed comment '" << comment->GetName() << "' from object '" << comment->GetCheckable()->GetName() << "'.";
197
198         Array::Ptr errors = new Array();
199
200         if (!ConfigObjectUtility::DeleteObject(comment, false, errors, nullptr)) {
201                 ObjectLock olock(errors);
202                 for (const String& error : errors) {
203                         Log(LogCritical, "Comment", error);
204                 }
205
206                 BOOST_THROW_EXCEPTION(std::runtime_error("Could not remove comment."));
207         }
208 }
209
210 String Comment::GetCommentIDFromLegacyID(int id)
211 {
212         boost::mutex::scoped_lock lock(l_CommentMutex);
213
214         auto it = l_LegacyCommentsCache.find(id);
215
216         if (it == l_LegacyCommentsCache.end())
217                 return Empty;
218
219         return it->second;
220 }
221
222 void Comment::CommentsExpireTimerHandler()
223 {
224         std::vector<Comment::Ptr> comments;
225
226         for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
227                 comments.push_back(comment);
228         }
229
230         for (const Comment::Ptr& comment : comments) {
231                 /* Only remove comments which are activated after daemon start. */
232                 if (comment->IsActive() && comment->IsExpired()) {
233                         /* Do not remove persistent comments from an acknowledgement */
234                         if (comment->GetEntryType() == CommentAcknowledgement && comment->GetPersistent())
235                                 continue;
236
237                         RemoveComment(comment->GetName());
238                 }
239         }
240 }