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 "ido/hostdbobject.h"
21 #include "ido/dbtype.h"
22 #include "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("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", Empty);
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());
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);
191 void HostDbObject::OnConfigUpdate(void)
193 Host::Ptr host = static_pointer_cast<Host>(GetObject());
195 /* parents, host dependencies */
197 query_del1.Table = GetType()->GetTable() + "_parenthosts";
198 query_del1.Type = DbQueryDelete;
199 query_del1.WhereCriteria = boost::make_shared<Dictionary>();
200 query_del1.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
204 query_del2.Table = GetType()->GetTable() + "dependencies";
205 query_del2.Type = DbQueryDelete;
206 query_del2.WhereCriteria = boost::make_shared<Dictionary>();
207 query_del2.WhereCriteria->Set("dependent_host_object_id", host);
210 BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
211 Log(LogDebug, "ido", "host parents: " + parent->GetName());
213 /* parents: host_id, parent_host_object_id */
214 Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
215 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
216 fields1->Set("parent_host_object_id", parent);
217 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
220 query1.Table = GetType()->GetTable() + "_parenthosts";
221 query1.Type = DbQueryInsert;
222 query1.Fields = fields1;
225 /* host dependencies */
226 Dictionary::Ptr fields2 = boost::make_shared<Dictionary>();
227 fields2->Set("host_object_id", parent);
228 fields2->Set("dependent_host_object_id", host);
229 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
232 query2.Table = GetType()->GetTable() + "dependencies";
233 query2.Type = DbQueryInsert;
234 query2.Fields = fields2;
238 /* custom variables */
239 Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'");
242 query_del3.Table = "customvariables";
243 query_del3.Type = DbQueryDelete;
244 query_del3.WhereCriteria = boost::make_shared<Dictionary>();
245 query_del3.WhereCriteria->Set("object_id", host);
248 Dictionary::Ptr customvars;
250 ObjectLock olock(host);
251 customvars = CompatUtility::GetCustomVariableConfig(host);
255 ObjectLock olock (customvars);
259 BOOST_FOREACH(boost::tie(key, value), customvars) {
260 Log(LogDebug, "ido", "host customvar key: '" + key + "' value: '" + Convert::ToString(value) + "'");
262 Dictionary::Ptr fields3 = boost::make_shared<Dictionary>();
263 fields3->Set("varname", Convert::ToString(key));
264 fields3->Set("varvalue", Convert::ToString(value));
265 fields3->Set("config_type", 1);
266 fields3->Set("has_been_modified", 0);
267 fields3->Set("object_id", host);
268 fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
271 query3.Table = "customvariables";
272 query3.Type = DbQueryInsert;
273 query3.Fields = fields3;
279 void HostDbObject::OnStatusUpdate(void)