]> granicus.if.org Git - icinga2/blob - components/livestatus/contactstable.cpp
Add CompatUtility::GetModifiedAttributesList() for Livestatus.
[icinga2] / components / livestatus / contactstable.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/contactstable.h"
21 #include "icinga/user.h"
22 #include "icinga/timeperiod.h"
23 #include "icinga/compatutility.h"
24 #include "base/dynamictype.h"
25 #include "base/objectlock.h"
26 #include "base/utility.h"
27 #include <boost/foreach.hpp>
28 #include <boost/tuple/tuple.hpp>
29
30 using namespace icinga;
31
32 ContactsTable::ContactsTable(void)
33 {
34         AddColumns(this);
35 }
36
37 void ContactsTable::AddColumns(Table *table, const String& prefix,
38         const Column::ObjectAccessor& objectAccessor)
39 {
40         table->AddColumn(prefix + "name", Column(&ContactsTable::NameAccessor, objectAccessor));
41         table->AddColumn(prefix + "alias", Column(&ContactsTable::NameAccessor, objectAccessor));
42         table->AddColumn(prefix + "email", Column(&ContactsTable::EmailAccessor, objectAccessor));
43         table->AddColumn(prefix + "pager", Column(&ContactsTable::PagerAccessor, objectAccessor));
44         table->AddColumn(prefix + "host_notification_period", Column(&ContactsTable::HostNotificationPeriodAccessor, objectAccessor));
45         table->AddColumn(prefix + "service_notification_period", Column(&ContactsTable::ServiceNotificationPeriodAccessor, objectAccessor));
46         table->AddColumn(prefix + "can_submit_commands", Column(&Table::OneAccessor, objectAccessor));
47         table->AddColumn(prefix + "host_notifications_enabled", Column(&ContactsTable::HostNotificationsEnabledAccessor, objectAccessor));
48         table->AddColumn(prefix + "service_notifications_enabled", Column(&ContactsTable::ServiceNotificationsEnabledAccessor, objectAccessor));
49         table->AddColumn(prefix + "in_host_notification_period", Column(&ContactsTable::InHostNotificationPeriodAccessor, objectAccessor));
50         table->AddColumn(prefix + "in_service_notification_period", Column(&ContactsTable::InServiceNotificationPeriodAccessor, objectAccessor));
51         table->AddColumn(prefix + "vars_variable_names", Column(&ContactsTable::CustomVariableNamesAccessor, objectAccessor));
52         table->AddColumn(prefix + "vars_variable_values", Column(&ContactsTable::CustomVariableValuesAccessor, objectAccessor));
53         table->AddColumn(prefix + "vars_variables", Column(&ContactsTable::CustomVariablesAccessor, objectAccessor));
54         table->AddColumn(prefix + "modified_attributes", Column(&ContactsTable::ModifiedAttributesAccessor, objectAccessor));
55         table->AddColumn(prefix + "modified_attributes_list", Column(&ContactsTable::ModifiedAttributesListAccessor, objectAccessor));
56 }
57
58 String ContactsTable::GetName(void) const
59 {
60         return "contacts";
61 }
62
63 void ContactsTable::FetchRows(const AddRowFunction& addRowFn)
64 {
65         BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
66                 addRowFn(user);
67         }
68 }
69
70 Value ContactsTable::NameAccessor(const Value& row)
71 {
72         User::Ptr user = static_cast<User::Ptr>(row);
73
74         if (!user)
75                 return Empty;
76
77         return user->GetName();
78 }
79
80 Value ContactsTable::AliasAccessor(const Value& row)
81 {
82         User::Ptr user = static_cast<User::Ptr>(row);
83
84         if (!user)
85                 return Empty;
86
87         return user->GetDisplayName();
88 }
89
90 Value ContactsTable::EmailAccessor(const Value& row)
91 {
92         User::Ptr user = static_cast<User::Ptr>(row);
93
94         if (!user)
95                 return Empty;
96
97         return user->GetEmail();
98 }
99
100 Value ContactsTable::PagerAccessor(const Value& row)
101 {
102         User::Ptr user = static_cast<User::Ptr>(row);
103
104         if (!user)
105                 return Empty;
106
107         return user->GetPager();
108 }
109
110 Value ContactsTable::HostNotificationPeriodAccessor(const Value& row)
111 {
112         User::Ptr user = static_cast<User::Ptr>(row);
113
114         if (!user)
115                 return Empty;
116
117         /* same as service */
118         TimePeriod::Ptr timeperiod = user->GetPeriod();
119
120         if (!timeperiod)
121                 return Empty;
122
123         return timeperiod->GetName();
124 }
125
126 Value ContactsTable::ServiceNotificationPeriodAccessor(const Value& row)
127 {
128         User::Ptr user = static_cast<User::Ptr>(row);
129
130         if (!user)
131                 return Empty;
132
133         TimePeriod::Ptr timeperiod = user->GetPeriod();
134
135         if (!timeperiod)
136                 return Empty;
137
138         return timeperiod->GetName();
139 }
140
141 Value ContactsTable::HostNotificationsEnabledAccessor(const Value& row)
142 {
143         User::Ptr user = static_cast<User::Ptr>(row);
144
145         if (!user)
146                 return Empty;
147
148         return (user->GetEnableNotifications() ? 1 : 0);
149 }
150
151 Value ContactsTable::ServiceNotificationsEnabledAccessor(const Value& row)
152 {
153         User::Ptr user = static_cast<User::Ptr>(row);
154
155         if (!user)
156                 return Empty;
157
158         return (user->GetEnableNotifications() ? 1 : 0);
159 }
160
161 Value ContactsTable::InHostNotificationPeriodAccessor(const Value& row)
162 {
163         User::Ptr user = static_cast<User::Ptr>(row);
164
165         if (!user)
166                 return Empty;
167
168         TimePeriod::Ptr timeperiod = user->GetPeriod();
169
170         if (!timeperiod)
171                 return Empty;
172
173         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
174 }
175
176 Value ContactsTable::InServiceNotificationPeriodAccessor(const Value& row)
177 {
178         User::Ptr user = static_cast<User::Ptr>(row);
179
180         if (!user)
181                 return Empty;
182
183         TimePeriod::Ptr timeperiod = user->GetPeriod();
184
185         if (!timeperiod)
186                 return Empty;
187
188         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
189 }
190
191 Value ContactsTable::CustomVariableNamesAccessor(const Value& row)
192 {
193         User::Ptr user = static_cast<User::Ptr>(row);
194
195         if (!user)
196                 return Empty;
197
198         Dictionary::Ptr vars = user->GetVars();
199
200         if (!vars)
201                 return Empty;
202
203         Array::Ptr cv = make_shared<Array>();
204
205         ObjectLock olock(vars);
206         String key;
207         Value value;
208         BOOST_FOREACH(boost::tie(key, value), vars) {
209                 cv->Add(key);
210         }
211
212         return cv;
213 }
214
215 Value ContactsTable::CustomVariableValuesAccessor(const Value& row)
216 {
217         User::Ptr user = static_cast<User::Ptr>(row);
218
219         if (!user)
220                 return Empty;
221
222         Dictionary::Ptr vars = user->GetVars();
223
224         if (!vars)
225                 return Empty;
226
227         Array::Ptr cv = make_shared<Array>();
228
229         ObjectLock olock(vars);
230         String key;
231         Value value;
232         BOOST_FOREACH(boost::tie(key, value), vars) {
233                 cv->Add(value);
234         }
235
236         return cv;
237 }
238
239 Value ContactsTable::CustomVariablesAccessor(const Value& row)
240 {
241         User::Ptr user = static_cast<User::Ptr>(row);
242
243         if (!user)
244                 return Empty;
245
246         Dictionary::Ptr vars = user->GetVars();
247
248         if (!vars)
249                 return Empty;
250
251         Array::Ptr cv = make_shared<Array>();
252
253         ObjectLock olock(vars);
254         String key;
255         Value value;
256         BOOST_FOREACH(boost::tie(key, value), vars) {
257                 Array::Ptr key_val = make_shared<Array>();
258                 key_val->Add(key);
259                 key_val->Add(value);
260                 cv->Add(key_val);
261         }
262
263         return cv;
264 }
265
266 Value ContactsTable::ModifiedAttributesAccessor(const Value& row)
267 {
268         User::Ptr user = static_cast<User::Ptr>(row);
269
270         if (!user)
271                 return Empty;
272
273         /* not supported */
274         return user->GetModifiedAttributes();
275 }
276
277 Value ContactsTable::ModifiedAttributesListAccessor(const Value& row)
278 {
279         User::Ptr user = static_cast<User::Ptr>(row);
280
281         if (!user)
282                 return Empty;
283
284         return CompatUtility::GetModifiedAttributesList(user);
285 }