]> granicus.if.org Git - icinga2/blob - lib/livestatus/commentstable.cpp
Update copyright headers for 2016
[icinga2] / lib / livestatus / commentstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://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 Comment::Ptr& comment, ConfigType::GetObjectsByType<Comment>()) {
69                 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
70                         return;
71         }
72 }
73
74 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
75 {
76         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
77
78         Checkable::Ptr checkable = comment->GetCheckable();
79
80         Host::Ptr host;
81         Service::Ptr service;
82         tie(host, service) = GetHostService(checkable);
83
84         return host;
85 }
86
87 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
88 {
89         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
90
91         Checkable::Ptr checkable = comment->GetCheckable();
92
93         Host::Ptr host;
94         Service::Ptr service;
95         tie(host, service) = GetHostService(checkable);
96
97         return service;
98 }
99
100 Value CommentsTable::AuthorAccessor(const Value& row)
101 {
102         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
103
104         if (!comment)
105                 return Empty;
106
107         return comment->GetAuthor();
108 }
109
110 Value CommentsTable::CommentAccessor(const Value& row)
111 {
112         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
113
114         if (!comment)
115                 return Empty;
116
117         return comment->GetText();
118 }
119
120 Value CommentsTable::IdAccessor(const Value& row)
121 {
122         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
123
124         if (!comment)
125                 return Empty;
126
127         return comment->GetLegacyId();
128 }
129
130 Value CommentsTable::EntryTimeAccessor(const Value& row)
131 {
132         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
133
134         if (!comment)
135                 return Empty;
136
137         return static_cast<int>(comment->GetEntryTime());
138 }
139
140 Value CommentsTable::TypeAccessor(const Value& row)
141 {
142         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
143         Checkable::Ptr checkable = comment->GetCheckable();
144
145         if (!checkable)
146                 return Empty;
147
148         if (dynamic_pointer_cast<Host>(checkable))
149                 return 1;
150         else
151                 return 2;
152 }
153
154 Value CommentsTable::IsServiceAccessor(const Value& row)
155 {
156         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
157         Checkable::Ptr checkable = comment->GetCheckable();
158
159         if (!checkable)
160                 return Empty;
161
162         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
163 }
164
165 Value CommentsTable::EntryTypeAccessor(const Value& row)
166 {
167         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
168
169         if (!comment)
170                 return Empty;
171
172         return comment->GetEntryType();
173 }
174
175 Value CommentsTable::ExpiresAccessor(const Value& row)
176 {
177         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
178
179         if (!comment)
180                 return Empty;
181
182         return comment->GetExpireTime() != 0;
183 }
184
185 Value CommentsTable::ExpireTimeAccessor(const Value& row)
186 {
187         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
188
189         if (!comment)
190                 return Empty;
191
192         return static_cast<int>(comment->GetExpireTime());
193 }