]> granicus.if.org Git - icinga2/blob - lib/livestatus/commentstable.cpp
Merge pull request #5803 from Icinga/feature/cxx11-std-bind
[icinga2] / lib / livestatus / commentstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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
28 using namespace icinga;
29
30 CommentsTable::CommentsTable(void)
31 {
32         AddColumns(this);
33 }
34
35 void CommentsTable::AddColumns(Table *table, const String& prefix,
36     const Column::ObjectAccessor& objectAccessor)
37 {
38         table->AddColumn(prefix + "author", Column(&CommentsTable::AuthorAccessor, objectAccessor));
39         table->AddColumn(prefix + "comment", Column(&CommentsTable::CommentAccessor, objectAccessor));
40         table->AddColumn(prefix + "id", Column(&CommentsTable::IdAccessor, objectAccessor));
41         table->AddColumn(prefix + "entry_time", Column(&CommentsTable::EntryTimeAccessor, objectAccessor));
42         table->AddColumn(prefix + "type", Column(&CommentsTable::TypeAccessor, objectAccessor));
43         table->AddColumn(prefix + "is_service", Column(&CommentsTable::IsServiceAccessor, objectAccessor));
44         table->AddColumn(prefix + "persistent", Column(&Table::OneAccessor, objectAccessor));
45         table->AddColumn(prefix + "source", Column(&Table::OneAccessor, objectAccessor));
46         table->AddColumn(prefix + "entry_type", Column(&CommentsTable::EntryTypeAccessor, objectAccessor));
47         table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor));
48         table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor));
49
50         /* order is important - host w/o services must not be empty */
51         ServicesTable::AddColumns(table, "service_", std::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor));
52         HostsTable::AddColumns(table, "host_", std::bind(&CommentsTable::HostAccessor, _1, objectAccessor));
53 }
54
55 String CommentsTable::GetName(void) const
56 {
57         return "comments";
58 }
59
60 String CommentsTable::GetPrefix(void) const
61 {
62         return "comment";
63 }
64
65 void CommentsTable::FetchRows(const AddRowFunction& addRowFn)
66 {
67         for (const Comment::Ptr& comment : ConfigType::GetObjectsByType<Comment>()) {
68                 if (!addRowFn(comment, LivestatusGroupByNone, Empty))
69                         return;
70         }
71 }
72
73 Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
74 {
75         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
76
77         Checkable::Ptr checkable = comment->GetCheckable();
78
79         Host::Ptr host;
80         Service::Ptr service;
81         tie(host, service) = GetHostService(checkable);
82
83         return host;
84 }
85
86 Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
87 {
88         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
89
90         Checkable::Ptr checkable = comment->GetCheckable();
91
92         Host::Ptr host;
93         Service::Ptr service;
94         tie(host, service) = GetHostService(checkable);
95
96         return service;
97 }
98
99 Value CommentsTable::AuthorAccessor(const Value& row)
100 {
101         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
102
103         if (!comment)
104                 return Empty;
105
106         return comment->GetAuthor();
107 }
108
109 Value CommentsTable::CommentAccessor(const Value& row)
110 {
111         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
112
113         if (!comment)
114                 return Empty;
115
116         return comment->GetText();
117 }
118
119 Value CommentsTable::IdAccessor(const Value& row)
120 {
121         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
122
123         if (!comment)
124                 return Empty;
125
126         return comment->GetLegacyId();
127 }
128
129 Value CommentsTable::EntryTimeAccessor(const Value& row)
130 {
131         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
132
133         if (!comment)
134                 return Empty;
135
136         return static_cast<int>(comment->GetEntryTime());
137 }
138
139 Value CommentsTable::TypeAccessor(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         if (dynamic_pointer_cast<Host>(checkable))
148                 return 1;
149         else
150                 return 2;
151 }
152
153 Value CommentsTable::IsServiceAccessor(const Value& row)
154 {
155         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
156         Checkable::Ptr checkable = comment->GetCheckable();
157
158         if (!checkable)
159                 return Empty;
160
161         return (dynamic_pointer_cast<Host>(checkable) ? 0 : 1);
162 }
163
164 Value CommentsTable::EntryTypeAccessor(const Value& row)
165 {
166         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
167
168         if (!comment)
169                 return Empty;
170
171         return comment->GetEntryType();
172 }
173
174 Value CommentsTable::ExpiresAccessor(const Value& row)
175 {
176         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
177
178         if (!comment)
179                 return Empty;
180
181         return comment->GetExpireTime() != 0;
182 }
183
184 Value CommentsTable::ExpireTimeAccessor(const Value& row)
185 {
186         Comment::Ptr comment = static_cast<Comment::Ptr>(row);
187
188         if (!comment)
189                 return Empty;
190
191         return static_cast<int>(comment->GetExpireTime());
192 }