コード例 #1
0
ファイル: hostgroup.cpp プロジェクト: dupondje/icinga2
bool HostGroup::ResolveGroupMembership(const Host::Ptr& host, bool add, int rstack) {

	if (add && rstack > 20) {
		Log(LogWarning, "HostGroup")
			<< "Too many nested groups for group '" << GetName() << "': Host '"
			<< host->GetName() << "' membership assignment failed.";

		return false;
	}

	Array::Ptr groups = GetGroups();

	if (groups && groups->GetLength() > 0) {
		ObjectLock olock(groups);

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

			if (group && !group->ResolveGroupMembership(host, add, rstack + 1))
				return false;
		}
	}

	if (add)
		AddMember(host);
	else
		RemoveMember(host);

	return true;
}
コード例 #2
0
ファイル: hostgroup.cpp プロジェクト: 3v/icinga2
bool HostGroup::EvaluateObjectRuleOne(const Host::Ptr& host, const ObjectRule& rule)
{
	DebugInfo di = rule.GetDebugInfo();

	std::ostringstream msgbuf;
	msgbuf << "Evaluating 'object' rule (" << di << ")";
	CONTEXT(msgbuf.str());

	Dictionary::Ptr locals = make_shared<Dictionary>();
	locals->Set("host", host);

	if (!rule.EvaluateFilter(locals))
		return false;

	std::ostringstream msgbuf2;
	msgbuf2 << "Assigning membership for group '" << rule.GetName() << "' to host '" << host->GetName() << "' for rule " << di;
	Log(LogDebug, "HostGroup", msgbuf2.str());

	String group_name = rule.GetName();
	HostGroup::Ptr group = HostGroup::GetByName(group_name);

	if (!group) {
		Log(LogCritical, "HostGroup", "Invalid membership assignment. Group '" + group_name + "' does not exist.");
		return false;
	}

	/* assign host group membership */
	group->ResolveGroupMembership(host, true);

	/* update groups attribute for apply */
	host->AddGroup(group_name);

	return true;
}
コード例 #3
0
ファイル: hostgroupdbobject.cpp プロジェクト: haus/icinga2
void HostGroupDbObject::OnConfigUpdate(void)
{
	HostGroup::Ptr group = static_pointer_cast<HostGroup>(GetObject());

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

	BOOST_FOREACH(const Host::Ptr& host, group->GetMembers()) {
		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);
		OnQuery(query2);
	}
}
コード例 #4
0
ファイル: hostgroupdbobject.cpp プロジェクト: 3v/icinga2
Dictionary::Ptr HostGroupDbObject::GetConfigFields(void) const
{
	Dictionary::Ptr fields = make_shared<Dictionary>();
	HostGroup::Ptr group = static_pointer_cast<HostGroup>(GetObject());

	fields->Set("alias", group->GetDisplayName());
	fields->Set("notes", group->GetNotes());
	fields->Set("notes_url", group->GetNotesUrl());
	fields->Set("action_url", group->GetActionUrl());

	return fields;
}
コード例 #5
0
ファイル: statusdatawriter.cpp プロジェクト: dupondje/icinga2
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";
}