]> granicus.if.org Git - icinga2/blob - lib/db_ido/hostdbobject.cpp
db_ido: Fix modified_{host,service}_attributes columns.
[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 #include <boost/tuple/tuple.hpp>
33
34 using namespace icinga;
35
36 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
37
38 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
39         : DbObject(type, name1, name2)
40 { }
41
42 Dictionary::Ptr HostDbObject::GetConfigFields(void) const
43 {
44         Dictionary::Ptr fields = boost::make_shared<Dictionary>();
45         Host::Ptr host = static_pointer_cast<Host>(GetObject());
46
47         Service::Ptr service = host->GetCheckService();
48
49         Dictionary::Ptr attrs;
50
51         {
52                 ObjectLock olock(host);
53                 attrs = CompatUtility::GetHostConfigAttributes(host);
54         }
55
56         fields->Set("alias", attrs->Get("alias"));
57         fields->Set("display_name", attrs->Get("display_name"));
58
59         fields->Set("address", attrs->Get("address"));
60         fields->Set("address6", attrs->Get("address6"));
61
62         if (service) {
63                 fields->Set("check_command_object_id", service->GetCheckCommand());
64                 fields->Set("check_command_args", Empty);
65                 fields->Set("eventhandler_command_object_id", service->GetEventCommand());
66                 fields->Set("eventhandler_command_args", Empty);
67         }
68
69         fields->Set("notification_timeperiod_object_id", Notification::GetByName(attrs->Get("notification_period")));
70
71         if (service)
72                 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
73
74         fields->Set("failure_prediction_options", Empty);
75
76         fields->Set("check_interval", attrs->Get("check_interval"));
77         fields->Set("retry_interval", attrs->Get("retry_interval"));
78         fields->Set("max_check_attempts", attrs->Get("max_check_attempts"));
79
80         fields->Set("first_notification_delay", Empty);
81         fields->Set("notification_interval", attrs->Get("notification_interval"));
82         fields->Set("notify_on_down", attrs->Get("notify_on_down"));
83         fields->Set("notify_on_unreachable", attrs->Get("notify_on_unreachable"));
84         fields->Set("notify_on_recovery", attrs->Get("notify_on_recovery"));
85         fields->Set("notify_on_flapping", attrs->Get("notify_on_flapping"));
86         fields->Set("notify_on_downtime", attrs->Get("notify_on_downtime"));
87
88         fields->Set("stalk_on_up", Empty);
89         fields->Set("stalk_on_down", Empty);
90         fields->Set("stalk_on_unreachable", Empty);
91
92         fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled"));
93         fields->Set("flap_detection_on_up", Empty);
94         fields->Set("flap_detection_on_down", Empty);
95         fields->Set("flap_detection_on_unreachable", Empty);
96         fields->Set("low_flap_threshold", attrs->Get("low_flap_threshold"));
97         fields->Set("high_flap_threshold", attrs->Get("high_flap_threshold"));
98         fields->Set("process_performance_data", attrs->Get("process_performance_data"));
99         fields->Set("freshness_checks_enabled", attrs->Get("check_freshness"));
100         fields->Set("freshness_threshold", Empty);
101         fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled"));
102         fields->Set("event_handler_enabled", attrs->Get("event_handler_enabled"));
103         fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled"));
104         fields->Set("retain_status_information", 1);
105         fields->Set("retain_nonstatus_information", 1);
106         fields->Set("notifications_enabled", attrs->Get("notifications_enabled"));
107         fields->Set("obsess_over_host", 0);
108         fields->Set("failure_prediction_enabled", 0);
109
110         fields->Set("notes", attrs->Get("notes"));
111         fields->Set("notes_url", attrs->Get("notes_url"));
112         fields->Set("action_url", attrs->Get("action_url"));
113         fields->Set("icon_image", attrs->Get("icon_image"));
114         fields->Set("icon_image_alt", attrs->Get("icon_image_alt"));
115         fields->Set("statusmap_image", attrs->Get("statusmap_image"));
116         fields->Set("have_2d_coords", attrs->Get("have_2d_coords"));
117         fields->Set("x_2d", attrs->Get("x_2d"));
118         fields->Set("y_2d", attrs->Get("y_2d"));
119         /* deprecated in 1.x */
120         fields->Set("have_3d_coords", 0);
121
122         return fields;
123 }
124
125 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
126 {
127         Dictionary::Ptr fields = boost::make_shared<Dictionary>();
128         Host::Ptr host = static_pointer_cast<Host>(GetObject());
129         Service::Ptr service = host->GetCheckService();
130
131         Dictionary::Ptr attrs;
132
133         /* fetch service status, or dump a pending hoststatus */
134         if (service) {
135                 ObjectLock olock(service);
136                 attrs = CompatUtility::GetServiceStatusAttributes(service, CompatTypeHost);
137
138                 fields->Set("output", attrs->Get("plugin_output"));
139                 fields->Set("long_output", attrs->Get("long_plugin_output"));
140                 fields->Set("perfdata", attrs->Get("performance_data"));
141                 fields->Set("check_source", attrs->Get("check_source"));
142                 fields->Set("current_state", attrs->Get("current_state"));
143                 fields->Set("has_been_checked", attrs->Get("has_been_checked"));
144                 fields->Set("should_be_scheduled", attrs->Get("should_be_scheduled"));
145                 fields->Set("current_check_attempt", attrs->Get("current_attempt"));
146                 fields->Set("max_check_attempts", attrs->Get("max_attempts"));
147                 fields->Set("last_check", DbValue::FromTimestamp(attrs->Get("last_check")));
148                 fields->Set("next_check", DbValue::FromTimestamp(attrs->Get("next_check")));
149                 fields->Set("check_type", attrs->Get("check_type"));
150                 fields->Set("last_state_change", DbValue::FromTimestamp(attrs->Get("last_state_change")));
151                 fields->Set("last_hard_state_change", DbValue::FromTimestamp(attrs->Get("last_hard_state_change")));
152                 fields->Set("last_time_up", DbValue::FromTimestamp(attrs->Get("last_time_up")));
153                 fields->Set("last_time_down", DbValue::FromTimestamp(attrs->Get("last_time_down")));
154                 fields->Set("last_time_unreachable", DbValue::FromTimestamp(attrs->Get("last_time_unreachable")));
155                 fields->Set("state_type", attrs->Get("state_type"));
156                 fields->Set("last_notification", DbValue::FromTimestamp(attrs->Get("last_notification")));
157                 fields->Set("next_notification", DbValue::FromTimestamp(attrs->Get("next_notification")));
158                 fields->Set("no_more_notifications", Empty);
159                 fields->Set("notifications_enabled", attrs->Get("notifications_enabled"));
160                 fields->Set("problem_has_been_acknowledged", attrs->Get("problem_has_been_acknowledged"));
161                 fields->Set("acknowledgement_type", attrs->Get("acknowledgement_type"));
162                 fields->Set("current_notification_number", attrs->Get("current_notification_number"));
163                 fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled"));
164                 fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled"));
165                 fields->Set("eventhandler_enabled", attrs->Get("eventhandler_enabled"));
166                 fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled"));
167                 fields->Set("is_flapping", attrs->Get("is_flapping"));
168                 fields->Set("percent_state_change", attrs->Get("percent_state_change"));
169                 fields->Set("latency", attrs->Get("check_latency"));
170                 fields->Set("execution_time", attrs->Get("check_execution_time"));
171                 fields->Set("scheduled_downtime_depth", attrs->Get("scheduled_downtime_depth"));
172                 fields->Set("failure_prediction_enabled", Empty);
173                 fields->Set("process_performance_data", attrs->Get("process_performance_data"));
174                 fields->Set("obsess_over_host", Empty);
175                 fields->Set("modified_host_attributes", attrs->Get("modified_attributes"));
176                 fields->Set("event_handler", attrs->Get("event_handler"));
177                 fields->Set("check_command", attrs->Get("check_command"));
178                 fields->Set("normal_check_interval", attrs->Get("check_interval"));
179                 fields->Set("retry_check_interval", attrs->Get("retry_interval"));
180                 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
181         }
182         else {
183                 fields->Set("has_been_checked", 0);
184                 fields->Set("last_check", DbValue::FromTimestamp(0));
185                 fields->Set("next_check", DbValue::FromTimestamp(0));
186                 fields->Set("active_checks_enabled", 0);
187         }
188
189         return fields;
190 }
191
192 void HostDbObject::OnConfigUpdate(void)
193 {
194         Host::Ptr host = static_pointer_cast<Host>(GetObject());
195
196         /* parents, host dependencies */
197         DbQuery query_del1;
198         query_del1.Table = GetType()->GetTable() + "_parenthosts";
199         query_del1.Type = DbQueryDelete;
200         query_del1.WhereCriteria = boost::make_shared<Dictionary>();
201         query_del1.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
202         OnQuery(query_del1);
203
204         DbQuery query_del2;
205         query_del2.Table = GetType()->GetTable() + "dependencies";
206         query_del2.Type = DbQueryDelete;
207         query_del2.WhereCriteria = boost::make_shared<Dictionary>();
208         query_del2.WhereCriteria->Set("dependent_host_object_id", host);
209         OnQuery(query_del2);
210
211         BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
212                 Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
213
214                 /* parents: host_id, parent_host_object_id */
215                 Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
216                 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
217                 fields1->Set("parent_host_object_id", parent);
218                 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
219
220                 DbQuery query1;
221                 query1.Table = GetType()->GetTable() + "_parenthosts";
222                 query1.Type = DbQueryInsert;
223                 query1.Fields = fields1;
224                 OnQuery(query1);
225
226                 /* host dependencies */
227                 Dictionary::Ptr fields2 = boost::make_shared<Dictionary>();
228                 fields2->Set("host_object_id", parent);
229                 fields2->Set("dependent_host_object_id", host);
230                 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
231
232                 DbQuery query2;
233                 query2.Table = GetType()->GetTable() + "dependencies";
234                 query2.Type = DbQueryInsert;
235                 query2.Fields = fields2;
236                 OnQuery(query2);
237         }
238
239         /* host contacts, contactgroups */
240         Service::Ptr service = host->GetCheckService();
241
242         if (service) {
243                 Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
244
245                 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
246                         Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
247
248                         Dictionary::Ptr fields_contact = boost::make_shared<Dictionary>();
249                         fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
250                         fields_contact->Set("contact_object_id", user);
251                         fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
252
253                         DbQuery query_contact;
254                         query_contact.Table = GetType()->GetTable() + "_contacts";
255                         query_contact.Type = DbQueryInsert;
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 = boost::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.Fields = fields_contact;
274                         OnQuery(query_contact);
275                 }
276         }
277
278         /* custom variables */
279         Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'");
280
281         DbQuery query_del3;
282         query_del3.Table = "customvariables";
283         query_del3.Type = DbQueryDelete;
284         query_del3.WhereCriteria = boost::make_shared<Dictionary>();
285         query_del3.WhereCriteria->Set("object_id", host);
286         OnQuery(query_del3);
287
288         Dictionary::Ptr customvars;
289         {
290                 ObjectLock olock(host);
291                 customvars = CompatUtility::GetCustomVariableConfig(host);
292         }
293
294         if (customvars) {
295                 ObjectLock olock (customvars);
296
297                 String key;
298                 Value value;
299                 BOOST_FOREACH(boost::tie(key, value), customvars) {
300                         Log(LogDebug, "db_ido", "host customvar key: '" + key + "' value: '" + Convert::ToString(value) + "'");
301
302                         Dictionary::Ptr fields3 = boost::make_shared<Dictionary>();
303                         fields3->Set("varname", Convert::ToString(key));
304                         fields3->Set("varvalue", Convert::ToString(value));
305                         fields3->Set("config_type", 1);
306                         fields3->Set("has_been_modified", 0);
307                         fields3->Set("object_id", host);
308                         fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
309
310                         DbQuery query3;
311                         query3.Table = "customvariables";
312                         query3.Type = DbQueryInsert;
313                         query3.Fields = fields3;
314                         OnQuery(query3);
315                 }
316         }
317 }
318
319 void HostDbObject::OnStatusUpdate(void)
320 {
321 }