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