]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/servicegroup.cpp
Move the VMFrame class to libbase
[icinga2] / lib / icinga / servicegroup.cpp
index b60e4e4210e2ab768274ee979d47c44545a55b42..e205c7dd5dd1e76e3c03b7a4e7272532a65875c3 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * 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_MembersCacheValid = true;
-
 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
-{
-       ObjectLock olock(this);
+       Host::Ptr host = service->GetHost();
 
-       if (!m_DisplayName.Get().IsEmpty())
-               return m_DisplayName;
-       else
-               return GetName();
-}
-
-/**
- * @threadsafety Always.
- */
-String ServiceGroup::GetNotesUrl(void) const
-{
-       ObjectLock olock(this);
-
-       return m_NotesUrl;
-}
-
-/**
- * @threadsafety Always.
- */
-String ServiceGroup::GetActionUrl(void) const
-{
-       ObjectLock olock(this);
+       ScriptFrame frame;
+       if (group->GetScope())
+               group->GetScope()->CopyTo(frame.Locals);
+       frame.Locals->Set("host", host);
+       frame.Locals->Set("service", service);
 
-       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 (!service)
-                               continue;
+               if (!group->GetFilter())
+                       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);
+       boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+       return m_Members;
+}
 
-       if (m_MembersCacheValid)
-               Utility::QueueAsyncCallback(boost::bind(&ServiceGroup::RefreshMembersCache));
+void ServiceGroup::AddMember(const Service::Ptr& service)
+{
+       service->AddGroup(GetName());
 
-       m_MembersCacheValid = false;
+       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_MembersCacheValid)
-                       return;
+bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
 
-               m_MembersCacheValid = true;
+       if (add && rstack > 20) {
+               Log(LogWarning, "ServiceGroup")
+                   << "Too many nested groups for group '" << GetName() << "': Service '"
+                   << service->GetName() << "' membership assignment failed.";
+
+               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);
-               ObjectLock olock(service);
+       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;
 }