/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2013 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 *
#include "base/convert.h"
#include "base/objectlock.h"
#include <boost/foreach.hpp>
-#include <boost/tuple/tuple.hpp>
using namespace icinga;
Dictionary::Ptr HostDbObject::GetConfigFields(void) const
{
- Dictionary::Ptr fields = boost::make_shared<Dictionary>();
+ Dictionary::Ptr fields = make_shared<Dictionary>();
Host::Ptr host = static_pointer_cast<Host>(GetObject());
- Service::Ptr service = host->GetCheckService();
-
- Dictionary::Ptr attrs;
-
- {
- ObjectLock olock(host);
- attrs = CompatUtility::GetHostConfigAttributes(host);
- }
-
- fields->Set("alias", attrs->Get("alias"));
- fields->Set("display_name", attrs->Get("display_name"));
-
- fields->Set("address", attrs->Get("address"));
- fields->Set("address6", attrs->Get("address6"));
-
- 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(attrs->Get("notification_period")));
-
- if (service)
- fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
-
+ fields->Set("alias", CompatUtility::GetHostAlias(host));
+ fields->Set("display_name", host->GetDisplayName());
+ fields->Set("address", CompatUtility::GetHostAddress(host));
+ fields->Set("address6", CompatUtility::GetHostAddress6(host));
+
+ 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", attrs->Get("check_interval"));
- fields->Set("retry_interval", attrs->Get("retry_interval"));
- fields->Set("max_check_attempts", attrs->Get("max_check_attempts"));
+ 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", attrs->Get("notification_interval"));
- fields->Set("notify_on_down", attrs->Get("notify_on_down"));
- fields->Set("notify_on_unreachable", attrs->Get("notify_on_unreachable"));
- fields->Set("notify_on_recovery", attrs->Get("notify_on_recovery"));
- fields->Set("notify_on_flapping", attrs->Get("notify_on_flapping"));
- fields->Set("notify_on_downtime", attrs->Get("notify_on_downtime"));
+
+ 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", attrs->Get("flap_detection_enabled"));
+ 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", attrs->Get("low_flap_threshold"));
- fields->Set("high_flap_threshold", attrs->Get("high_flap_threshold"));
- fields->Set("process_performance_data", attrs->Get("process_performance_data"));
- fields->Set("freshness_checks_enabled", attrs->Get("check_freshness"));
- fields->Set("freshness_threshold", Empty);
- fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled"));
- fields->Set("event_handler_enabled", attrs->Get("event_handler_enabled"));
- fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled"));
+ fields->Set("low_flap_threshold", CompatUtility::GetCheckableLowFlapThreshold(host));
+ fields->Set("high_flap_threshold", CompatUtility::GetCheckableHighFlapThreshold(host));
+
+ fields->Set("process_performance_data", 0);
+
+ 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);
- fields->Set("notifications_enabled", attrs->Get("notifications_enabled"));
+
+ fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host));
+
fields->Set("obsess_over_host", 0);
fields->Set("failure_prediction_enabled", 0);
- fields->Set("notes", attrs->Get("notes"));
- fields->Set("notes_url", attrs->Get("notes_url"));
- fields->Set("action_url", attrs->Get("action_url"));
- fields->Set("icon_image", attrs->Get("icon_image"));
- fields->Set("icon_image_alt", attrs->Get("icon_image_alt"));
- fields->Set("statusmap_image", attrs->Get("statusmap_image"));
- fields->Set("have_2d_coords", attrs->Get("have_2d_coords"));
- fields->Set("x_2d", attrs->Get("x_2d"));
- fields->Set("y_2d", attrs->Get("y_2d"));
+ fields->Set("notes", CompatUtility::GetCustomAttributeConfig(host, "notes"));
+ fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(host, "notes_url"));
+ fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(host, "action_url"));
+ fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(host, "icon_image"));
+ fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt"));
+ fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"));
+
+ Host2dCoords coords = CompatUtility::GetHost2dCoords(host);
+
+ fields->Set("have_2d_coords", coords.have_2d_coords);
+
+ if (coords.have_2d_coords) {
+ fields->Set("x_2d", coords.x_2d);
+ fields->Set("y_2d", coords.y_2d);
+ }
+
/* deprecated in 1.x */
fields->Set("have_3d_coords", 0);
Dictionary::Ptr HostDbObject::GetStatusFields(void) const
{
- Dictionary::Ptr fields = boost::make_shared<Dictionary>();
+ Dictionary::Ptr fields = make_shared<Dictionary>();
Host::Ptr host = static_pointer_cast<Host>(GetObject());
- Service::Ptr service = host->GetCheckService();
-
- Dictionary::Ptr attrs;
-
- /* fetch service status, or dump a pending hoststatus */
- if (service) {
- ObjectLock olock(service);
- attrs = CompatUtility::GetServiceStatusAttributes(service, CompatTypeHost);
-
- fields->Set("output", attrs->Get("plugin_output"));
- fields->Set("long_output", attrs->Get("long_plugin_output"));
- fields->Set("perfdata", attrs->Get("performance_data"));
- fields->Set("check_source", attrs->Get("check_source"));
- fields->Set("current_state", attrs->Get("current_state"));
- fields->Set("has_been_checked", attrs->Get("has_been_checked"));
- fields->Set("should_be_scheduled", attrs->Get("should_be_scheduled"));
- fields->Set("current_check_attempt", attrs->Get("current_attempt"));
- fields->Set("max_check_attempts", attrs->Get("max_attempts"));
- fields->Set("last_check", DbValue::FromTimestamp(attrs->Get("last_check")));
- fields->Set("next_check", DbValue::FromTimestamp(attrs->Get("next_check")));
- fields->Set("check_type", attrs->Get("check_type"));
- fields->Set("last_state_change", DbValue::FromTimestamp(attrs->Get("last_state_change")));
- fields->Set("last_hard_state_change", DbValue::FromTimestamp(attrs->Get("last_hard_state_change")));
- fields->Set("last_time_up", DbValue::FromTimestamp(attrs->Get("last_time_up")));
- fields->Set("last_time_down", DbValue::FromTimestamp(attrs->Get("last_time_down")));
- fields->Set("last_time_unreachable", DbValue::FromTimestamp(attrs->Get("last_time_unreachable")));
- fields->Set("state_type", attrs->Get("state_type"));
- fields->Set("last_notification", DbValue::FromTimestamp(attrs->Get("last_notification")));
- fields->Set("next_notification", DbValue::FromTimestamp(attrs->Get("next_notification")));
- fields->Set("no_more_notifications", Empty);
- fields->Set("notifications_enabled", attrs->Get("notifications_enabled"));
- fields->Set("problem_has_been_acknowledged", attrs->Get("problem_has_been_acknowledged"));
- fields->Set("acknowledgement_type", attrs->Get("acknowledgement_type"));
- fields->Set("current_notification_number", attrs->Get("current_notification_number"));
- fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled"));
- fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled"));
- fields->Set("eventhandler_enabled", attrs->Get("eventhandler_enabled"));
- fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled"));
- fields->Set("is_flapping", attrs->Get("is_flapping"));
- fields->Set("percent_state_change", attrs->Get("percent_state_change"));
- fields->Set("latency", attrs->Get("check_latency"));
- fields->Set("execution_time", attrs->Get("check_execution_time"));
- fields->Set("scheduled_downtime_depth", attrs->Get("scheduled_downtime_depth"));
- fields->Set("failure_prediction_enabled", Empty);
- fields->Set("process_performance_data", attrs->Get("process_performance_data"));
- fields->Set("obsess_over_host", Empty);
- fields->Set("modified_host_attributes", Empty);
- fields->Set("event_handler", attrs->Get("event_handler"));
- fields->Set("check_command", attrs->Get("check_command"));
- fields->Set("normal_check_interval", attrs->Get("check_interval"));
- fields->Set("retry_check_interval", attrs->Get("retry_interval"));
- fields->Set("check_timeperiod_object_id", service->GetCheckPeriod());
+
+ 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());
}
- 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;
}
Host::Ptr host = static_pointer_cast<Host>(GetObject());
/* parents, host dependencies */
- DbQuery query_del1;
- query_del1.Table = GetType()->GetTable() + "_parenthosts";
- query_del1.Type = DbQueryDelete;
- query_del1.WhereCriteria = boost::make_shared<Dictionary>();
- query_del1.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
- OnQuery(query_del1);
-
- DbQuery query_del2;
- query_del2.Table = GetType()->GetTable() + "dependencies";
- query_del2.Type = DbQueryDelete;
- query_del2.WhereCriteria = boost::make_shared<Dictionary>();
- query_del2.WhereCriteria->Set("dependent_host_object_id", host);
- OnQuery(query_del2);
-
- BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) {
- Log(LogDebug, "ido", "host parents: " + parent->GetName());
+ 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 */
- Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
+ Dictionary::Ptr fields1 = make_shared<Dictionary>();
fields1->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));
fields1->Set("parent_host_object_id", parent);
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
DbQuery query1;
query1.Table = GetType()->GetTable() + "_parenthosts";
query1.Type = DbQueryInsert;
+ query1.Category = DbCatConfig;
query1.Fields = fields1;
OnQuery(query1);
/* host dependencies */
- Dictionary::Ptr fields2 = boost::make_shared<Dictionary>();
+ Dictionary::Ptr fields2 = make_shared<Dictionary>();
fields2->Set("host_object_id", parent);
fields2->Set("dependent_host_object_id", host);
fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
DbQuery query2;
query2.Table = GetType()->GetTable() + "dependencies";
query2.Type = DbQueryInsert;
+ query2.Category = DbCatConfig;
query2.Fields = fields2;
OnQuery(query2);
}
- /* custom variables */
- Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'");
+ Log(LogDebug, "db_ido", "host contacts: " + host->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 */
+
+ 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_del3;
- query_del3.Table = "customvariables";
- query_del3.Type = DbQueryDelete;
- query_del3.WhereCriteria = boost::make_shared<Dictionary>();
- query_del3.WhereCriteria->Set("object_id", host);
- OnQuery(query_del3);
+ Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName());
- Dictionary::Ptr customvars;
+ 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 */
+
+ 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 vars;
{
ObjectLock olock(host);
- customvars = CompatUtility::GetCustomVariableConfig(host);
+ vars = CompatUtility::GetCustomAttributeConfig(host);
}
- if (customvars) {
- ObjectLock olock (customvars);
-
- String key;
- Value value;
- BOOST_FOREACH(boost::tie(key, value), customvars) {
- Log(LogDebug, "ido", "host customvar key: '" + key + "' value: '" + Convert::ToString(value) + "'");
-
- Dictionary::Ptr fields3 = boost::make_shared<Dictionary>();
- fields3->Set("varname", Convert::ToString(key));
- fields3->Set("varvalue", Convert::ToString(value));
- fields3->Set("config_type", 1);
- fields3->Set("has_been_modified", 0);
- fields3->Set("object_id", host);
- fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
-
- DbQuery query3;
- query3.Table = "customvariables";
- query3.Type = DbQueryInsert;
- query3.Fields = fields3;
- OnQuery(query3);
+ if (vars) {
+ Log(LogDebug, "ido", "Dumping host vars for '" + host->GetName() + "'");
+
+ ObjectLock olock (vars);
+
+ 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) + "'");
+
+ Dictionary::Ptr fields3 = make_shared<Dictionary>();
+ fields3->Set("varname", Convert::ToString(kv.first));
+ fields3->Set("varvalue", Convert::ToString(kv.second));
+ fields3->Set("config_type", 1);
+ fields3->Set("has_been_modified", 0);
+ fields3->Set("object_id", host);
+ fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */
+
+ DbQuery query3;
+ query3.Table = "customvariables";
+ query3.Type = DbQueryInsert;
+ query3.Category = DbCatConfig;
+ query3.Fields = fields3;
+ OnQuery(query3);
+ }
}
}
}