]> granicus.if.org Git - icinga2/blob - lib/db_ido/hostdbobject.cpp
Refactor CompatUtility::* for DB IDO, Livestatus, StatusData.
[icinga2] / lib / db_ido / hostdbobject.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
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.h"
21 #include "db_ido/dbtype.h"
22 #include "db_ido/dbvalue.h"
23 #include "icinga/host.h"
24 #include "icinga/service.h"
25 #include "icinga/notification.h"
26 #include "icinga/checkcommand.h"
27 #include "icinga/eventcommand.h"
28 #include "icinga/compatutility.h"
29 #include "base/convert.h"
30 #include "base/objectlock.h"
31 #include <boost/foreach.hpp>
32
33 using namespace icinga;
34
35 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
36
37 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
38         : DbObject(type, name1, name2)
39 { }
40
41 Dictionary::Ptr HostDbObject::GetConfigFields(void) const
42 {
43         Dictionary::Ptr fields = make_shared<Dictionary>();
44         Host::Ptr host = static_pointer_cast<Host>(GetObject());
45
46         Service::Ptr service = host->GetCheckService();
47
48         fields->Set("alias", CompatUtility::GetHostAlias(host));
49         fields->Set("display_name", host->GetDisplayName());
50         fields->Set("address", CompatUtility::GetHostAddress(host));
51         fields->Set("address6", CompatUtility::GetHostAddress6(host));
52
53         if (service) {
54                 fields->Set("check_command_object_id", service->GetCheckCommand());
55                 fields->Set("check_command_args", Empty);
56                 fields->Set("eventhandler_command_object_id", service->GetEventCommand());
57                 fields->Set("eventhandler_command_args", Empty);
58                 fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetServiceNotificationNotificationPeriod(service)));
59                 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
60                 fields->Set("failure_prediction_options", Empty);
61                 fields->Set("check_interval", CompatUtility::GetServiceCheckInterval(service));
62                 fields->Set("retry_interval", CompatUtility::GetServiceRetryInterval(service));
63                 fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
64
65                 fields->Set("first_notification_delay", Empty);
66
67                 fields->Set("notification_interval", CompatUtility::GetServiceNotificationNotificationInterval(service));
68                 /* requires host check service */
69                 fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
70                 fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
71
72                 fields->Set("notify_on_recovery", CompatUtility::GetServiceNotifyOnRecovery(service));
73                 fields->Set("notify_on_flapping", CompatUtility::GetServiceNotifyOnFlapping(service));
74                 fields->Set("notify_on_downtime", CompatUtility::GetServiceNotifyOnDowntime(service));
75
76                 fields->Set("stalk_on_up", Empty);
77                 fields->Set("stalk_on_down", Empty);
78                 fields->Set("stalk_on_unreachable", Empty);
79
80                 fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service));
81                 fields->Set("flap_detection_on_up", Empty);
82                 fields->Set("flap_detection_on_down", Empty);
83                 fields->Set("flap_detection_on_unreachable", Empty);
84                 fields->Set("low_flap_threshold", CompatUtility::GetServiceLowFlapThreshold(service));
85                 fields->Set("high_flap_threshold", CompatUtility::GetServiceHighFlapThreshold(service));
86         }
87
88         fields->Set("process_performance_data", 0);
89
90         if (service) {
91                 fields->Set("freshness_checks_enabled", CompatUtility::GetServiceFreshnessChecksEnabled(service));
92                 fields->Set("freshness_threshold", CompatUtility::GetServiceFreshnessThreshold(service));
93                 fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service));
94                 fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service));
95                 fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service));
96         }
97
98         fields->Set("retain_status_information", 1);
99         fields->Set("retain_nonstatus_information", 1);
100
101         if (service)
102                 fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service));
103
104         fields->Set("obsess_over_host", 0);
105         fields->Set("failure_prediction_enabled", 0);
106
107         fields->Set("notes", CompatUtility::GetCustomAttributeConfig(host, "notes"));
108         fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(host, "notes_url"));
109         fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(host, "action_url"));
110         fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(host, "icon_image"));
111         fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt"));
112         fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"));
113
114         Host2dCoords coords = CompatUtility::GetHost2dCoords(host);
115
116         fields->Set("have_2d_coords", coords.have_2d_coords);
117
118         if (coords.have_2d_coords) {
119                 fields->Set("x_2d", coords.x_2d);
120                 fields->Set("y_2d", coords.y_2d);
121         }
122
123         /* deprecated in 1.x */
124         fields->Set("have_3d_coords", 0);
125
126         return fields;
127 }
128
129 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
130 {
131         Dictionary::Ptr fields = make_shared<Dictionary>();
132         Host::Ptr host = static_pointer_cast<Host>(GetObject());
133         Service::Ptr service = host->GetCheckService();
134
135         /* fetch service status, or dump a pending hoststatus */
136         if (service) {
137                 CheckResult::Ptr cr = service->GetLastCheckResult();
138
139                 if (cr) {
140                         fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
141                         fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
142                         fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
143                         fields->Set("check_source", cr->GetCheckSource());
144                 }
145
146                 fields->Set("current_state", host->GetState());
147                 fields->Set("has_been_checked", CompatUtility::GetServiceHasBeenChecked(service));
148                 fields->Set("should_be_scheduled", CompatUtility::GetServiceShouldBeScheduled(service));
149                 fields->Set("current_check_attempt", service->GetCheckAttempt());
150                 fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
151
152                 if (cr)
153                         fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
154
155                 fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
156                 fields->Set("check_type", CompatUtility::GetServiceCheckType(service));
157                 fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
158                 fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
159                 fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
160                 fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
161                 fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
162                 fields->Set("state_type", service->GetStateType());
163                 fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationLastNotification(service)));
164                 fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationNextNotification(service)));
165                 fields->Set("no_more_notifications", Empty);
166                 fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service));
167                 fields->Set("problem_has_been_acknowledged", CompatUtility::GetServiceProblemHasBeenAcknowledged(service));
168                 fields->Set("acknowledgement_type", CompatUtility::GetServiceAcknowledgementType(service));
169                 fields->Set("current_notification_number", CompatUtility::GetServiceNotificationNotificationNumber(service));
170                 fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service));
171                 fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service));
172                 fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service));
173                 fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service));
174                 fields->Set("is_flapping", CompatUtility::GetServiceIsFlapping(service));
175                 fields->Set("percent_state_change", CompatUtility::GetServicePercentStateChange(service));
176
177                 if (cr) {
178                         fields->Set("latency", Service::CalculateLatency(cr));
179                         fields->Set("execution_time", Service::CalculateExecutionTime(cr));
180                 }
181                 fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
182                 fields->Set("failure_prediction_enabled", Empty);
183                 fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */
184                 fields->Set("obsess_over_host", Empty);
185                 fields->Set("modified_host_attributes", service->GetModifiedAttributes());
186                 fields->Set("event_handler", CompatUtility::GetServiceEventHandler(service));
187                 fields->Set("check_command", CompatUtility::GetServiceCheckCommand(service));
188                 fields->Set("normal_check_interval", CompatUtility::GetServiceCheckInterval(service));
189                 fields->Set("retry_check_interval", CompatUtility::GetServiceRetryInterval(service));
190                 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
191         }
192         else {
193                 fields->Set("has_been_checked", 0);
194                 fields->Set("last_check", DbValue::FromTimestamp(0));
195                 fields->Set("next_check", DbValue::FromTimestamp(0));
196                 fields->Set("active_checks_enabled", 0);
197         }
198
199         return fields;
200 }
201
202 void HostDbObject::OnConfigUpdate(void)
203 {
204         Host::Ptr host = static_pointer_cast<Host>(GetObject());
205
206         /* parents, host dependencies */
207         BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
208                 Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
209
210                 /* parents: host_id, parent_host_object_id */
211                 Dictionary::Ptr fields1 = make_shared<Dictionary>();
212                 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
213                 fields1->Set("parent_host_object_id", parent);
214                 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
215
216                 DbQuery query1;
217                 query1.Table = GetType()->GetTable() + "_parenthosts";
218                 query1.Type = DbQueryInsert;
219                 query1.Category = DbCatConfig;
220                 query1.Fields = fields1;
221                 OnQuery(query1);
222
223                 /* host dependencies */
224                 Dictionary::Ptr fields2 = make_shared<Dictionary>();
225                 fields2->Set("host_object_id", parent);
226                 fields2->Set("dependent_host_object_id", host);
227                 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
228
229                 DbQuery query2;
230                 query2.Table = GetType()->GetTable() + "dependencies";
231                 query2.Type = DbQueryInsert;
232                 query2.Category = DbCatConfig;
233                 query2.Fields = fields2;
234                 OnQuery(query2);
235         }
236
237         /* host contacts, contactgroups */
238         Service::Ptr service = host->GetCheckService();
239
240         if (service) {
241                 Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
242
243                 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
244                         Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
245
246                         Dictionary::Ptr fields_contact = make_shared<Dictionary>();
247                         fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
248                         fields_contact->Set("contact_object_id", user);
249                         fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
250
251                         DbQuery query_contact;
252                         query_contact.Table = GetType()->GetTable() + "_contacts";
253                         query_contact.Type = DbQueryInsert;
254                         query_contact.Category = DbCatConfig;
255                         query_contact.Fields = fields_contact;
256                         OnQuery(query_contact);
257                 }
258
259                 Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
260
261                 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
262                         Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
263
264                         Dictionary::Ptr fields_contact = make_shared<Dictionary>();
265                         fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
266                         fields_contact->Set("contactgroup_object_id", usergroup);
267                         fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
268
269                         DbQuery query_contact;
270                         query_contact.Table = GetType()->GetTable() + "_contactgroups";
271                         query_contact.Type = DbQueryInsert;
272                         query_contact.Category = DbCatConfig;
273                         query_contact.Fields = fields_contact;
274                         OnQuery(query_contact);
275                 }
276         }
277
278         /* custom variables */
279         Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'");
280
281         Dictionary::Ptr customvars;
282         {
283                 ObjectLock olock(host);
284                 customvars = CompatUtility::GetCustomVariableConfig(host);
285         }
286
287         if (customvars) {
288                 ObjectLock olock (customvars);
289
290                 BOOST_FOREACH(const Dictionary::Pair& kv, customvars) {
291                         Log(LogDebug, "db_ido", "host customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) + "'");
292
293                         Dictionary::Ptr fields3 = make_shared<Dictionary>();
294                         fields3->Set("varname", Convert::ToString(kv.first));
295                         fields3->Set("varvalue", Convert::ToString(kv.second));
296                         fields3->Set("config_type", 1);
297                         fields3->Set("has_been_modified", 0);
298                         fields3->Set("object_id", host);
299                         fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
300
301                         DbQuery query3;
302                         query3.Table = "customvariables";
303                         query3.Type = DbQueryInsert;
304                         query3.Category = DbCatConfig;
305                         query3.Fields = fields3;
306                         OnQuery(query3);
307                 }
308         }
309 }
310
311 void HostDbObject::OnStatusUpdate(void)
312 {
313 }