1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
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 
10 using namespace icinga;
11 
CommentsTable()12 CommentsTable::CommentsTable()
13 {
14 	AddColumns(this);
15 }
16 
AddColumns(Table * table,const String & prefix,const Column::ObjectAccessor & objectAccessor)17 void CommentsTable::AddColumns(Table *table, const String& prefix,
18 	const Column::ObjectAccessor& objectAccessor)
19 {
20 	table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
21 	table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
22 	table->AddColumn(prefix + "id", Column(&CommentsTable::IdAccessor, objectAccessor));
23 	table->AddColumn(prefix + "entry_time", Column(&CommentsTable::EntryTimeAccessor, objectAccessor));
24 	table->AddColumn(prefix + "type", Column(&CommentsTable::TypeAccessor, objectAccessor));
25 	table->AddColumn(prefix + "is_service", Column(&CommentsTable::IsServiceAccessor, objectAccessor));
26 	table->AddColumn(prefix + "persistent", Column(&Table::OneAccessor, objectAccessor));
27 	table->AddColumn(prefix + "source", Column(&Table::OneAccessor, objectAccessor));
28 	table->AddColumn(prefix + "entry_type", Column(&CommentsTable::EntryTypeAccessor, objectAccessor));
29 	table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
30 	table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
31 
32 	/* order is important - host w/o services must not be empty */
33 	ServicesTable::AddColumns(table, "service_", [objectAccessor](const Value& row, LivestatusGroupByType, const Object::Ptr&) -> Value {
34 		return ServiceAccessor(row, objectAccessor);
35 	});
36 	HostsTable::AddColumns(table, "host_", [objectAccessor](const Value& row, LivestatusGroupByType, const Object::Ptr&) -> Value {
37 		return HostAccessor(row, objectAccessor);
38 	});
39 }
40 
GetName() const41 String CommentsTable::GetName() const
42 {
43 	return "comments";
44 }
45 
GetPrefix() const46 String CommentsTable::GetPrefix() const
47 {
48 	return "comment";
49 }
50 
FetchRows(const AddRowFunction & addRowFn)51 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
52 {
53 	for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
54 		if (!addRowFn(comment, LivestatusGroupByNone, Empty))
55 			return;
56 	}
57 }
58 
HostAccessor(const Value & row,const Column::ObjectAccessor &)59 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
60 {
61 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
62 
63 	Checkable::Ptr checkable = comment->GetCheckable();
64 
65 	Host::Ptr host;
66 	Service::Ptr service;
67 	tie(host, service) = GetHostService(checkable);
68 
69 	return host;
70 }
71 
ServiceAccessor(const Value & row,const Column::ObjectAccessor &)72 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
73 {
74 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
75 
76 	Checkable::Ptr checkable = comment->GetCheckable();
77 
78 	Host::Ptr host;
79 	Service::Ptr service;
80 	tie(host, service) = GetHostService(checkable);
81 
82 	return service;
83 }
84 
AuthorAccessor(const Value & row)85 Value CommentsTable::AuthorAccessor(const Value& row)
86 {
87 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
88 
89 	if (!comment)
90 		return Empty;
91 
92 	return comment->GetAuthor();
93 }
94 
CommentAccessor(const Value & row)95 Value CommentsTable::CommentAccessor(const Value& row)
96 {
97 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
98 
99 	if (!comment)
100 		return Empty;
101 
102 	return comment->GetText();
103 }
104 
IdAccessor(const Value & row)105 Value CommentsTable::IdAccessor(const Value& row)
106 {
107 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
108 
109 	if (!comment)
110 		return Empty;
111 
112 	return comment->GetLegacyId();
113 }
114 
EntryTimeAccessor(const Value & row)115 Value CommentsTable::EntryTimeAccessor(const Value& row)
116 {
117 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
118 
119 	if (!comment)
120 		return Empty;
121 
122 	return static_cast<int>(comment->GetEntryTime());
123 }
124 
TypeAccessor(const Value & row)125 Value CommentsTable::TypeAccessor(const Value& row)
126 {
127 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
128 	Checkable::Ptr checkable = comment->GetCheckable();
129 
130 	if (!checkable)
131 		return Empty;
132 
133 	if (dynamic_pointer_cast<Host>(checkable))
134 		return 1;
135 	else
136 		return 2;
137 }
138 
IsServiceAccessor(const Value & row)139 Value CommentsTable::IsServiceAccessor(const Value& row)
140 {
141 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
142 	Checkable::Ptr checkable = comment->GetCheckable();
143 
144 	if (!checkable)
145 		return Empty;
146 
147 	return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
148 }
149 
EntryTypeAccessor(const Value & row)150 Value CommentsTable::EntryTypeAccessor(const Value& row)
151 {
152 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
153 
154 	if (!comment)
155 		return Empty;
156 
157 	return comment->GetEntryType();
158 }
159 
ExpiresAccessor(const Value & row)160 Value CommentsTable::ExpiresAccessor(const Value& row)
161 {
162 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
163 
164 	if (!comment)
165 		return Empty;
166 
167 	return comment->GetExpireTime() != 0;
168 }
169 
ExpireTimeAccessor(const Value & row)170 Value CommentsTable::ExpireTimeAccessor(const Value& row)
171 {
172 	Comment::Ptr comment = static_cast<Comment::Ptr>(row);
173 
174 	if (!comment)
175 		return Empty;
176 
177 	return static_cast<int>(comment->GetExpireTime());
178 }
179