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"
12 using namespace icinga;
14 ContactsTable::ContactsTable()
19 void ContactsTable::AddColumns(Table *table, const String& prefix,
20 const Column::ObjectAccessor& objectAccessor)
22 table->AddColumn(prefix + "name", Column(&ContactsTable::NameAccessor, objectAccessor));
23 table->AddColumn(prefix + "alias", Column(&ContactsTable::NameAccessor, objectAccessor));
24 table->AddColumn(prefix + "email", Column(&ContactsTable::EmailAccessor, objectAccessor));
25 table->AddColumn(prefix + "pager", Column(&ContactsTable::PagerAccessor, objectAccessor));
26 table->AddColumn(prefix + "host_notification_period", Column(&ContactsTable::HostNotificationPeriodAccessor, objectAccessor));
27 table->AddColumn(prefix + "service_notification_period", Column(&ContactsTable::ServiceNotificationPeriodAccessor, objectAccessor));
28 table->AddColumn(prefix + "can_submit_commands", Column(&Table::OneAccessor, objectAccessor));
29 table->AddColumn(prefix + "host_notifications_enabled", Column(&ContactsTable::HostNotificationsEnabledAccessor, objectAccessor));
30 table->AddColumn(prefix + "service_notifications_enabled", Column(&ContactsTable::ServiceNotificationsEnabledAccessor, objectAccessor));
31 table->AddColumn(prefix + "in_host_notification_period", Column(&ContactsTable::InHostNotificationPeriodAccessor, objectAccessor));
32 table->AddColumn(prefix + "in_service_notification_period", Column(&ContactsTable::InServiceNotificationPeriodAccessor, objectAccessor));
33 table->AddColumn(prefix + "vars_variable_names", Column(&ContactsTable::CustomVariableNamesAccessor, objectAccessor));
34 table->AddColumn(prefix + "vars_variable_values", Column(&ContactsTable::CustomVariableValuesAccessor, objectAccessor));
35 table->AddColumn(prefix + "vars_variables", Column(&ContactsTable::CustomVariablesAccessor, objectAccessor));
36 table->AddColumn(prefix + "modified_attributes", Column(&Table::ZeroAccessor, objectAccessor));
37 table->AddColumn(prefix + "modified_attributes_list", Column(&Table::ZeroAccessor, objectAccessor));
38 table->AddColumn(prefix + "cv_is_json", Column(&ContactsTable::CVIsJsonAccessor, objectAccessor));
42 String ContactsTable::GetName() const
47 String ContactsTable::GetPrefix() const
52 void ContactsTable::FetchRows(const AddRowFunction& addRowFn)
54 for (const User::Ptr& user : ConfigType::GetObjectsByType<User>()) {
55 if (!addRowFn(user, LivestatusGroupByNone, Empty))
60 Value ContactsTable::NameAccessor(const Value& row)
62 User::Ptr user = static_cast<User::Ptr>(row);
67 return user->GetName();
70 Value ContactsTable::AliasAccessor(const Value& row)
72 User::Ptr user = static_cast<User::Ptr>(row);
77 return user->GetDisplayName();
80 Value ContactsTable::EmailAccessor(const Value& row)
82 User::Ptr user = static_cast<User::Ptr>(row);
87 return user->GetEmail();
90 Value ContactsTable::PagerAccessor(const Value& row)
92 User::Ptr user = static_cast<User::Ptr>(row);
97 return user->GetPager();
100 Value ContactsTable::HostNotificationPeriodAccessor(const Value& row)
102 User::Ptr user = static_cast<User::Ptr>(row);
107 /* same as service */
108 TimePeriod::Ptr timeperiod = user->GetPeriod();
113 return timeperiod->GetName();
116 Value ContactsTable::ServiceNotificationPeriodAccessor(const Value& row)
118 User::Ptr user = static_cast<User::Ptr>(row);
123 TimePeriod::Ptr timeperiod = user->GetPeriod();
128 return timeperiod->GetName();
131 Value ContactsTable::HostNotificationsEnabledAccessor(const Value& row)
133 User::Ptr user = static_cast<User::Ptr>(row);
138 return (user->GetEnableNotifications() ? 1 : 0);
141 Value ContactsTable::ServiceNotificationsEnabledAccessor(const Value& row)
143 User::Ptr user = static_cast<User::Ptr>(row);
148 return (user->GetEnableNotifications() ? 1 : 0);
151 Value ContactsTable::InHostNotificationPeriodAccessor(const Value& row)
153 User::Ptr user = static_cast<User::Ptr>(row);
158 TimePeriod::Ptr timeperiod = user->GetPeriod();
163 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
166 Value ContactsTable::InServiceNotificationPeriodAccessor(const Value& row)
168 User::Ptr user = static_cast<User::Ptr>(row);
173 TimePeriod::Ptr timeperiod = user->GetPeriod();
178 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
181 Value ContactsTable::CustomVariableNamesAccessor(const Value& row)
183 User::Ptr user = static_cast<User::Ptr>(row);
188 Dictionary::Ptr vars = user->GetVars();
193 ObjectLock olock(vars);
194 for (const Dictionary::Pair& kv : vars) {
195 result.push_back(kv.first);
199 return new Array(std::move(result));
202 Value ContactsTable::CustomVariableValuesAccessor(const Value& row)
204 User::Ptr user = static_cast<User::Ptr>(row);
209 Dictionary::Ptr vars = user->GetVars();
214 ObjectLock olock(vars);
215 for (const Dictionary::Pair& kv : vars) {
216 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
217 result.push_back(JsonEncode(kv.second));
219 result.push_back(kv.second);
223 return new Array(std::move(result));
226 Value ContactsTable::CustomVariablesAccessor(const Value& row)
228 User::Ptr user = static_cast<User::Ptr>(row);
233 Dictionary::Ptr vars = user->GetVars();
238 ObjectLock olock(vars);
239 for (const Dictionary::Pair& kv : vars) {
242 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
243 val = JsonEncode(kv.second);
247 result.push_back(new Array({
254 return new Array(std::move(result));
257 Value ContactsTable::CVIsJsonAccessor(const Value& row)
259 User::Ptr user = static_cast<User::Ptr>(row);
264 Dictionary::Ptr vars = user->GetVars();
269 bool cv_is_json = false;
271 ObjectLock olock(vars);
272 for (const Dictionary::Pair& kv : vars) {
273 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())