1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include <boost/tuple/tuple.hpp>
28 using namespace icinga;
30 CommentsTable::CommentsTable()
35 void CommentsTable::AddColumns(Table *table, const String& prefix,
36 const Column::ObjectAccessor& objectAccessor)
38 table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
39 table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
40 table->AddColumn(prefix + "id", Column(&CommentsTable::IdAccessor, objectAccessor));
41 table->AddColumn(prefix + "entry_time", Column(&CommentsTable::EntryTimeAccessor, objectAccessor));
42 table->AddColumn(prefix + "type", Column(&CommentsTable::TypeAccessor, objectAccessor));
43 table->AddColumn(prefix + "is_service", Column(&CommentsTable::IsServiceAccessor, objectAccessor));
44 table->AddColumn(prefix + "persistent", Column(&Table::OneAccessor, objectAccessor));
45 table->AddColumn(prefix + "source", Column(&Table::OneAccessor, objectAccessor));
46 table->AddColumn(prefix + "entry_type", Column(&CommentsTable::EntryTypeAccessor, objectAccessor));
47 table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
48 table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
50 /* order is important - host w/o services must not be empty */
51 ServicesTable::AddColumns(table, "service_", std::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
52 HostsTable::AddColumns(table, "host_", std::bind(&CommentsTable::HostAccessor, _1, objectAccessor));
55 String CommentsTable::GetName() const
60 String CommentsTable::GetPrefix() const
65 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
67 for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
68 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
73 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
75 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
77 Checkable::Ptr checkable = comment->GetCheckable();
81 tie(host, service) = GetHostService(checkable);
86 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
88 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
90 Checkable::Ptr checkable = comment->GetCheckable();
94 tie(host, service) = GetHostService(checkable);
99 Value CommentsTable::AuthorAccessor(const Value& row)
101 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
106 return comment->GetAuthor();
109 Value CommentsTable::CommentAccessor(const Value& row)
111 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
116 return comment->GetText();
119 Value CommentsTable::IdAccessor(const Value& row)
121 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
126 return comment->GetLegacyId();
129 Value CommentsTable::EntryTimeAccessor(const Value& row)
131 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
136 return static_cast<int>(comment->GetEntryTime());
139 Value CommentsTable::TypeAccessor(const Value& row)
141 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
142 Checkable::Ptr checkable = comment->GetCheckable();
147 if (dynamic_pointer_cast<Host>(checkable))
153 Value CommentsTable::IsServiceAccessor(const Value& row)
155 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
156 Checkable::Ptr checkable = comment->GetCheckable();
161 return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
164 Value CommentsTable::EntryTypeAccessor(const Value& row)
166 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
171 return comment->GetEntryType();
174 Value CommentsTable::ExpiresAccessor(const Value& row)
176 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
181 return comment->GetExpireTime() != 0;
184 Value CommentsTable::ExpireTimeAccessor(const Value& row)
186 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
191 return static_cast<int>(comment->GetExpireTime());