1 /******************************************************************************
3 * Copyright (C) 2012-2014 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 fields->Set("alias", CompatUtility::GetHostAlias(host));
47 fields->Set("display_name", host->GetDisplayName());
48 fields->Set("address", CompatUtility::GetHostAddress(host));
49 fields->Set("address6", CompatUtility::GetHostAddress6(host));
51 fields->Set("check_service_object_id", host);
52 fields->Set("check_command_object_id", host->GetCheckCommand());
53 fields->Set("check_command_args", Empty);
54 fields->Set("eventhandler_command_object_id", host->GetEventCommand());
55 fields->Set("eventhandler_command_args", Empty);
56 fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(host)));
57 fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
58 fields->Set("failure_prediction_options", Empty);
59 fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(host));
60 fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(host));
61 fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
63 fields->Set("first_notification_delay", Empty);
65 fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(host));
66 fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
67 fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
69 fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(host));
70 fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(host));
71 fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(host));
73 fields->Set("stalk_on_up", Empty);
74 fields->Set("stalk_on_down", Empty);
75 fields->Set("stalk_on_unreachable", Empty);
77 fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
78 fields->Set("flap_detection_on_up", Empty);
79 fields->Set("flap_detection_on_down", Empty);
80 fields->Set("flap_detection_on_unreachable", Empty);
81 fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(host));
82 fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(host));
84 fields->Set("process_performance_data", 0);
86 fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(host));
87 fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(host));
88 fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
89 fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
90 fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
92 fields->Set("retain_status_information", 1);
93 fields->Set("retain_nonstatus_information", 1);
95 fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
97 fields->Set("obsess_over_host", 0);
98 fields->Set("failure_prediction_enabled", 0);
100 fields->Set("notes", CompatUtility::GetCustomAttributeConfig(host, "notes"));
101 fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(host, "notes_url"));
102 fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(host, "action_url"));
103 fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(host, "icon_image"));
104 fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt"));
105 fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"));
107 Host2dCoords coords = CompatUtility::GetHost2dCoords(host);
109 fields->Set("have_2d_coords", coords.have_2d_coords);
111 if (coords.have_2d_coords) {
112 fields->Set("x_2d", coords.x_2d);
113 fields->Set("y_2d", coords.y_2d);
116 /* deprecated in 1.x */
117 fields->Set("have_3d_coords", 0);
122 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
124 Dictionary::Ptr fields = make_shared<Dictionary>();
125 Host::Ptr host = static_pointer_cast<Host>(GetObject());
127 CheckResult::Ptr cr = host->GetLastCheckResult();
130 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
131 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
132 fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
133 fields->Set("check_source", cr->GetCheckSource());
136 fields->Set("current_state", host->GetState());
137 fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host));
138 fields->Set("should_be_scheduled", CompatUtility::GetCheckableShouldBeScheduled(host));
139 fields->Set("current_check_attempt", host->GetCheckAttempt());
140 fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
143 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
145 fields->Set("next_check", DbValue::FromTimestamp(host->GetNextCheck()));
146 fields->Set("check_type", CompatUtility::GetCheckableCheckType(host));
147 fields->Set("last_state_change", DbValue::FromTimestamp(host->GetLastStateChange()));
148 fields->Set("last_hard_state_change", DbValue::FromTimestamp(host->GetLastHardStateChange()));
149 fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
150 fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
151 fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
152 fields->Set("state_type", host->GetStateType());
153 fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(host)));
154 fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(host)));
155 fields->Set("no_more_notifications", Empty);
156 fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
157 fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(host));
158 fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(host));
159 fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(host));
160 fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
161 fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
162 fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
163 fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
164 fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(host));
165 fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(host));
168 fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
169 fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
172 fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth());
173 fields->Set("failure_prediction_enabled", Empty);
174 fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */
175 fields->Set("obsess_over_host", Empty);
176 fields->Set("modified_host_attributes", host->GetModifiedAttributes());
177 fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(host));
178 fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(host));
179 fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(host));
180 fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(host));
181 fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
186 void HostDbObject::OnConfigUpdate(void)
188 Host::Ptr host = static_pointer_cast<Host>(GetObject());
190 /* parents, host dependencies */
191 BOOST_FOREACH(const Checkable::Ptr& checkable, host->GetParents()) {
192 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
197 Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
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 */
206 query1.Table = GetType()->GetTable() + "_parenthosts";
207 query1.Type = DbQueryInsert;
208 query1.Category = DbCatConfig;
209 query1.Fields = fields1;
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 */
219 query2.Table = GetType()->GetTable() + "dependencies";
220 query2.Type = DbQueryInsert;
221 query2.Category = DbCatConfig;
222 query2.Fields = fields2;
226 Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
228 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(host)) {
229 Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
231 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
232 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
233 fields_contact->Set("contact_object_id", user);
234 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
236 DbQuery query_contact;
237 query_contact.Table = GetType()->GetTable() + "_contacts";
238 query_contact.Type = DbQueryInsert;
239 query_contact.Category = DbCatConfig;
240 query_contact.Fields = fields_contact;
241 OnQuery(query_contact);
244 Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
246 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(host)) {
247 Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
249 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
250 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
251 fields_contact->Set("contactgroup_object_id", usergroup);
252 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
254 DbQuery query_contact;
255 query_contact.Table = GetType()->GetTable() + "_contactgroups";
256 query_contact.Type = DbQueryInsert;
257 query_contact.Category = DbCatConfig;
258 query_contact.Fields = fields_contact;
259 OnQuery(query_contact);
262 /* custom variables */
263 Dictionary::Ptr vars;
265 ObjectLock olock(host);
266 vars = CompatUtility::GetCustomAttributeConfig(host);
270 Log(LogDebug, "ido", "Dumping host vars for '" + host->GetName() + "'");
272 ObjectLock olock (vars);
274 BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
275 if (!kv.first.IsEmpty()) {
276 Log(LogDebug, "db_ido", "host customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) + "'");
278 Dictionary::Ptr fields3 = make_shared<Dictionary>();
279 fields3->Set("varname", Convert::ToString(kv.first));
280 fields3->Set("varvalue", Convert::ToString(kv.second));
281 fields3->Set("config_type", 1);
282 fields3->Set("has_been_modified", 0);
283 fields3->Set("object_id", host);
284 fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
287 query3.Table = "customvariables";
288 query3.Type = DbQueryInsert;
289 query3.Category = DbCatConfig;
290 query3.Fields = fields3;
297 void HostDbObject::OnStatusUpdate(void)