1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
30 using namespace icinga;
32 ContactsTable::ContactsTable(void)
37 void ContactsTable::AddColumns(Table *table, const String& prefix,
38 const Column::ObjectAccessor& objectAccessor)
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));
58 String ContactsTable::GetName(void) const
63 void ContactsTable::FetchRows(const AddRowFunction& addRowFn)
65 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
70 Value ContactsTable::NameAccessor(const Value& row)
72 User::Ptr user = static_cast<User::Ptr>(row);
77 return user->GetName();
80 Value ContactsTable::AliasAccessor(const Value& row)
82 User::Ptr user = static_cast<User::Ptr>(row);
87 return user->GetDisplayName();
90 Value ContactsTable::EmailAccessor(const Value& row)
92 User::Ptr user = static_cast<User::Ptr>(row);
97 return user->GetEmail();
100 Value ContactsTable::PagerAccessor(const Value& row)
102 User::Ptr user = static_cast<User::Ptr>(row);
107 return user->GetPager();
110 Value ContactsTable::HostNotificationPeriodAccessor(const Value& row)
112 User::Ptr user = static_cast<User::Ptr>(row);
117 /* same as service */
118 TimePeriod::Ptr timeperiod = user->GetPeriod();
123 return timeperiod->GetName();
126 Value ContactsTable::ServiceNotificationPeriodAccessor(const Value& row)
128 User::Ptr user = static_cast<User::Ptr>(row);
133 TimePeriod::Ptr timeperiod = user->GetPeriod();
138 return timeperiod->GetName();
141 Value ContactsTable::HostNotificationsEnabledAccessor(const Value& row)
143 User::Ptr user = static_cast<User::Ptr>(row);
148 return (user->GetEnableNotifications() ? 1 : 0);
151 Value ContactsTable::ServiceNotificationsEnabledAccessor(const Value& row)
153 User::Ptr user = static_cast<User::Ptr>(row);
158 return (user->GetEnableNotifications() ? 1 : 0);
161 Value ContactsTable::InHostNotificationPeriodAccessor(const Value& row)
163 User::Ptr user = static_cast<User::Ptr>(row);
168 TimePeriod::Ptr timeperiod = user->GetPeriod();
173 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
176 Value ContactsTable::InServiceNotificationPeriodAccessor(const Value& row)
178 User::Ptr user = static_cast<User::Ptr>(row);
183 TimePeriod::Ptr timeperiod = user->GetPeriod();
188 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
191 Value ContactsTable::CustomVariableNamesAccessor(const Value& row)
193 User::Ptr user = static_cast<User::Ptr>(row);
198 Dictionary::Ptr vars = user->GetVars();
203 Array::Ptr cv = make_shared<Array>();
205 ObjectLock olock(vars);
208 BOOST_FOREACH(boost::tie(key, value), vars) {
215 Value ContactsTable::CustomVariableValuesAccessor(const Value& row)
217 User::Ptr user = static_cast<User::Ptr>(row);
222 Dictionary::Ptr vars = user->GetVars();
227 Array::Ptr cv = make_shared<Array>();
229 ObjectLock olock(vars);
232 BOOST_FOREACH(boost::tie(key, value), vars) {
239 Value ContactsTable::CustomVariablesAccessor(const Value& row)
241 User::Ptr user = static_cast<User::Ptr>(row);
246 Dictionary::Ptr vars = user->GetVars();
251 Array::Ptr cv = make_shared<Array>();
253 ObjectLock olock(vars);
256 BOOST_FOREACH(boost::tie(key, value), vars) {
257 Array::Ptr key_val = make_shared<Array>();
266 Value ContactsTable::ModifiedAttributesAccessor(const Value& row)
268 User::Ptr user = static_cast<User::Ptr>(row);
274 return user->GetModifiedAttributes();
277 Value ContactsTable::ModifiedAttributesListAccessor(const Value& row)
279 User::Ptr user = static_cast<User::Ptr>(row);
284 return CompatUtility::GetModifiedAttributesList(user);