]> granicus.if.org Git - icinga2/blob - lib/db_ido/hostdbobject.cpp
82e75ec7a7725f3ced48be78f69bfa9398a42596
[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_command_object_id", host->GetCheckCommand());
52         fields->Set("check_command_args", Empty);
53         fields->Set("eventhandler_command_object_id", host->GetEventCommand());
54         fields->Set("eventhandler_command_args", Empty);
55         fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(host)));
56         fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
57         fields->Set("failure_prediction_options", Empty);
58         fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(host));
59         fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(host));
60         fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
61
62         fields->Set("first_notification_delay", Empty);
63
64         fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(host));
65         fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
66         fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
67
68         fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(host));
69         fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(host));
70         fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(host));
71
72         fields->Set("stalk_on_up", Empty);
73         fields->Set("stalk_on_down", Empty);
74         fields->Set("stalk_on_unreachable", Empty);
75
76         fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
77         fields->Set("flap_detection_on_up", Empty);
78         fields->Set("flap_detection_on_down", Empty);
79         fields->Set("flap_detection_on_unreachable", Empty);
80         fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(host));
81         fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(host));
82
83         fields->Set("process_performance_data", 0);
84
85         fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(host));
86         fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(host));
87         fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
88         fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
89         fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
90
91         fields->Set("retain_status_information", 1);
92         fields->Set("retain_nonstatus_information", 1);
93
94         fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
95
96         fields->Set("obsess_over_host", 0);
97         fields->Set("failure_prediction_enabled", 0);
98
99         fields->Set("notes", CompatUtility::GetCustomAttributeConfig(host, "notes"));
100         fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(host, "notes_url"));
101         fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(host, "action_url"));
102         fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(host, "icon_image"));
103         fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt"));
104         fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"));
105
106         Host2dCoords coords = CompatUtility::GetHost2dCoords(host);
107
108         fields->Set("have_2d_coords", coords.have_2d_coords);
109
110         if (coords.have_2d_coords) {
111                 fields->Set("x_2d", coords.x_2d);
112                 fields->Set("y_2d", coords.y_2d);
113         }
114
115         /* deprecated in 1.x */
116         fields->Set("have_3d_coords", 0);
117
118         return fields;
119 }
120
121 Dictionary::Ptr HostDbObject::GetStatusFields(void) const
122 {
123         Dictionary::Ptr fields = make_shared<Dictionary>();
124         Host::Ptr host = static_pointer_cast<Host>(GetObject());
125
126         CheckResult::Ptr cr = host->GetLastCheckResult();
127
128         if (cr) {
129                 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
130                 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
131                 fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
132                 fields->Set("check_source", cr->GetCheckSource());
133         }
134
135         fields->Set("current_state", host->IsReachable() ? host->GetState() : 2);
136         fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host));
137         fields->Set("should_be_scheduled", CompatUtility::GetCheckableShouldBeScheduled(host));
138         fields->Set("current_check_attempt", host->GetCheckAttempt());
139         fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
140
141         if (cr)
142                 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
143
144         fields->Set("next_check", DbValue::FromTimestamp(host->GetNextCheck()));
145         fields->Set("check_type", CompatUtility::GetCheckableCheckType(host));
146         fields->Set("last_state_change", DbValue::FromTimestamp(host->GetLastStateChange()));
147         fields->Set("last_hard_state_change", DbValue::FromTimestamp(host->GetLastHardStateChange()));
148         fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
149         fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
150         fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
151         fields->Set("state_type", host->GetStateType());
152         fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(host)));
153         fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(host)));
154         fields->Set("no_more_notifications", Empty);
155         fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
156         fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(host));
157         fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(host));
158         fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(host));
159         fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
160         fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
161         fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
162         fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
163         fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(host));
164         fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(host));
165
166         if (cr) {
167                 fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
168                 fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
169         }
170
171         fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth());
172         fields->Set("failure_prediction_enabled", Empty);
173         fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */
174         fields->Set("obsess_over_host", Empty);
175         fields->Set("modified_host_attributes", host->GetModifiedAttributes());
176         fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(host));
177         fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(host));
178         fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(host));
179         fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(host));
180         fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
181
182         return fields;
183 }
184
185 void HostDbObject::OnConfigUpdate(void)
186 {
187         Host::Ptr host = static_pointer_cast<Host>(GetObject());
188
189         /* parents, host dependencies */
190         BOOST_FOREACH(const Checkable::Ptr& checkable, host->GetParents()) {
191                 Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
192
193                 if (!parent)
194                         continue;
195
196                 Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
197
198                 /* parents: host_id, parent_host_object_id */
199                 Dictionary::Ptr fields1 = make_shared<Dictionary>();
200                 fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
201                 fields1->Set("parent_host_object_id", parent);
202                 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
203
204                 DbQuery query1;
205                 query1.Table = GetType()->GetTable() + "_parenthosts";
206                 query1.Type = DbQueryInsert;
207                 query1.Category = DbCatConfig;
208                 query1.Fields = fields1;
209                 OnQuery(query1);
210
211                 /* host dependencies */
212                 Dictionary::Ptr fields2 = make_shared<Dictionary>();
213                 fields2->Set("host_object_id", parent);
214                 fields2->Set("dependent_host_object_id", host);
215                 fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
216
217                 DbQuery query2;
218                 query2.Table = GetType()->GetTable() + "dependencies";
219                 query2.Type = DbQueryInsert;
220                 query2.Category = DbCatConfig;
221                 query2.Fields = fields2;
222                 OnQuery(query2);
223         }
224
225         Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
226
227         BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(host)) {
228                 Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
229
230                 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
231                 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
232                 fields_contact->Set("contact_object_id", user);
233                 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
234
235                 DbQuery query_contact;
236                 query_contact.Table = GetType()->GetTable() + "_contacts";
237                 query_contact.Type = DbQueryInsert;
238                 query_contact.Category = DbCatConfig;
239                 query_contact.Fields = fields_contact;
240                 OnQuery(query_contact);
241         }
242
243         Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
244
245         BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(host)) {
246                 Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
247
248                 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
249                 fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
250                 fields_contact->Set("contactgroup_object_id", usergroup);
251                 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
252
253                 DbQuery query_contact;
254                 query_contact.Table = GetType()->GetTable() + "_contactgroups";
255                 query_contact.Type = DbQueryInsert;
256                 query_contact.Category = DbCatConfig;
257                 query_contact.Fields = fields_contact;
258                 OnQuery(query_contact);
259         }
260
261         /* custom variables */
262         Dictionary::Ptr vars;
263         {
264                 ObjectLock olock(host);
265                 vars = CompatUtility::GetCustomAttributeConfig(host);
266         }
267
268         if (vars) {
269                 Log(LogDebug, "ido", "Dumping host vars for '" + host->GetName() + "'");
270
271                 ObjectLock olock (vars);
272
273                 BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
274                         if (!kv.first.IsEmpty()) {
275                                 Log(LogDebug, "db_ido", "host customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) + "'");
276
277                                 Dictionary::Ptr fields3 = make_shared<Dictionary>();
278                                 fields3->Set("varname", Convert::ToString(kv.first));
279                                 fields3->Set("varvalue", Convert::ToString(kv.second));
280                                 fields3->Set("config_type", 1);
281                                 fields3->Set("has_been_modified", 0);
282                                 fields3->Set("object_id", host);
283                                 fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
284
285                                 DbQuery query3;
286                                 query3.Table = "customvariables";
287                                 query3.Type = DbQueryInsert;
288                                 query3.Category = DbCatConfig;
289                                 query3.Fields = fields3;
290                                 OnQuery(query3);
291                         }
292                 }
293         }
294 }
295
296 void HostDbObject::OnStatusUpdate(void)
297 {
298 }