1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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 "db_ido/hostdbobject.hpp"
21 #include "db_ido/hostgroupdbobject.hpp"
22 #include "db_ido/dbtype.hpp"
23 #include "db_ido/dbvalue.hpp"
24 #include "db_ido/dbevents.hpp"
25 #include "icinga/host.hpp"
26 #include "icinga/service.hpp"
27 #include "icinga/notification.hpp"
28 #include "icinga/dependency.hpp"
29 #include "icinga/checkcommand.hpp"
30 #include "icinga/eventcommand.hpp"
31 #include "icinga/compatutility.hpp"
32 #include "base/convert.hpp"
33 #include "base/objectlock.hpp"
34 #include "base/logger.hpp"
35 #include "base/json.hpp"
37 using namespace icinga;
39 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
41 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
42 : DbObject(type, name1, name2)
45 Dictionary::Ptr HostDbObject::GetConfigFields() const
47 Dictionary::Ptr fields = new Dictionary();
48 Host::Ptr host = static_pointer_cast<Host>(GetObject());
50 /* Compatibility fallback. */
51 String displayName = host->GetDisplayName();
53 if (!displayName.IsEmpty()) {
54 fields->Set("alias", displayName);
56 fields->Set("alias", host->GetName());
59 fields->Set("display_name", displayName);
60 fields->Set("address", host->GetAddress());
61 fields->Set("address6", host->GetAddress6());
63 fields->Set("check_command_object_id", host->GetCheckCommand());
64 fields->Set("check_command_args", CompatUtility::GetCheckableCommandArgs(host));
65 fields->Set("eventhandler_command_object_id", host->GetEventCommand());
66 fields->Set("eventhandler_command_args", Empty);
67 fields->Set("notification_timeperiod_object_id", Empty);
68 fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
69 fields->Set("failure_prediction_options", Empty);
70 fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(host));
71 fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(host));
72 fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
74 fields->Set("first_notification_delay", Empty);
76 fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(host));
77 fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
78 fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
80 fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(host));
81 fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(host));
82 fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(host));
84 fields->Set("stalk_on_up", Empty);
85 fields->Set("stalk_on_down", Empty);
86 fields->Set("stalk_on_unreachable", Empty);
88 fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
89 fields->Set("flap_detection_on_up", Empty);
90 fields->Set("flap_detection_on_down", Empty);
91 fields->Set("flap_detection_on_unreachable", Empty);
92 fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(host));
93 fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(host));
95 fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(host));
97 fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(host));
98 fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(host));
99 fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
100 fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
101 fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
103 fields->Set("retain_status_information", 1);
104 fields->Set("retain_nonstatus_information", 1);
106 fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
108 fields->Set("obsess_over_host", 0);
109 fields->Set("failure_prediction_enabled", 0);
111 fields->Set("notes", host->GetNotes());
112 fields->Set("notes_url", host->GetNotesUrl());
113 fields->Set("action_url", host->GetActionUrl());
114 fields->Set("icon_image", host->GetIconImage());
115 fields->Set("icon_image_alt", host->GetIconImageAlt());
120 Dictionary::Ptr HostDbObject::GetStatusFields() const
122 Dictionary::Ptr fields = new Dictionary();
123 Host::Ptr host = static_pointer_cast<Host>(GetObject());
125 CheckResult::Ptr cr = host->GetLastCheckResult();
128 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
129 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
130 fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
131 fields->Set("check_source", cr->GetCheckSource());
134 fields->Set("current_state", CompatUtility::GetHostCurrentState(host));
135 fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host));
136 fields->Set("should_be_scheduled", host->GetEnableActiveChecks());
137 fields->Set("current_check_attempt", host->GetCheckAttempt());
138 fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
141 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
143 fields->Set("next_check", DbValue::FromTimestamp(host->GetNextCheck()));
144 fields->Set("check_type", !host->GetEnableActiveChecks()); /* 0 .. active, 1 .. passive */
145 fields->Set("last_state_change", DbValue::FromTimestamp(host->GetLastStateChange()));
146 fields->Set("last_hard_state_change", DbValue::FromTimestamp(host->GetLastHardStateChange()));
147 fields->Set("last_hard_state", host->GetLastHardState());
148 fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
149 fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
150 fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
151 fields->Set("state_type", host->GetStateType());
152 fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(host)));
153 fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(host)));
154 fields->Set("no_more_notifications", Empty);
155 fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
156 fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(host));
157 fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(host));
158 fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(host));
159 fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
160 fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
161 fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
162 fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
163 fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(host));
164 fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(host));
167 fields->Set("latency", Convert::ToString(cr->CalculateLatency()));
168 fields->Set("execution_time", Convert::ToString(cr->CalculateExecutionTime()));
171 fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth());
172 fields->Set("failure_prediction_enabled", Empty);
173 fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(host));
174 fields->Set("obsess_over_host", Empty);
175 fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(host));
176 fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(host));
177 fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(host));
178 fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(host));
179 fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
180 fields->Set("is_reachable", CompatUtility::GetCheckableIsReachable(host));
182 fields->Set("original_attributes", JsonEncode(host->GetOriginalAttributes()));
187 void HostDbObject::OnConfigUpdateHeavy()
189 Host::Ptr host = static_pointer_cast<Host>(GetObject());
192 Array::Ptr groups = host->GetGroups();
194 std::vector<DbQuery> queries;
197 query1.Table = DbType::GetByName("HostGroup")->GetTable() + "_members";
198 query1.Type = DbQueryDelete;
199 query1.Category = DbCatConfig;
200 query1.WhereCriteria = new Dictionary();
201 query1.WhereCriteria->Set("host_object_id", host);
202 queries.emplace_back(std::move(query1));
205 ObjectLock olock(groups);
206 for (const String& groupName : groups) {
207 HostGroup::Ptr group = HostGroup::GetByName(groupName);
210 query2.Table = DbType::GetByName("HostGroup")->GetTable() + "_members";
211 query2.Type = DbQueryInsert;
212 query2.Category = DbCatConfig;
213 query2.Fields = new Dictionary();
214 query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
215 query2.Fields->Set("hostgroup_id", DbValue::FromObjectInsertID(group));
216 query2.Fields->Set("host_object_id", host);
217 query2.WhereCriteria = new Dictionary();
218 query2.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
219 query2.WhereCriteria->Set("hostgroup_id", DbValue::FromObjectInsertID(group));
220 query2.WhereCriteria->Set("host_object_id", host);
221 queries.emplace_back(std::move(query2));
225 DbObject::OnMultipleQueries(queries);
230 query2.Table = GetType()->GetTable() + "_parenthosts";
231 query2.Type = DbQueryDelete;
232 query2.Category = DbCatConfig;
233 query2.WhereCriteria = new Dictionary();
234 query2.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
235 queries.emplace_back(std::move(query2));
238 for (const Checkable::Ptr& checkable : host->GetParents()) {
239 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
244 Log(LogDebug, "HostDbObject")
245 << "host parents: " << parent->GetName();
247 /* parents: host_id, parent_host_object_id */
248 Dictionary::Ptr fields1 = new Dictionary();
249 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
250 fields1->Set("parent_host_object_id", parent);
251 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
254 query1.Table = GetType()->GetTable() + "_parenthosts";
255 query1.Type = DbQueryInsert;
256 query1.Category = DbCatConfig;
257 query1.Fields = fields1;
258 queries.emplace_back(std::move(query1));
261 DbObject::OnMultipleQueries(queries);
263 /* host dependencies */
264 Log(LogDebug, "HostDbObject")
265 << "host dependencies for '" << host->GetName() << "'";
270 query3.Table = GetType()->GetTable() + "dependencies";
271 query3.Type = DbQueryDelete;
272 query3.Category = DbCatConfig;
273 query3.WhereCriteria = new Dictionary();
274 query3.WhereCriteria->Set("dependent_host_object_id", host);
275 queries.emplace_back(std::move(query3));
277 for (const Dependency::Ptr& dep : host->GetDependencies()) {
278 Checkable::Ptr parent = dep->GetParent();
281 Log(LogDebug, "HostDbObject")
282 << "Missing parent for dependency '" << dep->GetName() << "'.";
286 int state_filter = dep->GetStateFilter();
288 Log(LogDebug, "HostDbObject")
289 << "parent host: " << parent->GetName();
291 Dictionary::Ptr fields2 = new Dictionary();
292 fields2->Set("host_object_id", parent);
293 fields2->Set("dependent_host_object_id", host);
294 fields2->Set("inherits_parent", 1);
295 fields2->Set("timeperiod_object_id", dep->GetPeriod());
296 fields2->Set("fail_on_up", (state_filter & StateFilterUp) ? 1 : 0);
297 fields2->Set("fail_on_down", (state_filter & StateFilterDown) ? 1 : 0);
298 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
301 query2.Table = GetType()->GetTable() + "dependencies";
302 query2.Type = DbQueryInsert;
303 query2.Category = DbCatConfig;
304 query2.Fields = fields2;
305 queries.emplace_back(std::move(query2));
308 DbObject::OnMultipleQueries(queries);
310 Log(LogDebug, "HostDbObject")
311 << "host contacts: " << host->GetName();
316 query4.Table = GetType()->GetTable() + "_contacts";
317 query4.Type = DbQueryDelete;
318 query4.Category = DbCatConfig;
319 query4.WhereCriteria = new Dictionary();
320 query4.WhereCriteria->Set("host_id", DbValue::FromObjectInsertID(host));
321 queries.emplace_back(std::move(query4));
323 for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
324 Log(LogDebug, "HostDbObject")
325 << "host contacts: " << user->GetName();
327 Dictionary::Ptr fields_contact = new Dictionary();
328 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
329 fields_contact->Set("contact_object_id", user);
330 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
332 DbQuery query_contact;
333 query_contact.Table = GetType()->GetTable() + "_contacts";
334 query_contact.Type = DbQueryInsert;
335 query_contact.Category = DbCatConfig;
336 query_contact.Fields = fields_contact;
337 queries.emplace_back(std::move(query_contact));
340 DbObject::OnMultipleQueries(queries);
342 Log(LogDebug, "HostDbObject")
343 << "host contactgroups: " << host->GetName();
348 query5.Table = GetType()->GetTable() + "_contactgroups";
349 query5.Type = DbQueryDelete;
350 query5.Category = DbCatConfig;
351 query5.WhereCriteria = new Dictionary();
352 query5.WhereCriteria->Set("host_id", DbValue::FromObjectInsertID(host));
353 queries.emplace_back(std::move(query5));
355 for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
356 Log(LogDebug, "HostDbObject")
357 << "host contactgroups: " << usergroup->GetName();
359 Dictionary::Ptr fields_contact = new Dictionary();
360 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
361 fields_contact->Set("contactgroup_object_id", usergroup);
362 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
364 DbQuery query_contact;
365 query_contact.Table = GetType()->GetTable() + "_contactgroups";
366 query_contact.Type = DbQueryInsert;
367 query_contact.Category = DbCatConfig;
368 query_contact.Fields = fields_contact;
369 queries.emplace_back(std::move(query_contact));
372 DbObject::OnMultipleQueries(queries);
374 DoCommonConfigUpdate();
377 void HostDbObject::OnConfigUpdateLight()
379 DoCommonConfigUpdate();
382 void HostDbObject::DoCommonConfigUpdate()
384 Host::Ptr host = static_pointer_cast<Host>(GetObject());
386 /* update comments and downtimes on config change */
387 DbEvents::AddComments(host);
388 DbEvents::AddDowntimes(host);
391 String HostDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
393 String hashData = DbObject::CalculateConfigHash(configFields);
395 Host::Ptr host = static_pointer_cast<Host>(GetObject());
397 Array::Ptr groups = host->GetGroups();
400 hashData += DbObject::HashValue(groups);
402 Array::Ptr parents = new Array();
405 for (const Checkable::Ptr& checkable : host->GetParents()) {
406 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
411 parents->Add(parent->GetName());
416 hashData += DbObject::HashValue(parents);
418 Array::Ptr dependencies = new Array();
421 for (const Dependency::Ptr& dep : host->GetDependencies()) {
422 Checkable::Ptr parent = dep->GetParent();
427 Array::Ptr depInfo = new Array();
428 depInfo->Add(parent->GetName());
429 depInfo->Add(dep->GetStateFilter());
430 depInfo->Add(dep->GetPeriodRaw());
432 dependencies->Add(depInfo);
435 dependencies->Sort();
437 hashData += DbObject::HashValue(dependencies);
439 Array::Ptr users = new Array();
441 for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
442 users->Add(user->GetName());
447 hashData += DbObject::HashValue(users);
449 Array::Ptr userGroups = new Array();
451 for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
452 userGroups->Add(usergroup->GetName());
457 hashData += DbObject::HashValue(userGroups);
459 return SHA256(hashData);