]> granicus.if.org Git - icinga2/blob - lib/livestatus/commentstable.cpp
Livestatus: Fix missing host downtimes/comments
[icinga2] / lib / livestatus / commentstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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/dynamictype.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, DynamicType::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                                 addRowFn(comment);
78                 }
79         }
80
81         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
82                 Dictionary::Ptr comments = service->GetComments();
83
84                 ObjectLock olock(comments);
85
86                 String id;
87                 Comment::Ptr comment;
88                 BOOST_FOREACH(tie(id, comment), comments) {
89                         if (Service::GetOwnerByCommentID(id) == service)
90                                 addRowFn(comment);
91                 }
92         }
93 }
94
95 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
96 {
97         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
98
99         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
100
101         Host::Ptr host;
102         Service::Ptr service;
103         tie(host, service) = GetHostService(checkable);
104
105         return host;
106 }
107
108 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
109 {
110         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
111
112         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
113
114         Host::Ptr host;
115         Service::Ptr service;
116         tie(host, service) = GetHostService(checkable);
117
118         return service;
119 }
120
121 Value CommentsTable::AuthorAccessor(const Value& row)
122 {
123         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
124
125         if (!comment)
126                 return Empty;
127
128         return comment->GetAuthor();
129 }
130
131 Value CommentsTable::CommentAccessor(const Value& row)
132 {
133         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
134
135         if (!comment)
136                 return Empty;
137
138         return comment->GetText();
139 }
140
141 Value CommentsTable::IdAccessor(const Value& row)
142 {
143         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
144
145         if (!comment)
146                 return Empty;
147
148         return comment->GetLegacyId();
149 }
150
151 Value CommentsTable::EntryTimeAccessor(const Value& row)
152 {
153         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
154
155         if (!comment)
156                 return Empty;
157
158         return static_cast<int>(comment->GetEntryTime());
159 }
160
161 Value CommentsTable::TypeAccessor(const Value& row)
162 {
163         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
164         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
165
166         if (!checkable)
167                 return Empty;
168
169         if (dynamic_pointer_cast<Host>(checkable))
170                 return 1;
171         else
172                 return 2;
173 }
174
175 Value CommentsTable::IsServiceAccessor(const Value& row)
176 {
177         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
178         Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId());
179
180         if (!checkable)
181                 return Empty;
182
183         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
184 }
185
186 Value CommentsTable::EntryTypeAccessor(const Value& row)
187 {
188         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
189
190         if (!comment)
191                 return Empty;
192
193         return comment->GetEntryType();
194 }
195
196 Value CommentsTable::ExpiresAccessor(const Value& row)
197 {
198         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
199
200         if (!comment)
201                 return Empty;
202
203         return comment->GetExpireTime() != 0;
204 }
205
206 Value CommentsTable::ExpireTimeAccessor(const Value& row)
207 {
208         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
209
210         if (!comment)
211                 return Empty;
212
213         return static_cast<int>(comment->GetExpireTime());
214 }