]> granicus.if.org Git - icinga2/blobdiff - lib/db_ido/hostdbobject.cpp
Remove the HostUnreachable state.
[icinga2] / lib / db_ido / hostdbobject.cpp
index 8c43ec315be530fd7c6362ccb84ebebd0872c9d0..82e75ec7a7725f3ced48be78f69bfa9398a42596 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
+ * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
  *                                                                            *
  * This program is free software; you can redistribute it and/or              *
  * modify it under the terms of the GNU General Public License                *
@@ -43,63 +43,55 @@ Dictionary::Ptr HostDbObject::GetConfigFields(void) const
        Dictionary::Ptr fields = make_shared<Dictionary>();
        Host::Ptr host = static_pointer_cast<Host>(GetObject());
 
-       Service::Ptr service = host->GetCheckService();
-
        fields->Set("alias", CompatUtility::GetHostAlias(host));
        fields->Set("display_name", host->GetDisplayName());
        fields->Set("address", CompatUtility::GetHostAddress(host));
        fields->Set("address6", CompatUtility::GetHostAddress6(host));
 
-       if (service) {
-               fields->Set("check_command_object_id", service->GetCheckCommand());
-               fields->Set("check_command_args", Empty);
-               fields->Set("eventhandler_command_object_id", service->GetEventCommand());
-               fields->Set("eventhandler_command_args", Empty);
-               fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetServiceNotificationNotificationPeriod(service)));
-               fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
-               fields->Set("failure_prediction_options", Empty);
-               fields->Set("check_interval", CompatUtility::GetServiceCheckInterval(service));
-               fields->Set("retry_interval", CompatUtility::GetServiceRetryInterval(service));
-               fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
-
-               fields->Set("first_notification_delay", Empty);
-
-               fields->Set("notification_interval", CompatUtility::GetServiceNotificationNotificationInterval(service));
-               /* requires host check service */
-               fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
-               fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
-
-               fields->Set("notify_on_recovery", CompatUtility::GetServiceNotifyOnRecovery(service));
-               fields->Set("notify_on_flapping", CompatUtility::GetServiceNotifyOnFlapping(service));
-               fields->Set("notify_on_downtime", CompatUtility::GetServiceNotifyOnDowntime(service));
-
-               fields->Set("stalk_on_up", Empty);
-               fields->Set("stalk_on_down", Empty);
-               fields->Set("stalk_on_unreachable", Empty);
-
-               fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service));
-               fields->Set("flap_detection_on_up", Empty);
-               fields->Set("flap_detection_on_down", Empty);
-               fields->Set("flap_detection_on_unreachable", Empty);
-               fields->Set("low_flap_threshold", CompatUtility::GetServiceLowFlapThreshold(service));
-               fields->Set("high_flap_threshold", CompatUtility::GetServiceHighFlapThreshold(service));
-       }
+       fields->Set("check_command_object_id", host->GetCheckCommand());
+       fields->Set("check_command_args", Empty);
+       fields->Set("eventhandler_command_object_id", host->GetEventCommand());
+       fields->Set("eventhandler_command_args", Empty);
+       fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetCheckableNotificationNotificationPeriod(host)));
+       fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
+       fields->Set("failure_prediction_options", Empty);
+       fields->Set("check_interval", CompatUtility::GetCheckableCheckInterval(host));
+       fields->Set("retry_interval", CompatUtility::GetCheckableRetryInterval(host));
+       fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
+
+       fields->Set("first_notification_delay", Empty);
+
+       fields->Set("notification_interval", CompatUtility::GetCheckableNotificationNotificationInterval(host));
+       fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host));
+       fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host));
+
+       fields->Set("notify_on_recovery", CompatUtility::GetCheckableNotifyOnRecovery(host));
+       fields->Set("notify_on_flapping", CompatUtility::GetCheckableNotifyOnFlapping(host));
+       fields->Set("notify_on_downtime", CompatUtility::GetCheckableNotifyOnDowntime(host));
+
+       fields->Set("stalk_on_up", Empty);
+       fields->Set("stalk_on_down", Empty);
+       fields->Set("stalk_on_unreachable", Empty);
+
+       fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
+       fields->Set("flap_detection_on_up", Empty);
+       fields->Set("flap_detection_on_down", Empty);
+       fields->Set("flap_detection_on_unreachable", Empty);
+       fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(host));
+       fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(host));
 
        fields->Set("process_performance_data", 0);
 
-       if (service) {
-               fields->Set("freshness_checks_enabled", CompatUtility::GetServiceFreshnessChecksEnabled(service));
-               fields->Set("freshness_threshold", CompatUtility::GetServiceFreshnessThreshold(service));
-               fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service));
-               fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service));
-               fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service));
-       }
+       fields->Set("freshness_checks_enabled", CompatUtility::GetCheckableFreshnessChecksEnabled(host));
+       fields->Set("freshness_threshold", CompatUtility::GetCheckableFreshnessThreshold(host));
+       fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
+       fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
+       fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
 
        fields->Set("retain_status_information", 1);
        fields->Set("retain_nonstatus_information", 1);
 
-       if (service)
-               fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service));
+       fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
 
        fields->Set("obsess_over_host", 0);
        fields->Set("failure_prediction_enabled", 0);
@@ -130,72 +122,63 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const
 {
        Dictionary::Ptr fields = make_shared<Dictionary>();
        Host::Ptr host = static_pointer_cast<Host>(GetObject());
-       Service::Ptr service = host->GetCheckService();
 
-       /* fetch service status, or dump a pending hoststatus */
-       if (service) {
-               CheckResult::Ptr cr = service->GetLastCheckResult();
+       CheckResult::Ptr cr = host->GetLastCheckResult();
 
-               if (cr) {
-                       fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
-                       fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
-                       fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
-                       fields->Set("check_source", cr->GetCheckSource());
-               }
-
-               fields->Set("current_state", host->GetState());
-               fields->Set("has_been_checked", CompatUtility::GetServiceHasBeenChecked(service));
-               fields->Set("should_be_scheduled", CompatUtility::GetServiceShouldBeScheduled(service));
-               fields->Set("current_check_attempt", service->GetCheckAttempt());
-               fields->Set("max_check_attempts", service->GetMaxCheckAttempts());
-
-               if (cr)
-                       fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
-
-               fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck()));
-               fields->Set("check_type", CompatUtility::GetServiceCheckType(service));
-               fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange()));
-               fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange()));
-               fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
-               fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
-               fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
-               fields->Set("state_type", service->GetStateType());
-               fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationLastNotification(service)));
-               fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationNextNotification(service)));
-               fields->Set("no_more_notifications", Empty);
-               fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service));
-               fields->Set("problem_has_been_acknowledged", CompatUtility::GetServiceProblemHasBeenAcknowledged(service));
-               fields->Set("acknowledgement_type", CompatUtility::GetServiceAcknowledgementType(service));
-               fields->Set("current_notification_number", CompatUtility::GetServiceNotificationNotificationNumber(service));
-               fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service));
-               fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service));
-               fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service));
-               fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service));
-               fields->Set("is_flapping", CompatUtility::GetServiceIsFlapping(service));
-               fields->Set("percent_state_change", CompatUtility::GetServicePercentStateChange(service));
-
-               if (cr) {
-                       fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
-                       fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
-               }
-               fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth());
-               fields->Set("failure_prediction_enabled", Empty);
-               fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */
-               fields->Set("obsess_over_host", Empty);
-               fields->Set("modified_host_attributes", service->GetModifiedAttributes());
-               fields->Set("event_handler", CompatUtility::GetServiceEventHandler(service));
-               fields->Set("check_command", CompatUtility::GetServiceCheckCommand(service));
-               fields->Set("normal_check_interval", CompatUtility::GetServiceCheckInterval(service));
-               fields->Set("retry_check_interval", CompatUtility::GetServiceRetryInterval(service));
-               fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
+       if (cr) {
+               fields->Set("output", CompatUtility::GetCheckResultOutput(cr));
+               fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr));
+               fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr));
+               fields->Set("check_source", cr->GetCheckSource());
        }
-       else {
-               fields->Set("has_been_checked", 0);
-               fields->Set("last_check", DbValue::FromTimestamp(0));
-               fields->Set("next_check", DbValue::FromTimestamp(0));
-               fields->Set("active_checks_enabled", 0);
+
+       fields->Set("current_state", host->IsReachable() ? host->GetState() : 2);
+       fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host));
+       fields->Set("should_be_scheduled", CompatUtility::GetCheckableShouldBeScheduled(host));
+       fields->Set("current_check_attempt", host->GetCheckAttempt());
+       fields->Set("max_check_attempts", host->GetMaxCheckAttempts());
+
+       if (cr)
+               fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd()));
+
+       fields->Set("next_check", DbValue::FromTimestamp(host->GetNextCheck()));
+       fields->Set("check_type", CompatUtility::GetCheckableCheckType(host));
+       fields->Set("last_state_change", DbValue::FromTimestamp(host->GetLastStateChange()));
+       fields->Set("last_hard_state_change", DbValue::FromTimestamp(host->GetLastHardStateChange()));
+       fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp())));
+       fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown())));
+       fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable())));
+       fields->Set("state_type", host->GetStateType());
+       fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(host)));
+       fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(host)));
+       fields->Set("no_more_notifications", Empty);
+       fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
+       fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(host));
+       fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(host));
+       fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(host));
+       fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host));
+       fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host));
+       fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host));
+       fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host));
+       fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(host));
+       fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(host));
+
+       if (cr) {
+               fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr)));
+               fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr)));
        }
 
+       fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth());
+       fields->Set("failure_prediction_enabled", Empty);
+       fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */
+       fields->Set("obsess_over_host", Empty);
+       fields->Set("modified_host_attributes", host->GetModifiedAttributes());
+       fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(host));
+       fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(host));
+       fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(host));
+       fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(host));
+       fields->Set("check_timeperiod_object_id", host->GetCheckPeriod());
+
        return fields;
 }
 
@@ -204,7 +187,12 @@ void HostDbObject::OnConfigUpdate(void)
        Host::Ptr host = static_pointer_cast<Host>(GetObject());
 
        /* parents, host dependencies */
-       BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
+       BOOST_FOREACH(const Checkable::Ptr& checkable, host->GetParents()) {
+               Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);
+
+               if (!parent)
+                       continue;
+
                Log(LogDebug, "db_ido", "host parents: " + parent->GetName());
 
                /* parents: host_id, parent_host_object_id */
@@ -234,60 +222,55 @@ void HostDbObject::OnConfigUpdate(void)
                OnQuery(query2);
        }
 
-       /* host contacts, contactgroups */
-       Service::Ptr service = host->GetCheckService();
-
-       if (service) {
-               Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
+       Log(LogDebug, "db_ido", "host contacts: " + host->GetName());
 
-               BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
-                       Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
+       BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(host)) {
+               Log(LogDebug, "db_ido", "host contacts: " + user->GetName());
 
-                       Dictionary::Ptr fields_contact = make_shared<Dictionary>();
-                       fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
-                       fields_contact->Set("contact_object_id", user);
-                       fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
+               Dictionary::Ptr fields_contact = make_shared<Dictionary>();
+               fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
+               fields_contact->Set("contact_object_id", user);
+               fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
 
-                       DbQuery query_contact;
-                       query_contact.Table = GetType()->GetTable() + "_contacts";
-                       query_contact.Type = DbQueryInsert;
-                       query_contact.Category = DbCatConfig;
-                       query_contact.Fields = fields_contact;
-                       OnQuery(query_contact);
-               }
+               DbQuery query_contact;
+               query_contact.Table = GetType()->GetTable() + "_contacts";
+               query_contact.Type = DbQueryInsert;
+               query_contact.Category = DbCatConfig;
+               query_contact.Fields = fields_contact;
+               OnQuery(query_contact);
+       }
 
-               Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
+       Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
 
-               BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
-                       Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
+       BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(host)) {
+               Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName());
 
-                       Dictionary::Ptr fields_contact = make_shared<Dictionary>();
-                       fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
-                       fields_contact->Set("contactgroup_object_id", usergroup);
-                       fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
+               Dictionary::Ptr fields_contact = make_shared<Dictionary>();
+               fields_contact->Set("host_id", DbValue::FromObjectInsertID(host));
+               fields_contact->Set("contactgroup_object_id", usergroup);
+               fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */
 
-                       DbQuery query_contact;
-                       query_contact.Table = GetType()->GetTable() + "_contactgroups";
-                       query_contact.Type = DbQueryInsert;
-                       query_contact.Category = DbCatConfig;
-                       query_contact.Fields = fields_contact;
-                       OnQuery(query_contact);
-               }
+               DbQuery query_contact;
+               query_contact.Table = GetType()->GetTable() + "_contactgroups";
+               query_contact.Type = DbQueryInsert;
+               query_contact.Category = DbCatConfig;
+               query_contact.Fields = fields_contact;
+               OnQuery(query_contact);
        }
 
        /* custom variables */
-       Dictionary::Ptr customvars;
+       Dictionary::Ptr vars;
        {
                ObjectLock olock(host);
-               customvars = CompatUtility::GetCustomVariableConfig(host);
+               vars = CompatUtility::GetCustomAttributeConfig(host);
        }
 
-       if (customvars) {
-               Log(LogDebug, "ido", "Dumping host customvars for '" + host->GetName() + "'");
+       if (vars) {
+               Log(LogDebug, "ido", "Dumping host vars for '" + host->GetName() + "'");
 
-               ObjectLock olock (customvars);
+               ObjectLock olock (vars);
 
-               BOOST_FOREACH(const Dictionary::Pair& kv, customvars) {
+               BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
                        if (!kv.first.IsEmpty()) {
                                Log(LogDebug, "db_ido", "host customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) + "'");