1 /******************************************************************************
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
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>
34 using namespace icinga;
36 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
38 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
39 : DbObject(type, name1, name2)
42 Dictionary::Ptr HostDbObject::GetConfigFields(void) const
44 Dictionary::Ptr fields = boost::make_shared<Dictionary>();
45 Host::Ptr host = static_pointer_cast<Host>(GetObject());
47 Service::Ptr service = host->GetCheckService();
49 Dictionary::Ptr attrs;
52 ObjectLock olock(host);
53 attrs = CompatUtility::GetHostConfigAttributes(host);
56 fields->Set("alias", attrs->Get("alias"));
57 fields->Set("display_name", attrs->Get("display_name"));
59 fields->Set("address", attrs->Get("address"));
60 fields->Set("address6", attrs->Get("address6"));
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);
69 fields->Set("notification_timeperiod_object_id", Notification::GetByName(attrs->Get("notification_period")));
72 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
74 fields->Set("failure_prediction_options", Empty);
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"));
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"));
88 fields->Set("stalk_on_up", Empty);
89 fields->Set("stalk_on_down", Empty);
90 fields->Set("stalk_on_unreachable", Empty);
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);
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);
125 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
127 Dictionary::Ptr fields = boost::make_shared<Dictionary>();
128 Host::Ptr host = static_pointer_cast<Host>(GetObject());
129 Service::Ptr service = host->GetCheckService();
131 Dictionary::Ptr attrs;
133 /* fetch service status, or dump a pending hoststatus */
135 ObjectLock olock(service);
136 attrs = CompatUtility::GetServiceStatusAttributes(service, CompatTypeHost);
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());
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);
192 void HostDbObject::OnConfigUpdate(void)
194 Host::Ptr host = static_pointer_cast<Host>(GetObject());
196 /* parents, host dependencies */
198 query_del1.Table = GetType()->GetTable() + "_parenthosts";
199 query_del1.Type = DbQueryDelete;
200 query_del1.Category = DbCatConfig;
201 query_del1.WhereCriteria = boost::make_shared<Dictionary>();
202 query_del1.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
206 query_del2.Table = GetType()->GetTable() + "dependencies";
207 query_del2.Type = DbQueryDelete;
208 query_del2.Category = DbCatConfig;
209 query_del2.WhereCriteria = boost::make_shared<Dictionary>();
210 query_del2.WhereCriteria->Set("dependent_host_object_id", host);
213 BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
214 Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
216 /* parents: host_id, parent_host_object_id */
217 Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
218 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
219 fields1->Set("parent_host_object_id", parent);
220 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
223 query1.Table = GetType()->GetTable() + "_parenthosts";
224 query1.Type = DbQueryInsert;
225 query1.Category = DbCatConfig;
226 query1.Fields = fields1;
229 /* host dependencies */
230 Dictionary::Ptr fields2 = boost::make_shared<Dictionary>();
231 fields2->Set("host_object_id", parent);
232 fields2->Set("dependent_host_object_id", host);
233 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
236 query2.Table = GetType()->GetTable() + "dependencies";
237 query2.Type = DbQueryInsert;
238 query2.Category = DbCatConfig;
239 query2.Fields = fields2;
243 /* host contacts, contactgroups */
244 Service::Ptr service = host->GetCheckService();
247 Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
249 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
250 Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
252 Dictionary::Ptr fields_contact = boost::make_shared<Dictionary>();
253 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
254 fields_contact->Set("contact_object_id", user);
255 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
257 DbQuery query_contact;
258 query_contact.Table = GetType()->GetTable() + "_contacts";
259 query_contact.Type = DbQueryInsert;
260 query_contact.Category = DbCatConfig;
261 query_contact.Fields = fields_contact;
262 OnQuery(query_contact);
265 Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
267 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
268 Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
270 Dictionary::Ptr fields_contact = boost::make_shared<Dictionary>();
271 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
272 fields_contact->Set("contactgroup_object_id", usergroup);
273 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
275 DbQuery query_contact;
276 query_contact.Table = GetType()->GetTable() + "_contactgroups";
277 query_contact.Type = DbQueryInsert;
278 query_contact.Category = DbCatConfig;
279 query_contact.Fields = fields_contact;
280 OnQuery(query_contact);
284 /* custom variables */
285 Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'");
288 query_del3.Table = "customvariables";
289 query_del3.Type = DbQueryDelete;
290 query_del3.Category = DbCatConfig;
291 query_del3.WhereCriteria = boost::make_shared<Dictionary>();
292 query_del3.WhereCriteria->Set("object_id", host);
295 Dictionary::Ptr customvars;
297 ObjectLock olock(host);
298 customvars = CompatUtility::GetCustomVariableConfig(host);
302 ObjectLock olock (customvars);
306 BOOST_FOREACH(boost::tie(key, value), customvars) {
307 Log(LogDebug, "db_ido", "host customvar key: '" + key + "' value: '" + Convert::ToString(value) + "'");
309 Dictionary::Ptr fields3 = boost::make_shared<Dictionary>();
310 fields3->Set("varname", Convert::ToString(key));
311 fields3->Set("varvalue", Convert::ToString(value));
312 fields3->Set("config_type", 1);
313 fields3->Set("has_been_modified", 0);
314 fields3->Set("object_id", host);
315 fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
318 query3.Table = "customvariables";
319 query3.Type = DbQueryInsert;
320 query3.Category = DbCatConfig;
321 query3.Fields = fields3;
327 void HostDbObject::OnStatusUpdate(void)