]> granicus.if.org Git - icinga2/blob - lib/db_ido/hostdbobject.cpp
Move CompatUtility::GetCheckableCheckType() into IDO/Livestatus features
[icinga2] / lib / db_ido / hostdbobject.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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"
36
37 using namespace icinga;
38
39 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
40
41 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
42         : DbObject(type, name1, name2)
43 { }
44
45 Dictionary::Ptr HostDbObject::GetConfigFields() const
46 {
47         Dictionary::Ptr fields = new Dictionary();
48         Host::Ptr host = static_pointer_cast<Host>(GetObject());
49
50         /* Compatibility fallback. */
51         String displayName = host->GetDisplayName();
52
53         if (!displayName.IsEmpty()) {
54                 fields->Set("alias", displayName);
55         } else {
56                 fields->Set("alias", host->GetName());
57         }
58
59         fields->Set("display_name", displayName);
60         fields->Set("address", host->GetAddress());
61         fields->Set("address6", host->GetAddress6());
62
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());
73
74         fields->Set("first_notification_delay", Empty);
75
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));
79
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));
83
84         fields->Set("stalk_on_up", Empty);
85         fields->Set("stalk_on_down", Empty);
86         fields->Set("stalk_on_unreachable", Empty);
87
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));
94
95         fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(host));
96
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));
102
103         fields->Set("retain_status_information", 1);
104         fields->Set("retain_nonstatus_information", 1);
105
106         fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
107
108         fields->Set("obsess_over_host", 0);
109         fields->Set("failure_prediction_enabled", 0);
110
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());
116
117         return fields;
118 }
119
120 Dictionary::Ptr HostDbObject::GetStatusFields() const
121 {
122         Dictionary::Ptr fields = new Dictionary();
123         Host::Ptr host = static_pointer_cast<Host>(GetObject());
124
125         CheckResult::Ptr cr = host->GetLastCheckResult();
126
127         if (cr) {
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());
132         }
133
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());
139
140         if (cr)
141                 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
142
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));
165
166         if (cr) {
167                 fields->Set("latency", Convert::ToString(cr->CalculateLatency()));
168                 fields->Set("execution_time", Convert::ToString(cr->CalculateExecutionTime()));
169         }
170
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));
181
182         fields->Set("original_attributes", JsonEncode(host->GetOriginalAttributes()));
183
184         return fields;
185 }
186
187 void HostDbObject::OnConfigUpdateHeavy()
188 {
189         Host::Ptr host = static_pointer_cast<Host>(GetObject());
190
191         /* groups */
192         Array::Ptr groups = host->GetGroups();
193
194         std::vector<DbQuery> queries;
195
196         DbQuery query1;
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));
203
204         if (groups) {
205                 ObjectLock olock(groups);
206                 for (const String& groupName : groups) {
207                         HostGroup::Ptr group = HostGroup::GetByName(groupName);
208
209                         DbQuery query2;
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));
222                 }
223         }
224
225         DbObject::OnMultipleQueries(queries);
226
227         queries.clear();
228
229         DbQuery query2;
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));
236
237         /* parents */
238         for (const Checkable::Ptr& checkable : host->GetParents()) {
239                 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
240
241                 if (!parent)
242                         continue;
243
244                 Log(LogDebug, "HostDbObject")
245                         << "host parents: " << parent->GetName();
246
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 */
252
253                 DbQuery query1;
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));
259         }
260
261         DbObject::OnMultipleQueries(queries);
262
263         /* host dependencies */
264         Log(LogDebug, "HostDbObject")
265                 << "host dependencies for '" << host->GetName() << "'";
266
267         queries.clear();
268
269         DbQuery query3;
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));
276
277         for (const Dependency::Ptr& dep : host->GetDependencies()) {
278                 Checkable::Ptr parent = dep->GetParent();
279
280                 if (!parent) {
281                         Log(LogDebug, "HostDbObject")
282                                 << "Missing parent for dependency '" << dep->GetName() << "'.";
283                         continue;
284                 }
285
286                 int state_filter = dep->GetStateFilter();
287
288                 Log(LogDebug, "HostDbObject")
289                         << "parent host: " << parent->GetName();
290
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 */
299
300                 DbQuery query2;
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));
306         }
307
308         DbObject::OnMultipleQueries(queries);
309
310         Log(LogDebug, "HostDbObject")
311                 << "host contacts: " << host->GetName();
312
313         queries.clear();
314
315         DbQuery query4;
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));
322
323         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
324                 Log(LogDebug, "HostDbObject")
325                         << "host contacts: " << user->GetName();
326
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 */
331
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));
338         }
339
340         DbObject::OnMultipleQueries(queries);
341
342         Log(LogDebug, "HostDbObject")
343                 << "host contactgroups: " << host->GetName();
344
345         queries.clear();
346
347         DbQuery query5;
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));
354
355         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
356                 Log(LogDebug, "HostDbObject")
357                         << "host contactgroups: " << usergroup->GetName();
358
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 */
363
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));
370         }
371
372         DbObject::OnMultipleQueries(queries);
373
374         DoCommonConfigUpdate();
375 }
376
377 void HostDbObject::OnConfigUpdateLight()
378 {
379         DoCommonConfigUpdate();
380 }
381
382 void HostDbObject::DoCommonConfigUpdate()
383 {
384         Host::Ptr host = static_pointer_cast<Host>(GetObject());
385
386         /* update comments and downtimes on config change */
387         DbEvents::AddComments(host);
388         DbEvents::AddDowntimes(host);
389 }
390
391 String HostDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
392 {
393         String hashData = DbObject::CalculateConfigHash(configFields);
394
395         Host::Ptr host = static_pointer_cast<Host>(GetObject());
396
397         Array::Ptr groups = host->GetGroups();
398
399         if (groups)
400                 hashData += DbObject::HashValue(groups);
401
402         Array::Ptr parents = new Array();
403
404         /* parents */
405         for (const Checkable::Ptr& checkable : host->GetParents()) {
406                 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
407
408                 if (!parent)
409                         continue;
410
411                 parents->Add(parent->GetName());
412         }
413
414         parents->Sort();
415
416         hashData += DbObject::HashValue(parents);
417
418         Array::Ptr dependencies = new Array();
419
420         /* dependencies */
421         for (const Dependency::Ptr& dep : host->GetDependencies()) {
422                 Checkable::Ptr parent = dep->GetParent();
423
424                 if (!parent)
425                         continue;
426
427                 Array::Ptr depInfo = new Array();
428                 depInfo->Add(parent->GetName());
429                 depInfo->Add(dep->GetStateFilter());
430                 depInfo->Add(dep->GetPeriodRaw());
431
432                 dependencies->Add(depInfo);
433         }
434
435         dependencies->Sort();
436
437         hashData += DbObject::HashValue(dependencies);
438
439         Array::Ptr users = new Array();
440
441         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
442                 users->Add(user->GetName());
443         }
444
445         users->Sort();
446
447         hashData += DbObject::HashValue(users);
448
449         Array::Ptr userGroups = new Array();
450
451         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
452                 userGroups->Add(usergroup->GetName());
453         }
454
455         userGroups->Sort();
456
457         hashData += DbObject::HashValue(userGroups);
458
459         return SHA256(hashData);
460 }