void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
{
	String notes = host->GetNotes();
	String notes_url = host->GetNotesUrl();
	String action_url = host->GetActionUrl();
	String icon_image = host->GetIconImage();
	String icon_image_alt = host->GetIconImageAlt();
	String display_name = host->GetDisplayName();
	String address = host->GetAddress();
	String address6 = host->GetAddress6();

	fp << "define host {" "\n"
		"\t" "host_name" "\t" << host->GetName() << "\n";
	if (!display_name.IsEmpty()) {
		fp << "\t" "display_name" "\t" << host->GetDisplayName() << "\n"
			"\t" "alias" "\t" << host->GetDisplayName() << "\n";
	}
	if (!address.IsEmpty())
		fp << "\t" "address" "\t" << address << "\n";
	if (!address6.IsEmpty())
		fp << "\t" "address6" "\t" << address6 << "\n";
	if (!notes.IsEmpty())
		fp << "\t" "notes" "\t" << notes << "\n";
	if (!notes_url.IsEmpty())
		fp << "\t" "notes_url" "\t" << notes_url << "\n";
	if (!action_url.IsEmpty())
		fp << "\t" "action_url" "\t" << action_url << "\n";
	if (!icon_image.IsEmpty())
		fp << "\t" "icon_image" "\t" << icon_image << "\n";
	if (!icon_image_alt.IsEmpty())
		fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";

	std::set<Checkable::Ptr> parents = host->GetParents();

	if (!parents.empty()) {
		fp << "\t" "parents" "\t";
		DumpNameList(fp, parents);
		fp << "\n";
	}

	ObjectLock olock(host);

	fp << "\t" "check_interval" "\t" << (host->GetCheckInterval() / 60.0) << "\n"
		"\t" "retry_interval" "\t" << (host->GetRetryInterval() / 60.0) << "\n"
		"\t" "max_check_attempts" "\t" << host->GetMaxCheckAttempts() << "\n"
		"\t" "active_checks_enabled" "\t" << Convert::ToLong(host->GetEnableActiveChecks()) << "\n"
		"\t" "passive_checks_enabled" "\t" << Convert::ToLong(host->GetEnablePassiveChecks()) << "\n"
		"\t" "notifications_enabled" "\t" << Convert::ToLong(host->GetEnableNotifications()) << "\n"
		"\t" "notification_options" "\t" << GetNotificationOptions(host) << "\n"
		"\t" "notification_interval" "\t" << CompatUtility::GetCheckableNotificationNotificationInterval(host) << "\n"
		"\t" "event_handler_enabled" "\t" << Convert::ToLong(host->GetEnableEventHandler()) << "\n";

	CheckCommand::Ptr checkcommand = host->GetCheckCommand();
	if (checkcommand)
		fp << "\t" "check_command" "\t" << CompatUtility::GetCommandName(checkcommand) << "!" << CompatUtility::GetCheckableCommandArgs(host) << "\n";

	EventCommand::Ptr eventcommand = host->GetEventCommand();
	if (eventcommand && host->GetEnableEventHandler())
		fp << "\t" "event_handler" "\t" << CompatUtility::GetCommandName(eventcommand) << "\n";

	TimePeriod::Ptr checkPeriod = host->GetCheckPeriod();
	if (checkPeriod)
		fp << "\t" "check_period" "\t" << checkPeriod->GetName() << "\n";

	fp << "\t" "contacts" "\t";
	DumpNameList(fp, CompatUtility::GetCheckableNotificationUsers(host));
	fp << "\n";

	fp << "\t" "contact_groups" "\t";
	DumpNameList(fp, CompatUtility::GetCheckableNotificationUserGroups(host));
	fp << "\n";

	fp << "\t" << "initial_state" "\t" "o" "\n"
		"\t" "low_flap_threshold" "\t" << host->GetFlappingThresholdLow() << "\n"
		"\t" "high_flap_threshold" "\t" << host->GetFlappingThresholdHigh() << "\n"
		"\t" "process_perf_data" "\t" << Convert::ToLong(host->GetEnablePerfdata()) << "\n"
		"\t" "check_freshness" "\t" "1" "\n";

	fp << "\t" "host_groups" "\t";
	bool first = true;

	Array::Ptr groups = host->GetGroups();

	if (groups) {
		ObjectLock olock(groups);

		for (const String& name : groups) {
			HostGroup::Ptr hg = HostGroup::GetByName(name);

			if (hg) {
				if (!first)
					fp << ",";
				else
					first = false;

				fp << hg->GetName();
			}
		}
	}

	fp << "\n";

	DumpCustomAttributes(fp, host);

	fp << "\t" "}" "\n" "\n";
}
String HostDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
{
	String hashData = DbObject::CalculateConfigHash(configFields);

	Host::Ptr host = static_pointer_cast<Host>(GetObject());

	Array::Ptr groups = host->GetGroups();

	if (groups)
		hashData += DbObject::HashValue(groups);

	Array::Ptr parents = new Array();

	/* parents */
	for (const Checkable::Ptr& checkable : host->GetParents()) {
		Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);

		if (!parent)
			continue;

		parents->Add(parent->GetName());
	}

	parents->Sort();

	hashData += DbObject::HashValue(parents);

	Array::Ptr dependencies = new Array();

	/* dependencies */
	for (const Dependency::Ptr& dep : host->GetDependencies()) {
		Checkable::Ptr parent = dep->GetParent();

		if (!parent)
			continue;

		Array::Ptr depInfo = new Array();
		depInfo->Add(parent->GetName());
		depInfo->Add(dep->GetStateFilter());
		depInfo->Add(dep->GetPeriodRaw());

		dependencies->Add(depInfo);
	}

	dependencies->Sort();

	hashData += DbObject::HashValue(dependencies);

	Array::Ptr users = new Array();

	for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
		users->Add(user->GetName());
	}

	users->Sort();

	hashData += DbObject::HashValue(users);

	Array::Ptr userGroups = new Array();

	for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
		userGroups->Add(usergroup->GetName());
	}

	userGroups->Sort();

	hashData += DbObject::HashValue(userGroups);

	return SHA256(hashData);
}
Exemple #3
0
void HostDbObject::OnConfigUpdate(void)
{
	Host::Ptr host = static_pointer_cast<Host>(GetObject());

	/* parents */
	BOOST_FOREACH(const Checkable::Ptr& checkable, host->GetParents()) {
		Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);

		if (!parent)
			continue;

		Log(LogDebug, "HostDbObject")
		    << "host parents: " << parent->GetName();

		/* parents: host_id, parent_host_object_id */
		Dictionary::Ptr fields1 = new 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 */
	Log(LogDebug, "HostDbObject")
	    << "host dependencies for '" << host->GetName() << "'";

	BOOST_FOREACH(const Dependency::Ptr& dep, host->GetDependencies()) {
		Checkable::Ptr parent = dep->GetParent();

		if (!parent) {
			Log(LogDebug, "HostDbObject")
			    << "Missing parent for dependency '" << dep->GetName() << "'.";
			continue;
		}

		int state_filter = dep->GetStateFilter();

		Log(LogDebug, "HostDbObject")
		    << "parent host: " << parent->GetName();

		Dictionary::Ptr fields2 = new Dictionary();
		fields2->Set("host_object_id", parent);
		fields2->Set("dependent_host_object_id", host);
		fields2->Set("inherits_parent", 1);
		fields2->Set("timeperiod_object_id", dep->GetPeriod());
		fields2->Set("fail_on_up", (state_filter & StateFilterUp) ? 1 : 0);
		fields2->Set("fail_on_down", (state_filter & StateFilterDown) ? 1 : 0);
		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);
	}

	Log(LogDebug, "HostDbObject")
	    << "host contacts: " << host->GetName();

	BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetCheckableNotificationUsers(host)) {
		Log(LogDebug, "HostDbObject")
		    << "host contacts: " << user->GetName();

		Dictionary::Ptr fields_contact = new 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);
	}
void HostDbObject::OnConfigUpdateHeavy(void)
{
	Host::Ptr host = static_pointer_cast<Host>(GetObject());

	/* groups */
	Array::Ptr groups = host->GetGroups();

	std::vector<DbQuery> queries;

	DbQuery query1;
	query1.Table = DbType::GetByName("HostGroup")->GetTable() + "_members";
	query1.Type = DbQueryDelete;
	query1.Category = DbCatConfig;
	query1.WhereCriteria = new Dictionary();
	query1.WhereCriteria->Set("host_object_id", host);

	queries.push_back(query1);

	if (groups) {
		ObjectLock olock(groups);
		for (const String& groupName : groups) {
			HostGroup::Ptr group = HostGroup::GetByName(groupName);

			DbQuery query2;
			query2.Table = DbType::GetByName("HostGroup")->GetTable() + "_members";
			query2.Type = DbQueryInsert;
			query2.Category = DbCatConfig;
			query2.Fields = new Dictionary();
			query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
			query2.Fields->Set("hostgroup_id", DbValue::FromObjectInsertID(group));
			query2.Fields->Set("host_object_id", host);
			query2.WhereCriteria = new Dictionary();
			query2.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
			query2.WhereCriteria->Set("hostgroup_id", DbValue::FromObjectInsertID(group));
			query2.WhereCriteria->Set("host_object_id", host);

			queries.push_back(query2);
		}
	}

	DbObject::OnMultipleQueries(queries);

	queries.clear();

	DbQuery query2;
	query2.Table = GetType()->GetTable() + "_parenthosts";
	query2.Type = DbQueryDelete;
	query2.Category = DbCatConfig;
	query2.WhereCriteria = new Dictionary();
	query2.WhereCriteria->Set(GetType()->GetTable() + "_id", DbValue::FromObjectInsertID(GetObject()));

	queries.push_back(query2);

	/* parents */
	for (const Checkable::Ptr& checkable : host->GetParents()) {
		Host::Ptr parent = dynamic_pointer_cast<Host>(checkable);

		if (!parent)
			continue;

		Log(LogDebug, "HostDbObject")
		    << "host parents: " << parent->GetName();

		/* parents: host_id, parent_host_object_id */
		Dictionary::Ptr fields1 = new 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;

		queries.push_back(query1);
	}

	DbObject::OnMultipleQueries(queries);

	/* host dependencies */
	Log(LogDebug, "HostDbObject")
	    << "host dependencies for '" << host->GetName() << "'";

	queries.clear();

	DbQuery query3;
	query3.Table = GetType()->GetTable() + "dependencies";
	query3.Type = DbQueryDelete;
	query3.Category = DbCatConfig;
	query3.WhereCriteria = new Dictionary();
	query3.WhereCriteria->Set("dependent_host_object_id", host);

	queries.push_back(query3);

	for (const Dependency::Ptr& dep : host->GetDependencies()) {
		Checkable::Ptr parent = dep->GetParent();

		if (!parent) {
			Log(LogDebug, "HostDbObject")
			    << "Missing parent for dependency '" << dep->GetName() << "'.";
			continue;
		}

		int state_filter = dep->GetStateFilter();

		Log(LogDebug, "HostDbObject")
		    << "parent host: " << parent->GetName();

		Dictionary::Ptr fields2 = new Dictionary();
		fields2->Set("host_object_id", parent);
		fields2->Set("dependent_host_object_id", host);
		fields2->Set("inherits_parent", 1);
		fields2->Set("timeperiod_object_id", dep->GetPeriod());
		fields2->Set("fail_on_up", (state_filter & StateFilterUp) ? 1 : 0);
		fields2->Set("fail_on_down", (state_filter & StateFilterDown) ? 1 : 0);
		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;

		queries.push_back(query2);
	}

	DbObject::OnMultipleQueries(queries);

	Log(LogDebug, "HostDbObject")
	    << "host contacts: " << host->GetName();

	queries.clear();

	DbQuery query4;
	query4.Table = GetType()->GetTable() + "_contacts";
	query4.Type = DbQueryDelete;
	query4.Category = DbCatConfig;
	query4.WhereCriteria = new Dictionary();
	query4.WhereCriteria->Set("host_id", DbValue::FromObjectInsertID(host));

	queries.push_back(query4);

	for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(host)) {
		Log(LogDebug, "HostDbObject")
		    << "host contacts: " << user->GetName();

		Dictionary::Ptr fields_contact = new 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;

		queries.push_back(query_contact);
	}

	DbObject::OnMultipleQueries(queries);

	Log(LogDebug, "HostDbObject")
	    << "host contactgroups: " << host->GetName();

	queries.clear();

	DbQuery query5;
	query5.Table = GetType()->GetTable() + "_contactgroups";
	query5.Type = DbQueryDelete;
	query5.Category = DbCatConfig;
	query5.WhereCriteria = new Dictionary();
	query5.WhereCriteria->Set("host_id", DbValue::FromObjectInsertID(host));

	queries.push_back(query5);

	for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(host)) {
		Log(LogDebug, "HostDbObject")
		    << "host contactgroups: " << usergroup->GetName();

		Dictionary::Ptr fields_contact = new 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;

		queries.push_back(query_contact);
	}

	DbObject::OnMultipleQueries(queries);

	DoCommonConfigUpdate();
}