]> granicus.if.org Git - icinga2/blob - lib/db_ido/hostdbobject.cpp
Implement host checks.
[icinga2] / lib / db_ido / hostdbobject.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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         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));
50
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());
62
63         fields->Set("first_notification_delay", Empty);
64
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));
68
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));
72
73         fields->Set("stalk_on_up", Empty);
74         fields->Set("stalk_on_down", Empty);
75         fields->Set("stalk_on_unreachable", Empty);
76
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));
83
84         fields->Set("process_performance_data", 0);
85
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));
91
92         fields->Set("retain_status_information", 1);
93         fields->Set("retain_nonstatus_information", 1);
94
95         fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
96
97         fields->Set("obsess_over_host", 0);
98         fields->Set("failure_prediction_enabled", 0);
99
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"));
106
107         Host2dCoords coords = CompatUtility::GetHost2dCoords(host);
108
109         fields->Set("have_2d_coords", coords.have_2d_coords);
110
111         if (coords.have_2d_coords) {
112                 fields->Set("x_2d", coords.x_2d);
113                 fields->Set("y_2d", coords.y_2d);
114         }
115
116         /* deprecated in 1.x */
117         fields->Set("have_3d_coords", 0);
118
119         return fields;
120 }
121
122 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
123 {
124         Dictionary::Ptr fields = make_shared<Dictionary>();
125         Host::Ptr host = static_pointer_cast<Host>(GetObject());
126
127         CheckResult::Ptr cr = host->GetLastCheckResult();
128
129         if (cr) {
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());
134         }
135
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());
141
142         if (cr)
143                 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
144
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));
166
167         if (cr) {
168                 fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
169                 fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
170         }
171
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());
182
183         return fields;
184 }
185
186 void HostDbObject::OnConfigUpdate(void)
187 {
188         Host::Ptr host = static_pointer_cast<Host>(GetObject());
189
190         /* parents, host dependencies */
191         BOOST_FOREACH(const Checkable::Ptr& checkable, host->GetParents()) {
192                 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
193
194                 if (!parent)
195                         continue;
196
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         Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
227
228         BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(host)) {
229                 Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
230
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 */
235
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);
242         }
243
244         Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
245
246         BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(host)) {
247                 Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
248
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 */
253
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);
260         }
261
262         /* custom variables */
263         Dictionary::Ptr vars;
264         {
265                 ObjectLock olock(host);
266                 vars = CompatUtility::GetCustomAttributeConfig(host);
267         }
268
269         if (vars) {
270                 Log(LogDebug, "ido", "Dumping host vars for '" + host->GetName() + "'");
271
272                 ObjectLock olock (vars);
273
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) + "'");
277
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 */
285
286                                 DbQuery query3;
287                                 query3.Table = "customvariables";
288                                 query3.Type = DbQueryInsert;
289                                 query3.Category = DbCatConfig;
290                                 query3.Fields = fields3;
291                                 OnQuery(query3);
292                         }
293                 }
294         }
295 }
296
297 void HostDbObject::OnStatusUpdate(void)
298 {
299 }