bool ServiceGroup::EvaluateObjectRule(const Service::Ptr service, const ObjectRule& rule) { DebugInfo di = rule.GetDebugInfo(); std::ostringstream msgbuf; msgbuf << "Evaluating 'object' rule (" << di << ")"; CONTEXT(msgbuf.str()); Host::Ptr host = service->GetHost(); Dictionary::Ptr locals = make_shared<Dictionary>(); locals->Set("host", host); locals->Set("service", service); if (!rule.EvaluateFilter(locals)) return false; std::ostringstream msgbuf2; msgbuf2 << "Assigning membership for group '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di; Log(LogDebug, "icinga", msgbuf2.str()); String group_name = rule.GetName(); ServiceGroup::Ptr group = ServiceGroup::GetByName(group_name); if (!group) { Log(LogCritical, "icinga", "Invalid membership assignment. Group '" + group_name + "' does not exist."); return false; } /* assign service group membership */ group->ResolveGroupMembership(service, true); return true; }
bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) { if (add && rstack > 20) { Log(LogWarning, "ServiceGroup") << "Too many nested groups for group '" << GetName() << "': Service '" << service->GetName() << "' membership assignment failed."; return false; } Array::Ptr groups = GetGroups(); if (groups && groups->GetLength() > 0) { ObjectLock olock(groups); for (const String& name : groups) { ServiceGroup::Ptr group = ServiceGroup::GetByName(name); if (group && !group->ResolveGroupMembership(service, add, rstack + 1)) return false; } } if (add) AddMember(service); else RemoveMember(service); return true; }