1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "db_ido/hostdbobject.hpp"
4 #include "db_ido/hostgroupdbobject.hpp"
5 #include "db_ido/dbtype.hpp"
6 #include "db_ido/dbvalue.hpp"
7 #include "db_ido/dbevents.hpp"
8 #include "icinga/host.hpp"
9 #include "icinga/service.hpp"
10 #include "icinga/notification.hpp"
11 #include "icinga/dependency.hpp"
12 #include "icinga/checkcommand.hpp"
13 #include "icinga/eventcommand.hpp"
14 #include "icinga/compatutility.hpp"
15 #include "icinga/pluginutility.hpp"
16 #include "base/convert.hpp"
17 #include "base/objectlock.hpp"
18 #include "base/logger.hpp"
19 #include "base/json.hpp"
21 using namespace icinga;
23 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
25 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
26 : DbObject(type, name1, name2)
29 Dictionary::Ptr HostDbObject::GetConfigFields() const
31 Dictionary::Ptr fields = new Dictionary();
32 Host::Ptr host = static_pointer_cast<Host>(GetObject());
34 /* Compatibility fallback. */
35 String displayName = host->GetDisplayName();
37 unsigned long notificationStateFilter = CompatUtility::GetCheckableNotificationTypeFilter(host);
38 unsigned long notificationTypeFilter = CompatUtility::GetCheckableNotificationTypeFilter(host);
40 return new Dictionary({
41 { "alias", !displayName.IsEmpty() ? displayName : host->GetName() },
42 { "display_name", displayName },
43 { "address", host->GetAddress() },
44 { "address6", host->GetAddress6() },
45 { "check_command_object_id", host->GetCheckCommand() },
46 { "eventhandler_command_object_id", host->GetEventCommand() },
47 { "check_timeperiod_object_id", host->GetCheckPeriod() },
48 { "check_interval", host->GetCheckInterval() / 60.0 },
49 { "retry_interval", host->GetRetryInterval() / 60.0 },
50 { "max_check_attempts", host->GetMaxCheckAttempts() },
51 { "flap_detection_enabled", host->GetEnableFlapping() },
52 { "low_flap_threshold", host->GetFlappingThresholdLow() },
53 { "high_flap_threshold", host->GetFlappingThresholdLow() },
54 { "process_performance_data", host->GetEnablePerfdata() },
55 { "freshness_checks_enabled", 1 },
56 { "freshness_threshold", Convert::ToLong(host->GetCheckInterval()) },
57 { "event_handler_enabled", host->GetEnableEventHandler() },
58 { "passive_checks_enabled", host->GetEnablePassiveChecks() },
59 { "active_checks_enabled", host->GetEnableActiveChecks() },
60 { "notifications_enabled", host->GetEnableNotifications() },
61 { "notes", host->GetNotes() },
62 { "notes_url", host->GetNotesUrl() },
63 { "action_url", host->GetActionUrl() },
64 { "icon_image", host->GetIconImage() },
65 { "icon_image_alt", host->GetIconImageAlt() },
66 { "notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(host) },
67 { "notify_on_down", (notificationStateFilter & (ServiceWarning | ServiceCritical)) ? 1 : 0 },
68 { "notify_on_unreachable", 1 }, /* We don't have this filter and state, and as such we don't filter such notifications. */
69 { "notify_on_recovery", (notificationTypeFilter & NotificationRecovery) ? 1 : 0 },
70 { "notify_on_flapping", (notificationTypeFilter & (NotificationFlappingStart | NotificationFlappingEnd)) ? 1 : 0 },
71 { "notify_on_downtime", (notificationTypeFilter & (NotificationDowntimeStart | NotificationDowntimeEnd | NotificationDowntimeRemoved)) ? 1 : 0 }
75 Dictionary::Ptr HostDbObject::GetStatusFields() const
77 Dictionary::Ptr fields = new Dictionary();
78 Host::Ptr host = static_pointer_cast<Host>(GetObject());
80 CheckResult::Ptr cr = host->GetLastCheckResult();
83 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
84 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
85 fields->Set("perfdata", PluginUtility::FormatPerfdata(cr->GetPerformanceData()));
86 fields->Set("check_source", cr->GetCheckSource());
87 fields->Set("latency", cr->CalculateLatency());
88 fields->Set("execution_time", cr->CalculateExecutionTime());
91 int currentState = host->GetState();
93 if (currentState != HostUp && !host->IsReachable())
94 currentState = 2; /* hardcoded compat state */
96 fields->Set("current_state", currentState);
97 fields->Set("has_been_checked", host->HasBeenChecked());
98 fields->Set("should_be_scheduled", host->GetEnableActiveChecks());
99 fields->Set("current_check_attempt", host->GetCheckAttempt());
100 fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
101 fields->Set("last_check", DbValue::FromTimestamp(host->GetLastCheck()));
102 fields->Set("next_check", DbValue::FromTimestamp(host->GetNextCheck()));
103 fields->Set("check_type", !host->GetEnableActiveChecks()); /* 0 .. active, 1 .. passive */
104 fields->Set("last_state_change", DbValue::FromTimestamp(host->GetLastStateChange()));
105 fields->Set("last_hard_state_change", DbValue::FromTimestamp(host->GetLastHardStateChange()));
106 fields->Set("last_hard_state", host->GetLastHardState());
107 fields->Set("last_time_up", DbValue::FromTimestamp(host->GetLastStateUp()));
108 fields->Set("last_time_down", DbValue::FromTimestamp(host->GetLastStateDown()));
109 fields->Set("last_time_unreachable", DbValue::FromTimestamp(host->GetLastStateUnreachable()));
110 fields->Set("state_type", host->GetStateType());
111 fields->Set("notifications_enabled", host->GetEnableNotifications());
112 fields->Set("problem_has_been_acknowledged", host->GetAcknowledgement() != AcknowledgementNone);
113 fields->Set("acknowledgement_type", host->GetAcknowledgement());
114 fields->Set("passive_checks_enabled", host->GetEnablePassiveChecks());
115 fields->Set("active_checks_enabled", host->GetEnableActiveChecks());
116 fields->Set("event_handler_enabled", host->GetEnableEventHandler());
117 fields->Set("flap_detection_enabled", host->GetEnableFlapping());
118 fields->Set("is_flapping", host->IsFlapping());
119 fields->Set("percent_state_change", host->GetFlappingCurrent());
120 fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth());
121 fields->Set("process_performance_data", host->GetEnablePerfdata());
122 fields->Set("normal_check_interval", host->GetCheckInterval() / 60.0);
123 fields->Set("retry_check_interval", host->GetRetryInterval() / 60.0);
124 fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
125 fields->Set("is_reachable", host->IsReachable());
126 fields->Set("original_attributes", JsonEncode(host->GetOriginalAttributes()));
128 fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(host));
129 fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(host)));
130 fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(host)));
132 EventCommand::Ptr eventCommand = host->GetEventCommand();
135 fields->Set("event_handler", eventCommand->GetName());
137 CheckCommand::Ptr checkCommand = host->GetCheckCommand();
140 fields->Set("check_command", checkCommand->GetName());
145 void HostDbObject::OnConfigUpdateHeavy()
147 Host::Ptr host = static_pointer_cast<Host>(GetObject());
150 Array::Ptr groups = host->GetGroups();
152 std::vector<DbQuery> queries;
155 query1.Table = DbType::GetByName("HostGroup")->GetTable() + "_members";
156 query1.Type = DbQueryDelete;
157 query1.Category = DbCatConfig;
158 query1.WhereCriteria = new Dictionary();
159 query1.WhereCriteria->Set("host_object_id", host);
160 queries.emplace_back(std::move(query1));
163 ObjectLock olock(groups);
164 for (const String& groupName : groups) {
165 HostGroup::Ptr group = HostGroup::GetByName(groupName);
168 query2.Table = DbType::GetByName("HostGroup")->GetTable() + "_members";
169 query2.Type = DbQueryInsert;
170 query2.Category = DbCatConfig;
171 query2.Fields = new Dictionary({
172 { "instance_id", 0 }, /* DbConnection class fills in real ID */
173 { "hostgroup_id", DbValue::FromObjectInsertID(group) },
174 { "host_object_id", host }
176 query2.WhereCriteria = new Dictionary({
177 { "instance_id", 0 }, /* DbConnection class fills in real ID */
178 { "hostgroup_id", DbValue::FromObjectInsertID(group) },
179 { "host_object_id", host }
181 queries.emplace_back(std::move(query2));
185 DbObject::OnMultipleQueries(queries);
190 query2.Table = GetType()->GetTable() + "_parenthosts";
191 query2.Type = DbQueryDelete;
192 query2.Category = DbCatConfig;
193 query2.WhereCriteria = new Dictionary({
194 { GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()) }
196 queries.emplace_back(std::move(query2));
199 for (const Checkable::Ptr& checkable : host->GetParents()) {
200 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
205 Log(LogDebug, "HostDbObject")
206 << "host parents: " << parent->GetName();
208 /* parents: host_id, parent_host_object_id */
210 query1.Table = GetType()->GetTable() + "_parenthosts";
211 query1.Type = DbQueryInsert;
212 query1.Category = DbCatConfig;
213 query1.Fields = new Dictionary({
214 { GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()) },
215 { "parent_host_object_id", parent },
216 { "instance_id", 0 } /* DbConnection class fills in real ID */
218 queries.emplace_back(std::move(query1));
221 DbObject::OnMultipleQueries(queries);
223 /* host dependencies */
224 Log(LogDebug, "HostDbObject")
225 << "host dependencies for '" << host->GetName() << "'";
230 query3.Table = GetType()->GetTable() + "dependencies";
231 query3.Type = DbQueryDelete;
232 query3.Category = DbCatConfig;
233 query3.WhereCriteria = new Dictionary({
234 { "dependent_host_object_id", host }
236 queries.emplace_back(std::move(query3));
238 for (const Dependency::Ptr& dep : host->GetDependencies()) {
239 Checkable::Ptr parent = dep->GetParent();
242 Log(LogDebug, "HostDbObject")
243 << "Missing parent for dependency '" << dep->GetName() << "'.";
247 int stateFilter = dep->GetStateFilter();
249 Log(LogDebug, "HostDbObject")
250 << "parent host: " << parent->GetName();
253 query2.Table = GetType()->GetTable() + "dependencies";
254 query2.Type = DbQueryInsert;
255 query2.Category = DbCatConfig;
256 query2.Fields = new Dictionary({
257 { "host_object_id", parent },
258 { "dependent_host_object_id", host },
259 { "inherits_parent", 1 },
260 { "timeperiod_object_id", dep->GetPeriod() },
261 { "fail_on_up", (stateFilter & StateFilterUp) ? 1 : 0 },
262 { "fail_on_down", (stateFilter & StateFilterDown) ? 1 : 0 },
263 { "instance_id", 0 } /* DbConnection class fills in real ID */
265 queries.emplace_back(std::move(query2));
268 DbObject::OnMultipleQueries(queries);
270 Log(LogDebug, "HostDbObject")
271 << "host contacts: " << host->GetName();
276 query4.Table = GetType()->GetTable() + "_contacts";
277 query4.Type = DbQueryDelete;
278 query4.Category = DbCatConfig;
279 query4.WhereCriteria = new Dictionary({
280 { "host_id", DbValue::FromObjectInsertID(host) }
282 queries.emplace_back(std::move(query4));
284 for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
285 Log(LogDebug, "HostDbObject")
286 << "host contacts: " << user->GetName();
288 DbQuery query_contact;
289 query_contact.Table = GetType()->GetTable() + "_contacts";
290 query_contact.Type = DbQueryInsert;
291 query_contact.Category = DbCatConfig;
292 query_contact.Fields = new Dictionary({
293 { "host_id", DbValue::FromObjectInsertID(host) },
294 { "contact_object_id", user },
295 { "instance_id", 0 } /* DbConnection class fills in real ID */
297 queries.emplace_back(std::move(query_contact));
300 DbObject::OnMultipleQueries(queries);
302 Log(LogDebug, "HostDbObject")
303 << "host contactgroups: " << host->GetName();
308 query5.Table = GetType()->GetTable() + "_contactgroups";
309 query5.Type = DbQueryDelete;
310 query5.Category = DbCatConfig;
311 query5.WhereCriteria = new Dictionary({
312 { "host_id", DbValue::FromObjectInsertID(host) }
314 queries.emplace_back(std::move(query5));
316 for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
317 Log(LogDebug, "HostDbObject")
318 << "host contactgroups: " << usergroup->GetName();
320 DbQuery query_contact;
321 query_contact.Table = GetType()->GetTable() + "_contactgroups";
322 query_contact.Type = DbQueryInsert;
323 query_contact.Category = DbCatConfig;
324 query_contact.Fields = new Dictionary({
325 { "host_id", DbValue::FromObjectInsertID(host) },
326 { "contactgroup_object_id", usergroup },
327 { "instance_id", 0 } /* DbConnection class fills in real ID */
329 queries.emplace_back(std::move(query_contact));
332 DbObject::OnMultipleQueries(queries);
334 DoCommonConfigUpdate();
337 void HostDbObject::OnConfigUpdateLight()
339 DoCommonConfigUpdate();
342 void HostDbObject::DoCommonConfigUpdate()
344 Host::Ptr host = static_pointer_cast<Host>(GetObject());
346 /* update comments and downtimes on config change */
347 DbEvents::AddComments(host);
348 DbEvents::AddDowntimes(host);
351 String HostDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
353 String hashData = DbObject::CalculateConfigHash(configFields);
355 Host::Ptr host = static_pointer_cast<Host>(GetObject());
357 Array::Ptr groups = host->GetGroups();
360 hashData += DbObject::HashValue(groups);
365 for (const Checkable::Ptr& checkable : host->GetParents()) {
366 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
371 parents.push_back(parent->GetName());
374 std::sort(parents.begin(), parents.end());
376 hashData += DbObject::HashValue(new Array(std::move(parents)));
378 ArrayData dependencies;
381 for (const Dependency::Ptr& dep : host->GetDependencies()) {
382 Checkable::Ptr parent = dep->GetParent();
387 dependencies.push_back(new Array({
389 dep->GetStateFilter(),
394 std::sort(dependencies.begin(), dependencies.end());
396 hashData += DbObject::HashValue(new Array(std::move(dependencies)));
400 for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
401 users.push_back(user->GetName());
404 std::sort(users.begin(), users.end());
406 hashData += DbObject::HashValue(new Array(std::move(users)));
408 ArrayData userGroups;
410 for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
411 userGroups.push_back(usergroup->GetName());
414 std::sort(userGroups.begin(), userGroups.end());
416 hashData += DbObject::HashValue(new Array(std::move(userGroups)));
418 return SHA256(hashData);