/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012 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 *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
-#include "i2-icinga.h"
+#include "icinga/servicegroup.hpp"
+#include "config/objectrule.hpp"
+#include "config/configitem.hpp"
+#include "base/dynamictype.hpp"
+#include "base/objectlock.hpp"
+#include "base/logger.hpp"
+#include "base/context.hpp"
+#include "base/workqueue.hpp"
+#include <boost/foreach.hpp>
using namespace icinga;
-boost::mutex ServiceGroup::m_Mutex;
-map<String, vector<Service::WeakPtr> > ServiceGroup::m_MembersCache;
-bool ServiceGroup::m_MembersCacheNeedsUpdate = false;
-Timer::Ptr ServiceGroup::m_MembersCacheTimer;
-
REGISTER_TYPE(ServiceGroup);
-ServiceGroup::ServiceGroup(const Dictionary::Ptr& properties)
- : DynamicObject(properties)
-{
- RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
- RegisterAttribute("notes_url", Attribute_Config, &m_NotesUrl);
- RegisterAttribute("action_url", Attribute_Config, &m_ActionUrl);
-}
+INITIALIZE_ONCE(&ServiceGroup::RegisterObjectRuleHandler);
-ServiceGroup::~ServiceGroup(void)
+void ServiceGroup::RegisterObjectRuleHandler(void)
{
- InvalidateMembersCache();
+ ObjectRule::RegisterType("ServiceGroup");
}
-/**
- * @threadsafety Always.
- */
-void ServiceGroup::OnRegistrationCompleted(void)
+bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigItem::Ptr& group)
{
- assert(!OwnsLock());
+ String group_name = group->GetName();
- InvalidateMembersCache();
-}
+ CONTEXT("Evaluating rule for group '" + group_name + "'");
-/**
- * @threadsafety Always.
- */
-String ServiceGroup::GetDisplayName(void) const
-{
- if (!m_DisplayName.Get().IsEmpty())
- return m_DisplayName;
- else
- return GetName();
-}
+ Host::Ptr host = service->GetHost();
-/**
- * @threadsafety Always.
- */
-String ServiceGroup::GetNotesUrl(void) const
-{
- return m_NotesUrl;
-}
+ ScriptFrame frame;
+ if (group->GetScope())
+ group->GetScope()->CopyTo(frame.Locals);
+ frame.Locals->Set("host", host);
+ frame.Locals->Set("service", service);
-/**
- * @threadsafety Always.
- */
-String ServiceGroup::GetActionUrl(void) const
-{
- return m_ActionUrl;
-}
+ if (!group->GetFilter()->Evaluate(frame))
+ return false;
-/**
- * @threadsafety Always.
- */
-ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
-{
- DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
+ Log(LogDebug, "ServiceGroup")
+ << "Assigning membership for group '" << group_name << "' to service '" << service->GetName() << "'";
- if (!configObject)
- BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
+ Array::Ptr groups = service->GetGroups();
+ groups->Add(group_name);
- return dynamic_pointer_cast<ServiceGroup>(configObject);
+ return true;
}
-/**
- * @threadsafety Always.
- */
-set<Service::Ptr> ServiceGroup::GetMembers(void) const
+void ServiceGroup::EvaluateObjectRules(const Service::Ptr& service)
{
- set<Service::Ptr> services;
+ CONTEXT("Evaluating group membership for service '" + service->GetName() + "'");
+ BOOST_FOREACH(const ConfigItem::Ptr& group, ConfigItem::GetItems("ServiceGroup"))
{
- boost::mutex::scoped_lock lock(m_Mutex);
-
- BOOST_FOREACH(const Service::WeakPtr& wservice, m_MembersCache[GetName()]) {
- Service::Ptr service = wservice.lock();
+ if (!group->GetFilter())
+ continue;
- if (!service)
- continue;
-
- services.insert(service);
- }
+ EvaluateObjectRule(service, group);
}
-
- return services;
}
-/**
- * @threadsafety Always.
- */
-void ServiceGroup::InvalidateMembersCache(void)
+std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
{
- boost::mutex::scoped_lock lock(m_Mutex);
-
- if (m_MembersCacheNeedsUpdate)
- return; /* Someone else has already requested a refresh. */
+ boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+ return m_Members;
+}
- if (!m_MembersCacheTimer) {
- m_MembersCacheTimer = boost::make_shared<Timer>();
- m_MembersCacheTimer->SetInterval(0.5);
- m_MembersCacheTimer->OnTimerExpired.connect(boost::bind(&ServiceGroup::RefreshMembersCache));
- m_MembersCacheTimer->Start();
- }
+void ServiceGroup::AddMember(const Service::Ptr& service)
+{
+ service->AddGroup(GetName());
- m_MembersCacheNeedsUpdate = true;
+ boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+ m_Members.insert(service);
}
-/**
- * @threadsafety Always.
- */
-void ServiceGroup::RefreshMembersCache(void)
+void ServiceGroup::RemoveMember(const Service::Ptr& service)
{
- {
- boost::mutex::scoped_lock lock(m_Mutex);
+ boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+ m_Members.erase(service);
+}
- if (!m_MembersCacheNeedsUpdate)
- return;
+bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
- m_MembersCacheNeedsUpdate = false;
- }
+ if (add && rstack > 20) {
+ Log(LogWarning, "ServiceGroup")
+ << "Too many nested groups for group '" << GetName() << "': Service '"
+ << service->GetName() << "' membership assignment failed.";
- Logger::Write(LogInformation, "icinga", "Updating ServiceGroup members cache.");
+ return false;
+ }
- map<String, vector<Service::WeakPtr> > newMembersCache;
+ Array::Ptr groups = GetGroups();
- BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
- const Service::Ptr& service = static_pointer_cast<Service>(object);
+ if (groups && groups->GetLength() > 0) {
+ ObjectLock olock(groups);
- Dictionary::Ptr dict;
- dict = service->GetGroups();
+ BOOST_FOREACH(const String& name, groups) {
+ ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
- if (dict) {
- ObjectLock mlock(dict);
- Value servicegroup;
- BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
- newMembersCache[servicegroup].push_back(service);
- }
+ if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
+ return false;
}
}
- boost::mutex::scoped_lock lock(m_Mutex);
- m_MembersCache.swap(newMembersCache);
+ if (add)
+ AddMember(service);
+ else
+ RemoveMember(service);
+
+ return true;
}