]> granicus.if.org Git - icinga2/blob - lib/livestatus/commentstable.cpp
Rename DynamicObject/DynamicType to ConfigObject/ConfigType
[icinga2] / lib / livestatus / commentstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
27 #include <boost/foreach.hpp>
28
29 using namespace icinga;
30
31 CommentsTable::CommentsTable(void)
32 {
33         AddColumns(this);
34 }
35
36 void CommentsTable::AddColumns(Table *table, const String& prefix,
37     const Column::ObjectAccessor& objectAccessor)
38 {
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));
50
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));
54 }
55
56 String CommentsTable::GetName(void) const
57 {
58         return "comments";
59 }
60
61 String CommentsTable::GetPrefix(void) const
62 {
63         return "comment";
64 }
65
66 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
67 {
68         BOOST_FOREACH(const Host::Ptr& host, ConfigType::GetObjectsByType<Host>()) {
69                 Dictionary::Ptr comments = host->GetComments();
70
71                 ObjectLock olock(comments);
72
73                 String id;
74                 Comment::Ptr comment;
75                 BOOST_FOREACH(tie(id, comment), comments) {
76                         if (Host::GetOwnerByCommentID(id) == host) {
77                                 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
78                                         return;
79                         }
80                 }
81         }
82
83         BOOST_FOREACH(const Service::Ptr& service, ConfigType::GetObjectsByType<Service>()) {
84                 Dictionary::Ptr comments = service->GetComments();
85
86                 ObjectLock olock(comments);
87
88                 String id;
89                 Comment::Ptr comment;
90                 BOOST_FOREACH(tie(id, comment), comments) {
91                         if (Service::GetOwnerByCommentID(id) == service) {
92                                 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
93                                         return;
94                         }
95                 }
96         }
97 }
98
99 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
100 {
101         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
102
103         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
104
105         Host::Ptr host;
106         Service::Ptr service;
107         tie(host, service) = GetHostService(checkable);
108
109         return host;
110 }
111
112 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
113 {
114         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
115
116         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
117
118         Host::Ptr host;
119         Service::Ptr service;
120         tie(host, service) = GetHostService(checkable);
121
122         return service;
123 }
124
125 Value CommentsTable::AuthorAccessor(const Value& row)
126 {
127         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
128
129         if (!comment)
130                 return Empty;
131
132         return comment->GetAuthor();
133 }
134
135 Value CommentsTable::CommentAccessor(const Value& row)
136 {
137         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
138
139         if (!comment)
140                 return Empty;
141
142         return comment->GetText();
143 }
144
145 Value CommentsTable::IdAccessor(const Value& row)
146 {
147         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
148
149         if (!comment)
150                 return Empty;
151
152         return comment->GetLegacyId();
153 }
154
155 Value CommentsTable::EntryTimeAccessor(const Value& row)
156 {
157         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
158
159         if (!comment)
160                 return Empty;
161
162         return static_cast<int>(comment->GetEntryTime());
163 }
164
165 Value CommentsTable::TypeAccessor(const Value& row)
166 {
167         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
168         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
169
170         if (!checkable)
171                 return Empty;
172
173         if (dynamic_pointer_cast<Host>(checkable))
174                 return 1;
175         else
176                 return 2;
177 }
178
179 Value CommentsTable::IsServiceAccessor(const Value& row)
180 {
181         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
182         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
183
184         if (!checkable)
185                 return Empty;
186
187         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
188 }
189
190 Value CommentsTable::EntryTypeAccessor(const Value& row)
191 {
192         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
193
194         if (!comment)
195                 return Empty;
196
197         return comment->GetEntryType();
198 }
199
200 Value CommentsTable::ExpiresAccessor(const Value& row)
201 {
202         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
203
204         if (!comment)
205                 return Empty;
206
207         return comment->GetExpireTime() != 0;
208 }
209
210 Value CommentsTable::ExpireTimeAccessor(const Value& row)
211 {
212         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
213
214         if (!comment)
215                 return Empty;
216
217         return static_cast<int>(comment->GetExpireTime());
218 }