Example #1
0
bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigItem::Ptr& group)
{
	String group_name = group->GetName();

	CONTEXT("Evaluating rule for group '" + group_name + "'");

	Host::Ptr host = service->GetHost();

	ScriptFrame frame;
	if (group->GetScope())
		group->GetScope()->CopyTo(frame.Locals);
	frame.Locals->Set("host", host);
	frame.Locals->Set("service", service);

	if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool())
		return false;

	Log(LogDebug, "ServiceGroup")
	    << "Assigning membership for group '" << group_name << "' to service '" << service->GetName() << "'";

	Array::Ptr groups = service->GetGroups();
	groups->Add(group_name);

	return true;
}
String ServiceDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
{
	String hashData = DbObject::CalculateConfigHash(configFields);

	Service::Ptr service = static_pointer_cast<Service>(GetObject());

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

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

	Array::Ptr dependencies = new Array();

	/* dependencies */
	for (const Dependency::Ptr& dep : service->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(service)) {
		users->Add(user->GetName());
	}

	users->Sort();

	hashData += DbObject::HashValue(users);

	Array::Ptr userGroups = new Array();

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

	userGroups->Sort();

	hashData += DbObject::HashValue(userGroups);

	return SHA256(hashData);
}
Example #3
0
Value ServicesTable::GroupsAccessor(const Value& row)
{
	Service::Ptr service = static_cast<Service::Ptr>(row);

	if (!service)
		return Empty;

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

	if (!groups)
		return Empty;

	return groups;
}
Example #4
0
void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
{
	Host::Ptr host = service->GetHost();

	{
		ObjectLock olock(service);

		fp << "define service {" "\n"
			"\t" "host_name" "\t" << host->GetName() << "\n"
			"\t" "service_description" "\t" << service->GetShortName() << "\n"
			"\t" "display_name" "\t" << service->GetDisplayName() << "\n"
			"\t" "check_interval" "\t" << (service->GetCheckInterval() / 60.0) << "\n"
			"\t" "retry_interval" "\t" << (service->GetRetryInterval() / 60.0) << "\n"
			"\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
			"\t" "active_checks_enabled" "\t" << Convert::ToLong(service->GetEnableActiveChecks()) << "\n"
			"\t" "passive_checks_enabled" "\t" << Convert::ToLong(service->GetEnablePassiveChecks()) << "\n"
			"\t" "flap_detection_enabled" "\t" << Convert::ToLong(service->GetEnableFlapping()) << "\n"
			"\t" "is_volatile" "\t" << Convert::ToLong(service->GetVolatile()) << "\n"
			"\t" "notifications_enabled" "\t" << Convert::ToLong(service->GetEnableNotifications()) << "\n"
			"\t" "notification_options" "\t" << GetNotificationOptions(service) << "\n"
			"\t" "notification_interval" "\t" << CompatUtility::GetCheckableNotificationNotificationInterval(service) << "\n"
			"\t" "notification_period" "\t" << "" << "\n"
			"\t" "event_handler_enabled" "\t" << Convert::ToLong(service->GetEnableEventHandler()) << "\n";

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

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

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

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

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

		String notes = service->GetNotes();
		String notes_url = service->GetNotesUrl();
		String action_url = service->GetActionUrl();
		String icon_image = service->GetIconImage();
		String icon_image_alt = service->GetIconImageAlt();

		fp << "\t" "initial_state" "\t" "o" "\n"
			"\t" "low_flap_threshold" "\t" << service->GetFlappingThresholdLow() << "\n"
			"\t" "high_flap_threshold" "\t" << service->GetFlappingThresholdHigh() << "\n"
			"\t" "process_perf_data" "\t" << Convert::ToLong(service->GetEnablePerfdata()) << "\n"
			"\t" "check_freshness" << "\t" "1" "\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";
	}

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

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

	if (groups) {
		ObjectLock olock(groups);

		for (const String& name : groups) {
			ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);

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

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

	fp << "\n";

	DumpCustomAttributes(fp, service);

	fp << "\t" "}" "\n" "\n";
}
void ServiceDbObject::OnConfigUpdateHeavy(void)
{
	Service::Ptr service = static_pointer_cast<Service>(GetObject());

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

	std::vector<DbQuery> queries;

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

	queries.push_back(query1);

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

			DbQuery query2;
			query2.Table = DbType::GetByName("ServiceGroup")->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("servicegroup_id", DbValue::FromObjectInsertID(group));
			query2.Fields->Set("service_object_id", service);
			query2.WhereCriteria = new Dictionary();
			query2.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
			query2.WhereCriteria->Set("servicegroup_id", DbValue::FromObjectInsertID(group));
			query2.WhereCriteria->Set("service_object_id", service);

			queries.push_back(query2);
		}
	}

	DbObject::OnMultipleQueries(queries);

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

	queries.clear();

	DbQuery query2;
	query2.Table = GetType()->GetTable() + "dependencies";
	query2.Type = DbQueryDelete;
	query2.Category = DbCatConfig;
	query2.WhereCriteria = new Dictionary();
	query2.WhereCriteria->Set("dependent_service_object_id", service);

	queries.push_back(query2);

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

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

		Log(LogDebug, "ServiceDbObject")
		    << "service parents: " << parent->GetName();

		int state_filter = dep->GetStateFilter();

		/* service dependencies */
		Dictionary::Ptr fields1 = new Dictionary();
		fields1->Set("service_object_id", parent);
		fields1->Set("dependent_service_object_id", service);
		fields1->Set("inherits_parent", 1);
		fields1->Set("timeperiod_object_id", dep->GetPeriod());
		fields1->Set("fail_on_ok", (state_filter & StateFilterOK) ? 1 : 0);
		fields1->Set("fail_on_warning", (state_filter & StateFilterWarning) ? 1 : 0);
		fields1->Set("fail_on_critical", (state_filter & StateFilterCritical) ? 1 : 0);
		fields1->Set("fail_on_unknown", (state_filter & StateFilterUnknown) ? 1 : 0);
		fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */

		DbQuery query1;
		query1.Table = GetType()->GetTable() + "dependencies";
		query1.Type = DbQueryInsert;
		query1.Category = DbCatConfig;
		query1.Fields = fields1;

		queries.push_back(query1);
	}

	DbObject::OnMultipleQueries(queries);

	/* service contacts, contactgroups */
	Log(LogDebug, "ServiceDbObject")
	    << "service contacts: " << service->GetName();

	queries.clear();

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

	queries.push_back(query3);

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

		Dictionary::Ptr fields_contact = new Dictionary();
		fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
		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, "ServiceDbObject")
	    << "service contactgroups: " << service->GetName();

	queries.clear();

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

	queries.push_back(query4);

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

		Dictionary::Ptr fields_contact = new Dictionary();
		fields_contact->Set("service_id", DbValue::FromObjectInsertID(service));
		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();
}