1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/contactstable.hpp"
4 #include "icinga/user.hpp"
5 #include "icinga/timeperiod.hpp"
6 #include "icinga/compatutility.hpp"
7 #include "base/configtype.hpp"
8 #include "base/objectlock.hpp"
9 #include "base/json.hpp"
10 #include "base/utility.hpp"
11 #include <boost/tuple/tuple.hpp>
13 using namespace icinga;
15 ContactsTable::ContactsTable()
20 void ContactsTable::AddColumns(Table *table, const String& prefix,
21 const Column::ObjectAccessor& objectAccessor)
23 table->AddColumn(prefix + "name", Column(&ContactsTable::NameAccessor, objectAccessor));
24 table->AddColumn(prefix + "alias", Column(&ContactsTable::NameAccessor, objectAccessor));
25 table->AddColumn(prefix + "email", Column(&ContactsTable::EmailAccessor, objectAccessor));
26 table->AddColumn(prefix + "pager", Column(&ContactsTable::PagerAccessor, objectAccessor));
27 table->AddColumn(prefix + "host_notification_period", Column(&ContactsTable::HostNotificationPeriodAccessor, objectAccessor));
28 table->AddColumn(prefix + "service_notification_period", Column(&ContactsTable::ServiceNotificationPeriodAccessor, objectAccessor));
29 table->AddColumn(prefix + "can_submit_commands", Column(&Table::OneAccessor, objectAccessor));
30 table->AddColumn(prefix + "host_notifications_enabled", Column(&ContactsTable::HostNotificationsEnabledAccessor, objectAccessor));
31 table->AddColumn(prefix + "service_notifications_enabled", Column(&ContactsTable::ServiceNotificationsEnabledAccessor, objectAccessor));
32 table->AddColumn(prefix + "in_host_notification_period", Column(&ContactsTable::InHostNotificationPeriodAccessor, objectAccessor));
33 table->AddColumn(prefix + "in_service_notification_period", Column(&ContactsTable::InServiceNotificationPeriodAccessor, objectAccessor));
34 table->AddColumn(prefix + "vars_variable_names", Column(&ContactsTable::CustomVariableNamesAccessor, objectAccessor));
35 table->AddColumn(prefix + "vars_variable_values", Column(&ContactsTable::CustomVariableValuesAccessor, objectAccessor));
36 table->AddColumn(prefix + "vars_variables", Column(&ContactsTable::CustomVariablesAccessor, objectAccessor));
37 table->AddColumn(prefix + "modified_attributes", Column(&Table::ZeroAccessor, objectAccessor));
38 table->AddColumn(prefix + "modified_attributes_list", Column(&Table::ZeroAccessor, objectAccessor));
39 table->AddColumn(prefix + "cv_is_json", Column(&ContactsTable::CVIsJsonAccessor, objectAccessor));
43 String ContactsTable::GetName() const
48 String ContactsTable::GetPrefix() const
53 void ContactsTable::FetchRows(const AddRowFunction& addRowFn)
55 for (const User::Ptr& user : ConfigType::GetObjectsByType<User>()) {
56 if (!addRowFn(user, LivestatusGroupByNone, Empty))
61 Value ContactsTable::NameAccessor(const Value& row)
63 User::Ptr user = static_cast<User::Ptr>(row);
68 return user->GetName();
71 Value ContactsTable::AliasAccessor(const Value& row)
73 User::Ptr user = static_cast<User::Ptr>(row);
78 return user->GetDisplayName();
81 Value ContactsTable::EmailAccessor(const Value& row)
83 User::Ptr user = static_cast<User::Ptr>(row);
88 return user->GetEmail();
91 Value ContactsTable::PagerAccessor(const Value& row)
93 User::Ptr user = static_cast<User::Ptr>(row);
98 return user->GetPager();
101 Value ContactsTable::HostNotificationPeriodAccessor(const Value& row)
103 User::Ptr user = static_cast<User::Ptr>(row);
108 /* same as service */
109 TimePeriod::Ptr timeperiod = user->GetPeriod();
114 return timeperiod->GetName();
117 Value ContactsTable::ServiceNotificationPeriodAccessor(const Value& row)
119 User::Ptr user = static_cast<User::Ptr>(row);
124 TimePeriod::Ptr timeperiod = user->GetPeriod();
129 return timeperiod->GetName();
132 Value ContactsTable::HostNotificationsEnabledAccessor(const Value& row)
134 User::Ptr user = static_cast<User::Ptr>(row);
139 return (user->GetEnableNotifications() ? 1 : 0);
142 Value ContactsTable::ServiceNotificationsEnabledAccessor(const Value& row)
144 User::Ptr user = static_cast<User::Ptr>(row);
149 return (user->GetEnableNotifications() ? 1 : 0);
152 Value ContactsTable::InHostNotificationPeriodAccessor(const Value& row)
154 User::Ptr user = static_cast<User::Ptr>(row);
159 TimePeriod::Ptr timeperiod = user->GetPeriod();
164 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
167 Value ContactsTable::InServiceNotificationPeriodAccessor(const Value& row)
169 User::Ptr user = static_cast<User::Ptr>(row);
174 TimePeriod::Ptr timeperiod = user->GetPeriod();
179 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
182 Value ContactsTable::CustomVariableNamesAccessor(const Value& row)
184 User::Ptr user = static_cast<User::Ptr>(row);
189 Dictionary::Ptr vars = user->GetVars();
194 ObjectLock olock(vars);
195 for (const Dictionary::Pair& kv : vars) {
196 result.push_back(kv.first);
200 return new Array(std::move(result));
203 Value ContactsTable::CustomVariableValuesAccessor(const Value& row)
205 User::Ptr user = static_cast<User::Ptr>(row);
210 Dictionary::Ptr vars = user->GetVars();
215 ObjectLock olock(vars);
216 for (const Dictionary::Pair& kv : vars) {
217 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
218 result.push_back(JsonEncode(kv.second));
220 result.push_back(kv.second);
224 return new Array(std::move(result));
227 Value ContactsTable::CustomVariablesAccessor(const Value& row)
229 User::Ptr user = static_cast<User::Ptr>(row);
234 Dictionary::Ptr vars = user->GetVars();
239 ObjectLock olock(vars);
240 for (const Dictionary::Pair& kv : vars) {
243 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
244 val = JsonEncode(kv.second);
248 result.push_back(new Array({
255 return new Array(std::move(result));
258 Value ContactsTable::CVIsJsonAccessor(const Value& row)
260 User::Ptr user = static_cast<User::Ptr>(row);
265 Dictionary::Ptr vars = user->GetVars();
270 bool cv_is_json = false;
272 ObjectLock olock(vars);
273 for (const Dictionary::Pair& kv : vars) {
274 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())