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