]> granicus.if.org Git - icinga2/blob - lib/db_ido/servicedbobject.cpp
2c82e8b23f72edc0392b51bd8b1c7e72057c7107
[icinga2] / lib / db_ido / servicedbobject.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/servicedbobject.hpp"
21 #include "db_ido/servicegroupdbobject.hpp"
22 #include "db_ido/dbtype.hpp"
23 #include "db_ido/dbvalue.hpp"
24 #include "db_ido/dbevents.hpp"
25 #include "icinga/notification.hpp"
26 #include "icinga/dependency.hpp"
27 #include "icinga/checkcommand.hpp"
28 #include "icinga/eventcommand.hpp"
29 #include "icinga/externalcommandprocessor.hpp"
30 #include "icinga/compatutility.hpp"
31 #include "icinga/icingaapplication.hpp"
32 #include "remote/endpoint.hpp"
33 #include "base/convert.hpp"
34 #include "base/objectlock.hpp"
35 #include "base/initialize.hpp"
36 #include "base/configtype.hpp"
37 #include "base/utility.hpp"
38 #include "base/logger.hpp"
39 #include "base/json.hpp"
40 #include <boost/algorithm/string/join.hpp>
41
42 using namespace icinga;
43
44 REGISTER_DBTYPE(Service, "service", DbObjectTypeService, "service_object_id", ServiceDbObject);
45
46 ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
47         : DbObject(type, name1, name2)
48 { }
49
50 Dictionary::Ptr ServiceDbObject::GetConfigFields() const
51 {
52         Dictionary::Ptr fields = new Dictionary();
53         Service::Ptr service = static_pointer_cast<Service>(GetObject());
54         Host::Ptr host = service->GetHost();
55
56         fields->Set("host_object_id", host);
57         fields->Set("display_name", service->GetDisplayName());
58         fields->Set("check_command_object_id", service->GetCheckCommand());
59         fields->Set("check_command_args", CompatUtility::GetCheckableCommandArgs(service));
60         fields->Set("eventhandler_command_object_id", service->GetEventCommand());
61         fields->Set("eventhandler_command_args", Empty);
62         fields->Set("notification_timeperiod_object_id", Empty);
63         fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
64         fields->Set("failure_prediction_options", Empty);
65         fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(service));
66         fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(service));
67         fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
68         fields->Set("first_notification_delay", Empty);
69         fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(service));
70         fields->Set("notify_on_warning", CompatUtility::GetCheckableNotifyOnWarning(service));
71         fields->Set("notify_on_unknown", CompatUtility::GetCheckableNotifyOnUnknown(service));
72         fields->Set("notify_on_critical", CompatUtility::GetCheckableNotifyOnCritical(service));
73         fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(service));
74         fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(service));
75         fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(service));
76         fields->Set("stalk_on_ok", 0);
77         fields->Set("stalk_on_warning", 0);
78         fields->Set("stalk_on_unknown", 0);
79         fields->Set("stalk_on_critical", 0);
80         fields->Set("is_volatile", CompatUtility::GetCheckableIsVolatile(service));
81         fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service));
82         fields->Set("flap_detection_on_ok", Empty);
83         fields->Set("flap_detection_on_warning", Empty);
84         fields->Set("flap_detection_on_unknown", Empty);
85         fields->Set("flap_detection_on_critical", Empty);
86         fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(service));
87         fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(service));
88         fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service));
89         fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(service));
90         fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(service));
91         fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service));
92         fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service));
93         fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service));
94         fields->Set("retain_status_information", Empty);
95         fields->Set("retain_nonstatus_information", Empty);
96         fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service));
97         fields->Set("obsess_over_service", Empty);
98         fields->Set("failure_prediction_enabled", Empty);
99         fields->Set("notes", service->GetNotes());
100         fields->Set("notes_url", service->GetNotesUrl());
101         fields->Set("action_url", service->GetActionUrl());
102         fields->Set("icon_image", service->GetIconImage());
103         fields->Set("icon_image_alt", service->GetIconImageAlt());
104
105         return fields;
106 }
107
108 Dictionary::Ptr ServiceDbObject::GetStatusFields() const
109 {
110         Dictionary::Ptr fields = new Dictionary();
111         Service::Ptr service = static_pointer_cast<Service>(GetObject());
112         CheckResult::Ptr cr = service->GetLastCheckResult();
113
114         if (cr) {
115                 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
116                 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
117                 fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
118                 fields->Set("check_source", cr->GetCheckSource());
119         }
120
121         fields->Set("current_state", service->GetState());
122         fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(service));
123         fields->Set("should_be_scheduled", service->GetEnableActiveChecks());
124         fields->Set("current_check_attempt", service->GetCheckAttempt());
125         fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
126
127         if (cr)
128                 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
129
130         fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
131         fields->Set("check_type", CompatUtility::GetCheckableCheckType(service));
132         fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
133         fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
134         fields->Set("last_hard_state", service->GetLastHardState());
135         fields->Set("last_time_ok", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateOK())));
136         fields->Set("last_time_warning", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateWarning())));
137         fields->Set("last_time_critical", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateCritical())));
138         fields->Set("last_time_unknown", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateUnknown())));
139         fields->Set("state_type", service->GetStateType());
140         fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(service)));
141         fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(service)));
142         fields->Set("no_more_notifications", Empty);
143         fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service));
144         fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(service));
145         fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(service));
146         fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(service));
147         fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service));
148         fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service));
149         fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service));
150         fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service));
151         fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(service));
152         fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(service));
153
154         if (cr) {
155                 fields->Set("latency", Convert::ToString(cr->CalculateLatency()));
156                 fields->Set("execution_time", Convert::ToString(cr->CalculateExecutionTime()));
157         }
158
159         fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
160         fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service));
161         fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(service));
162         fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(service));
163         fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(service));
164         fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(service));
165         fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
166         fields->Set("is_reachable", CompatUtility::GetCheckableIsReachable(service));
167
168         fields->Set("original_attributes", JsonEncode(service->GetOriginalAttributes()));
169
170         return fields;
171 }
172
173 void ServiceDbObject::OnConfigUpdateHeavy()
174 {
175         Service::Ptr service = static_pointer_cast<Service>(GetObject());
176
177         /* groups */
178         Array::Ptr groups = service->GetGroups();
179
180         std::vector<DbQuery> queries;
181
182         DbQuery query1;
183         query1.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members";
184         query1.Type = DbQueryDelete;
185         query1.Category = DbCatConfig;
186         query1.WhereCriteria = new Dictionary();
187         query1.WhereCriteria->Set("service_object_id", service);
188         queries.emplace_back(std::move(query1));
189
190         if (groups) {
191                 ObjectLock olock(groups);
192                 for (const String& groupName : groups) {
193                         ServiceGroup::Ptr group = ServiceGroup::GetByName(groupName);
194
195                         DbQuery query2;
196                         query2.Table = DbType::GetByName("ServiceGroup")->GetTable() + "_members";
197                         query2.Type = DbQueryInsert;
198                         query2.Category = DbCatConfig;
199                         query2.Fields = new Dictionary();
200                         query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
201                         query2.Fields->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
202                         query2.Fields->Set("service_object_id", service);
203                         query2.WhereCriteria = new Dictionary();
204                         query2.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
205                         query2.WhereCriteria->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
206                         query2.WhereCriteria->Set("service_object_id", service);
207                         queries.emplace_back(std::move(query2));
208                 }
209         }
210
211         DbObject::OnMultipleQueries(queries);
212
213         /* service dependencies */
214         Log(LogDebug, "ServiceDbObject")
215                 << "service dependencies for '" << service->GetName() << "'";
216
217         queries.clear();
218
219         DbQuery query2;
220         query2.Table = GetType()->GetTable() + "dependencies";
221         query2.Type = DbQueryDelete;
222         query2.Category = DbCatConfig;
223         query2.WhereCriteria = new Dictionary();
224         query2.WhereCriteria->Set("dependent_service_object_id", service);
225         queries.emplace_back(std::move(query2));
226
227         for (const Dependency::Ptr& dep : service->GetDependencies()) {
228                 Checkable::Ptr parent = dep->GetParent();
229
230                 if (!parent) {
231                         Log(LogDebug, "ServiceDbObject")
232                                 << "Missing parent for dependency '" << dep->GetName() << "'.";
233                         continue;
234                 }
235
236                 Log(LogDebug, "ServiceDbObject")
237                         << "service parents: " << parent->GetName();
238
239                 int state_filter = dep->GetStateFilter();
240
241                 /* service dependencies */
242                 Dictionary::Ptr fields1 = new Dictionary();
243                 fields1->Set("service_object_id", parent);
244                 fields1->Set("dependent_service_object_id", service);
245                 fields1->Set("inherits_parent", 1);
246                 fields1->Set("timeperiod_object_id", dep->GetPeriod());
247                 fields1->Set("fail_on_ok", (state_filter & StateFilterOK) ? 1 : 0);
248                 fields1->Set("fail_on_warning", (state_filter & StateFilterWarning) ? 1 : 0);
249                 fields1->Set("fail_on_critical", (state_filter & StateFilterCritical) ? 1 : 0);
250                 fields1->Set("fail_on_unknown", (state_filter & StateFilterUnknown) ? 1 : 0);
251                 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
252
253                 DbQuery query1;
254                 query1.Table = GetType()->GetTable() + "dependencies";
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         /* service contacts, contactgroups */
264         Log(LogDebug, "ServiceDbObject")
265                 << "service contacts: " << service->GetName();
266
267         queries.clear();
268
269         DbQuery query3;
270         query3.Table = GetType()->GetTable() + "_contacts";
271         query3.Type = DbQueryDelete;
272         query3.Category = DbCatConfig;
273         query3.WhereCriteria = new Dictionary();
274         query3.WhereCriteria->Set("service_id", DbValue::FromObjectInsertID(service));
275         queries.emplace_back(std::move(query3));
276
277         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
278                 Log(LogDebug, "ServiceDbObject")
279                         << "service contacts: " << user->GetName();
280
281                 Dictionary::Ptr fields_contact = new Dictionary();
282                 fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
283                 fields_contact->Set("contact_object_id", user);
284                 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
285
286                 DbQuery query_contact;
287                 query_contact.Table = GetType()->GetTable() + "_contacts";
288                 query_contact.Type = DbQueryInsert;
289                 query_contact.Category = DbCatConfig;
290                 query_contact.Fields = fields_contact;
291                 queries.emplace_back(std::move(query_contact));
292         }
293
294         DbObject::OnMultipleQueries(queries);
295
296         Log(LogDebug, "ServiceDbObject")
297                 << "service contactgroups: " << service->GetName();
298
299         queries.clear();
300
301         DbQuery query4;
302         query4.Table = GetType()->GetTable() + "_contactgroups";
303         query4.Type = DbQueryDelete;
304         query4.Category = DbCatConfig;
305         query4.WhereCriteria = new Dictionary();
306         query4.WhereCriteria->Set("service_id", DbValue::FromObjectInsertID(service));
307         queries.emplace_back(std::move(query4));
308
309         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
310                 Log(LogDebug, "ServiceDbObject")
311                         << "service contactgroups: " << usergroup->GetName();
312
313                 Dictionary::Ptr fields_contact = new Dictionary();
314                 fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
315                 fields_contact->Set("contactgroup_object_id", usergroup);
316                 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
317
318                 DbQuery query_contact;
319                 query_contact.Table = GetType()->GetTable() + "_contactgroups";
320                 query_contact.Type = DbQueryInsert;
321                 query_contact.Category = DbCatConfig;
322                 query_contact.Fields = fields_contact;
323                 queries.emplace_back(std::move(query_contact));
324         }
325
326         DbObject::OnMultipleQueries(queries);
327
328         DoCommonConfigUpdate();
329 }
330
331 void ServiceDbObject::OnConfigUpdateLight()
332 {
333         DoCommonConfigUpdate();
334 }
335
336 void ServiceDbObject::DoCommonConfigUpdate()
337 {
338         Service::Ptr service = static_pointer_cast<Service>(GetObject());
339
340         /* update comments and downtimes on config change */
341         DbEvents::AddComments(service);
342         DbEvents::AddDowntimes(service);
343 }
344
345 String ServiceDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
346 {
347         String hashData = DbObject::CalculateConfigHash(configFields);
348
349         Service::Ptr service = static_pointer_cast<Service>(GetObject());
350
351         Array::Ptr groups = service->GetGroups();
352
353         if (groups)
354                 hashData += DbObject::HashValue(groups);
355
356         Array::Ptr dependencies = new Array();
357
358         /* dependencies */
359         for (const Dependency::Ptr& dep : service->GetDependencies()) {
360                 Checkable::Ptr parent = dep->GetParent();
361
362                 if (!parent)
363                         continue;
364
365                 Array::Ptr depInfo = new Array();
366                 depInfo->Add(parent->GetName());
367                 depInfo->Add(dep->GetStateFilter());
368                 depInfo->Add(dep->GetPeriodRaw());
369
370                 dependencies->Add(depInfo);
371         }
372
373         dependencies->Sort();
374
375         hashData += DbObject::HashValue(dependencies);
376
377         Array::Ptr users = new Array();
378
379         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
380                 users->Add(user->GetName());
381         }
382
383         users->Sort();
384
385         hashData += DbObject::HashValue(users);
386
387         Array::Ptr userGroups = new Array();
388
389         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
390                 userGroups->Add(usergroup->GetName());
391         }
392
393         userGroups->Sort();
394
395         hashData += DbObject::HashValue(userGroups);
396
397         return SHA256(hashData);
398 }