1 /******************************************************************************
3 * Copyright (C) 2012-present 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>
33 using namespace icinga;
35 REGISTER_DBTYPE(Host, "host", DbObjectTypeHost, "host_object_id", HostDbObject);
37 HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
38 : DbObject(type, name1, name2)
41 Dictionary::Ptr HostDbObject::GetConfigFields(void) const
43 Dictionary::Ptr fields = make_shared<Dictionary>();
44 Host::Ptr host = static_pointer_cast<Host>(GetObject());
46 Service::Ptr service = host->GetCheckService();
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));
54 fields->Set("check_command_object_id", service->GetCheckCommand());
55 fields->Set("check_command_args", Empty);
56 fields->Set("eventhandler_command_object_id", service->GetEventCommand());
57 fields->Set("eventhandler_command_args", Empty);
58 fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetServiceNotificationNotificationPeriod(service)));
59 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
60 fields->Set("failure_prediction_options", Empty);
61 fields->Set("check_interval", CompatUtility::GetServiceCheckInterval(service));
62 fields->Set("retry_interval", CompatUtility::GetServiceRetryInterval(service));
63 fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
65 fields->Set("first_notification_delay", Empty);
67 fields->Set("notification_interval", CompatUtility::GetServiceNotificationNotificationInterval(service));
68 /* requires host check service */
69 fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
70 fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
72 fields->Set("notify_on_recovery", CompatUtility::GetServiceNotifyOnRecovery(service));
73 fields->Set("notify_on_flapping", CompatUtility::GetServiceNotifyOnFlapping(service));
74 fields->Set("notify_on_downtime", CompatUtility::GetServiceNotifyOnDowntime(service));
76 fields->Set("stalk_on_up", Empty);
77 fields->Set("stalk_on_down", Empty);
78 fields->Set("stalk_on_unreachable", Empty);
80 fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service));
81 fields->Set("flap_detection_on_up", Empty);
82 fields->Set("flap_detection_on_down", Empty);
83 fields->Set("flap_detection_on_unreachable", Empty);
84 fields->Set("low_flap_threshold", CompatUtility::GetServiceLowFlapThreshold(service));
85 fields->Set("high_flap_threshold", CompatUtility::GetServiceHighFlapThreshold(service));
88 fields->Set("process_performance_data", 0);
91 fields->Set("freshness_checks_enabled", CompatUtility::GetServiceFreshnessChecksEnabled(service));
92 fields->Set("freshness_threshold", CompatUtility::GetServiceFreshnessThreshold(service));
93 fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service));
94 fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service));
95 fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service));
98 fields->Set("retain_status_information", 1);
99 fields->Set("retain_nonstatus_information", 1);
102 fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service));
104 fields->Set("obsess_over_host", 0);
105 fields->Set("failure_prediction_enabled", 0);
107 fields->Set("notes", CompatUtility::GetCustomAttributeConfig(host, "notes"));
108 fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(host, "notes_url"));
109 fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(host, "action_url"));
110 fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(host, "icon_image"));
111 fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt"));
112 fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"));
114 Host2dCoords coords = CompatUtility::GetHost2dCoords(host);
116 fields->Set("have_2d_coords", coords.have_2d_coords);
118 if (coords.have_2d_coords) {
119 fields->Set("x_2d", coords.x_2d);
120 fields->Set("y_2d", coords.y_2d);
123 /* deprecated in 1.x */
124 fields->Set("have_3d_coords", 0);
129 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
131 Dictionary::Ptr fields = make_shared<Dictionary>();
132 Host::Ptr host = static_pointer_cast<Host>(GetObject());
133 Service::Ptr service = host->GetCheckService();
135 /* fetch service status, or dump a pending hoststatus */
137 CheckResult::Ptr cr = service->GetLastCheckResult();
140 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
141 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
142 fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
143 fields->Set("check_source", cr->GetCheckSource());
146 fields->Set("current_state", host->GetState());
147 fields->Set("has_been_checked", CompatUtility::GetServiceHasBeenChecked(service));
148 fields->Set("should_be_scheduled", CompatUtility::GetServiceShouldBeScheduled(service));
149 fields->Set("current_check_attempt", service->GetCheckAttempt());
150 fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
153 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
155 fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
156 fields->Set("check_type", CompatUtility::GetServiceCheckType(service));
157 fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
158 fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
159 fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
160 fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
161 fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
162 fields->Set("state_type", service->GetStateType());
163 fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationLastNotification(service)));
164 fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationNextNotification(service)));
165 fields->Set("no_more_notifications", Empty);
166 fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service));
167 fields->Set("problem_has_been_acknowledged", CompatUtility::GetServiceProblemHasBeenAcknowledged(service));
168 fields->Set("acknowledgement_type", CompatUtility::GetServiceAcknowledgementType(service));
169 fields->Set("current_notification_number", CompatUtility::GetServiceNotificationNotificationNumber(service));
170 fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service));
171 fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service));
172 fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service));
173 fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service));
174 fields->Set("is_flapping", CompatUtility::GetServiceIsFlapping(service));
175 fields->Set("percent_state_change", CompatUtility::GetServicePercentStateChange(service));
178 fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
179 fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
181 fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
182 fields->Set("failure_prediction_enabled", Empty);
183 fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */
184 fields->Set("obsess_over_host", Empty);
185 fields->Set("modified_host_attributes", service->GetModifiedAttributes());
186 fields->Set("event_handler", CompatUtility::GetServiceEventHandler(service));
187 fields->Set("check_command", CompatUtility::GetServiceCheckCommand(service));
188 fields->Set("normal_check_interval", CompatUtility::GetServiceCheckInterval(service));
189 fields->Set("retry_check_interval", CompatUtility::GetServiceRetryInterval(service));
190 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
193 fields->Set("has_been_checked", 0);
194 fields->Set("last_check", DbValue::FromTimestamp(0));
195 fields->Set("next_check", DbValue::FromTimestamp(0));
196 fields->Set("active_checks_enabled", 0);
202 void HostDbObject::OnConfigUpdate(void)
204 Host::Ptr host = static_pointer_cast<Host>(GetObject());
206 /* parents, host dependencies */
207 BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
208 Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
210 /* parents: host_id, parent_host_object_id */
211 Dictionary::Ptr fields1 = make_shared<Dictionary>();
212 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
213 fields1->Set("parent_host_object_id", parent);
214 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
217 query1.Table = GetType()->GetTable() + "_parenthosts";
218 query1.Type = DbQueryInsert;
219 query1.Category = DbCatConfig;
220 query1.Fields = fields1;
223 /* host dependencies */
224 Dictionary::Ptr fields2 = make_shared<Dictionary>();
225 fields2->Set("host_object_id", parent);
226 fields2->Set("dependent_host_object_id", host);
227 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
230 query2.Table = GetType()->GetTable() + "dependencies";
231 query2.Type = DbQueryInsert;
232 query2.Category = DbCatConfig;
233 query2.Fields = fields2;
237 /* host contacts, contactgroups */
238 Service::Ptr service = host->GetCheckService();
241 Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
243 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
244 Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
246 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
247 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
248 fields_contact->Set("contact_object_id", user);
249 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
251 DbQuery query_contact;
252 query_contact.Table = GetType()->GetTable() + "_contacts";
253 query_contact.Type = DbQueryInsert;
254 query_contact.Category = DbCatConfig;
255 query_contact.Fields = fields_contact;
256 OnQuery(query_contact);
259 Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
261 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
262 Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
264 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
265 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
266 fields_contact->Set("contactgroup_object_id", usergroup);
267 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
269 DbQuery query_contact;
270 query_contact.Table = GetType()->GetTable() + "_contactgroups";
271 query_contact.Type = DbQueryInsert;
272 query_contact.Category = DbCatConfig;
273 query_contact.Fields = fields_contact;
274 OnQuery(query_contact);
278 /* custom variables */
279 Dictionary::Ptr customvars;
281 ObjectLock olock(host);
282 customvars = CompatUtility::GetCustomVariableConfig(host);
286 Log(LogDebug, "ido", "Dumping host customvars for '" + host->GetName() + "'");
288 ObjectLock olock (customvars);
290 BOOST_FOREACH(const Dictionary::Pair& kv, customvars) {
291 if (!kv.first.IsEmpty()) {
292 Log(LogDebug, "db_ido", "host customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) + "'");
294 Dictionary::Ptr fields3 = make_shared<Dictionary>();
295 fields3->Set("varname", Convert::ToString(kv.first));
296 fields3->Set("varvalue", Convert::ToString(kv.second));
297 fields3->Set("config_type", 1);
298 fields3->Set("has_been_modified", 0);
299 fields3->Set("object_id", host);
300 fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
303 query3.Table = "customvariables";
304 query3.Type = DbQueryInsert;
305 query3.Category = DbCatConfig;
306 query3.Fields = fields3;
313 void HostDbObject::OnStatusUpdate(void)