1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "livestatus/commentstable.hpp"
21 #include "livestatus/hoststable.hpp"
22 #include "livestatus/servicestable.hpp"
23 #include "icinga/service.hpp"
24 #include "base/dynamictype.hpp"
25 #include "base/objectlock.hpp"
26 #include <boost/tuple/tuple.hpp>
27 #include <boost/foreach.hpp>
29 using namespace icinga;
31 CommentsTable::CommentsTable(void)
36 void CommentsTable::AddColumns(Table *table, const String& prefix,
37 const Column::ObjectAccessor& objectAccessor)
39 table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
40 table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
41 table->AddColumn(prefix + "id", Column(&CommentsTable::IdAccessor, objectAccessor));
42 table->AddColumn(prefix + "entry_time", Column(&CommentsTable::EntryTimeAccessor, objectAccessor));
43 table->AddColumn(prefix + "type", Column(&CommentsTable::TypeAccessor, objectAccessor));
44 table->AddColumn(prefix + "is_service", Column(&CommentsTable::IsServiceAccessor, objectAccessor));
45 table->AddColumn(prefix + "persistent", Column(&Table::OneAccessor, objectAccessor));
46 table->AddColumn(prefix + "source", Column(&Table::OneAccessor, objectAccessor));
47 table->AddColumn(prefix + "entry_type", Column(&CommentsTable::EntryTypeAccessor, objectAccessor));
48 table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
49 table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
51 /* order is important - host w/o services must not be empty */
52 ServicesTable::AddColumns(table, "service_", boost::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
53 HostsTable::AddColumns(table, "host_", boost::bind(&CommentsTable::HostAccessor, _1, objectAccessor));
56 String CommentsTable::GetName(void) const
61 String CommentsTable::GetPrefix(void) const
66 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
68 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
69 Dictionary::Ptr comments = host->GetComments();
71 ObjectLock olock(comments);
75 BOOST_FOREACH(tie(id, comment), comments) {
76 if (Host::GetOwnerByCommentID(id) == host)
81 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
82 Dictionary::Ptr comments = service->GetComments();
84 ObjectLock olock(comments);
88 BOOST_FOREACH(tie(id, comment), comments) {
89 if (Service::GetOwnerByCommentID(id) == service)
95 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
97 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
99 Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
102 Service::Ptr service;
103 tie(host, service) = GetHostService(checkable);
108 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
110 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
112 Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
115 Service::Ptr service;
116 tie(host, service) = GetHostService(checkable);
121 Value CommentsTable::AuthorAccessor(const Value& row)
123 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
128 return comment->GetAuthor();
131 Value CommentsTable::CommentAccessor(const Value& row)
133 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
138 return comment->GetText();
141 Value CommentsTable::IdAccessor(const Value& row)
143 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
148 return comment->GetLegacyId();
151 Value CommentsTable::EntryTimeAccessor(const Value& row)
153 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
158 return static_cast<int>(comment->GetEntryTime());
161 Value CommentsTable::TypeAccessor(const Value& row)
163 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
164 Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
169 if (dynamic_pointer_cast<Host>(checkable))
175 Value CommentsTable::IsServiceAccessor(const Value& row)
177 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
178 Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
183 return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
186 Value CommentsTable::EntryTypeAccessor(const Value& row)
188 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
193 return comment->GetEntryType();
196 Value CommentsTable::ExpiresAccessor(const Value& row)
198 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
203 return comment->GetExpireTime() != 0;
206 Value CommentsTable::ExpireTimeAccessor(const Value& row)
208 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
213 return static_cast<int>(comment->GetExpireTime());