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/servicedbobject.h"
21 #include "db_ido/dbtype.h"
22 #include "db_ido/dbvalue.h"
23 #include "db_ido/dbevents.h"
24 #include "base/convert.h"
25 #include "base/objectlock.h"
26 #include "base/initialize.h"
27 #include "base/dynamictype.h"
28 #include "base/utility.h"
29 #include "remote/endpoint.h"
30 #include "icinga/notification.h"
31 #include "icinga/checkcommand.h"
32 #include "icinga/eventcommand.h"
33 #include "icinga/externalcommandprocessor.h"
34 #include "icinga/compatutility.h"
35 #include "icinga/icingaapplication.h"
36 #include <boost/foreach.hpp>
37 #include <boost/algorithm/string/join.hpp>
39 using namespace icinga;
41 REGISTER_DBTYPE(Service, "service", DbObjectTypeService, "service_object_id", ServiceDbObject);
43 ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
44 : DbObject(type, name1, name2)
47 Dictionary::Ptr ServiceDbObject::GetConfigFields(void) const
49 Dictionary::Ptr fields = make_shared<Dictionary>();
50 Service::Ptr service = static_pointer_cast<Service>(GetObject());
51 Host::Ptr host = service->GetHost();
53 fields->Set("host_object_id", host);
54 fields->Set("display_name", service->GetDisplayName());
55 fields->Set("check_command_object_id", service->GetCheckCommand());
56 fields->Set("check_command_args", Empty);
57 fields->Set("eventhandler_command_object_id", service->GetEventCommand());
58 fields->Set("eventhandler_command_args", Empty);
59 fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(service)));
60 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
61 fields->Set("failure_prediction_options", Empty);
62 fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(service));
63 fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(service));
64 fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
65 fields->Set("first_notification_delay", Empty);
66 fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(service));
67 fields->Set("notify_on_warning", CompatUtility::GetCheckableNotifyOnWarning(service));
68 fields->Set("notify_on_unknown", CompatUtility::GetCheckableNotifyOnUnknown(service));
69 fields->Set("notify_on_critical", CompatUtility::GetCheckableNotifyOnCritical(service));
70 fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(service));
71 fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(service));
72 fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(service));
73 fields->Set("stalk_on_ok", 0);
74 fields->Set("stalk_on_warning", 0);
75 fields->Set("stalk_on_unknown", 0);
76 fields->Set("stalk_on_critical", 0);
77 fields->Set("is_volatile", CompatUtility::GetCheckableIsVolatile(service));
78 fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service));
79 fields->Set("flap_detection_on_ok", Empty);
80 fields->Set("flap_detection_on_warning", Empty);
81 fields->Set("flap_detection_on_unknown", Empty);
82 fields->Set("flap_detection_on_critical", Empty);
83 fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(service));
84 fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(service));
85 fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service));
86 fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(service));
87 fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(service));
88 fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service));
89 fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service));
90 fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service));
91 fields->Set("retain_status_information", Empty);
92 fields->Set("retain_nonstatus_information", Empty);
93 fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service));
94 fields->Set("obsess_over_service", Empty);
95 fields->Set("failure_prediction_enabled", Empty);
96 fields->Set("notes", service->GetNotes());
97 fields->Set("notes_url", service->GetNotesUrl());
98 fields->Set("action_url", service->GetActionUrl());
99 fields->Set("icon_image", service->GetIconImage());
100 fields->Set("icon_image_alt", service->GetIconImageAlt());
105 Dictionary::Ptr ServiceDbObject::GetStatusFields(void) const
107 Dictionary::Ptr fields = make_shared<Dictionary>();
108 Service::Ptr service = static_pointer_cast<Service>(GetObject());
109 CheckResult::Ptr cr = service->GetLastCheckResult();
112 fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
113 fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
114 fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
115 fields->Set("check_source", cr->GetCheckSource());
118 fields->Set("current_state", service->GetState());
119 fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(service));
120 fields->Set("should_be_scheduled", service->GetEnableActiveChecks());
121 fields->Set("current_check_attempt", service->GetCheckAttempt());
122 fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
125 fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
127 fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
128 fields->Set("check_type", CompatUtility::GetCheckableCheckType(service));
129 fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
130 fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
131 fields->Set("last_time_ok", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateOK())));
132 fields->Set("last_time_warning", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateWarning())));
133 fields->Set("last_time_critical", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateCritical())));
134 fields->Set("last_time_unknown", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateUnknown())));
135 fields->Set("state_type", service->GetStateType());
136 fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(service)));
137 fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(service)));
138 fields->Set("no_more_notifications", Empty);
139 fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service));
140 fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(service));
141 fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(service));
142 fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(service));
143 fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service));
144 fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service));
145 fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service));
146 fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service));
147 fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(service));
148 fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(service));
151 fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
152 fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
155 fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
156 fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service));
157 fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(service));
158 fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(service));
159 fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(service));
160 fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(service));
161 fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
162 fields->Set("modified_service_attributes", service->GetModifiedAttributes());
167 bool ServiceDbObject::IsStatusAttribute(const String& attribute) const
169 return (attribute == "last_result");
172 void ServiceDbObject::OnConfigUpdate(void)
174 Service::Ptr service = static_pointer_cast<Service>(GetObject());
176 /* service dependencies */
177 Log(LogDebug, "db_ido", "service dependencies for '" + service->GetName() + "'");
179 BOOST_FOREACH(const Checkable::Ptr& checkable, service->GetParents()) {
180 Service::Ptr parent = dynamic_pointer_cast<Service>(checkable);
185 Log(LogDebug, "db_ido", "service parents: " + parent->GetName());
187 /* service dependencies */
188 Dictionary::Ptr fields1 = make_shared<Dictionary>();
189 fields1->Set("service_object_id", parent);
190 fields1->Set("dependent_service_object_id", service);
191 fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
194 query1.Table = GetType()->GetTable() + "dependencies";
195 query1.Type = DbQueryInsert;
196 query1.Category = DbCatConfig;
197 query1.Fields = fields1;
201 /* service contacts, contactgroups */
202 Log(LogDebug, "db_ido", "service contacts: " + service->GetName());
204 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(service)) {
205 Log(LogDebug, "db_ido", "service contacts: " + user->GetName());
207 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
208 fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
209 fields_contact->Set("contact_object_id", user);
210 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
212 DbQuery query_contact;
213 query_contact.Table = GetType()->GetTable() + "_contacts";
214 query_contact.Type = DbQueryInsert;
215 query_contact.Category = DbCatConfig;
216 query_contact.Fields = fields_contact;
217 OnQuery(query_contact);
220 Log(LogDebug, "db_ido", "service contactgroups: " + service->GetName());
222 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(service)) {
223 Log(LogDebug, "db_ido", "service contactgroups: " + usergroup->GetName());
225 Dictionary::Ptr fields_contact = make_shared<Dictionary>();
226 fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
227 fields_contact->Set("contactgroup_object_id", usergroup);
228 fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
230 DbQuery query_contact;
231 query_contact.Table = GetType()->GetTable() + "_contactgroups";
232 query_contact.Type = DbQueryInsert;
233 query_contact.Category = DbCatConfig;
234 query_contact.Fields = fields_contact;
235 OnQuery(query_contact);
238 /* update comments and downtimes on config change */
239 DbEvents::AddComments(service);
240 DbEvents::AddDowntimes(service);
243 void ServiceDbObject::OnStatusUpdate(void)