1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/commentstable.hpp"
4 #include "livestatus/hoststable.hpp"
5 #include "livestatus/servicestable.hpp"
6 #include "icinga/service.hpp"
7 #include "base/configtype.hpp"
8 #include "base/objectlock.hpp"
9 #include <boost/tuple/tuple.hpp>
11 using namespace icinga;
13 CommentsTable::CommentsTable()
18 void CommentsTable::AddColumns(Table *table, const String& prefix,
19 const Column::ObjectAccessor& objectAccessor)
21 table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
22 table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
23 table->AddColumn(prefix + "id", Column(&CommentsTable::IdAccessor, objectAccessor));
24 table->AddColumn(prefix + "entry_time", Column(&CommentsTable::EntryTimeAccessor, objectAccessor));
25 table->AddColumn(prefix + "type", Column(&CommentsTable::TypeAccessor, objectAccessor));
26 table->AddColumn(prefix + "is_service", Column(&CommentsTable::IsServiceAccessor, objectAccessor));
27 table->AddColumn(prefix + "persistent", Column(&Table::OneAccessor, objectAccessor));
28 table->AddColumn(prefix + "source", Column(&Table::OneAccessor, objectAccessor));
29 table->AddColumn(prefix + "entry_type", Column(&CommentsTable::EntryTypeAccessor, objectAccessor));
30 table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
31 table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
33 /* order is important - host w/o services must not be empty */
34 ServicesTable::AddColumns(table, "service_", std::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
35 HostsTable::AddColumns(table, "host_", std::bind(&CommentsTable::HostAccessor, _1, objectAccessor));
38 String CommentsTable::GetName() const
43 String CommentsTable::GetPrefix() const
48 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
50 for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
51 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
56 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
58 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
60 Checkable::Ptr checkable = comment->GetCheckable();
64 tie(host, service) = GetHostService(checkable);
69 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
71 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
73 Checkable::Ptr checkable = comment->GetCheckable();
77 tie(host, service) = GetHostService(checkable);
82 Value CommentsTable::AuthorAccessor(const Value& row)
84 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
89 return comment->GetAuthor();
92 Value CommentsTable::CommentAccessor(const Value& row)
94 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
99 return comment->GetText();
102 Value CommentsTable::IdAccessor(const Value& row)
104 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
109 return comment->GetLegacyId();
112 Value CommentsTable::EntryTimeAccessor(const Value& row)
114 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
119 return static_cast<int>(comment->GetEntryTime());
122 Value CommentsTable::TypeAccessor(const Value& row)
124 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
125 Checkable::Ptr checkable = comment->GetCheckable();
130 if (dynamic_pointer_cast<Host>(checkable))
136 Value CommentsTable::IsServiceAccessor(const Value& row)
138 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
139 Checkable::Ptr checkable = comment->GetCheckable();
144 return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
147 Value CommentsTable::EntryTypeAccessor(const Value& row)
149 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
154 return comment->GetEntryType();
157 Value CommentsTable::ExpiresAccessor(const Value& row)
159 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
164 return comment->GetExpireTime() != 0;
167 Value CommentsTable::ExpireTimeAccessor(const Value& row)
169 Comment::Ptr comment = static_cast<Comment::Ptr>(row);
174 return static_cast<int>(comment->GetExpireTime());