]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/servicegroup.cpp
Move the VMFrame class to libbase
[icinga2] / lib / icinga / servicegroup.cpp
index 3c4b091609ae2f7c513bcf9240b928557c17ff66..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;
 
-map<String, vector<Service::WeakPtr> > ServiceGroup::m_MembersCache;
-bool ServiceGroup::m_MembersCacheValid;
+REGISTER_TYPE(ServiceGroup);
 
-REGISTER_TYPE(ServiceGroup, NULL);
+INITIALIZE_ONCE(&ServiceGroup::RegisterObjectRuleHandler);
 
-String ServiceGroup::GetDisplayName(void) const
+void ServiceGroup::RegisterObjectRuleHandler(void)
 {
-       String value = Get("alias");
-
-       if (!value.IsEmpty())
-               return value;
-       else
-               return GetName();
+       ObjectRule::RegisterType("ServiceGroup");
 }
 
-String ServiceGroup::GetNotesUrl(void) const
+bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigItem::Ptr& group)
 {
-       return Get("notes_url");
-}
+       String group_name = group->GetName();
 
-String ServiceGroup::GetActionUrl(void) const
-{
-       return Get("action_url");
-}
+       CONTEXT("Evaluating rule for group '" + group_name + "'");
 
-bool ServiceGroup::Exists(const String& name)
-{
-       return (DynamicObject::GetObject("ServiceGroup", name));
-}
+       Host::Ptr host = service->GetHost();
 
-ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
-{
-       DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
+       ScriptFrame frame;
+       if (group->GetScope())
+               group->GetScope()->CopyTo(frame.Locals);
+       frame.Locals->Set("host", host);
+       frame.Locals->Set("service", service);
 
-       if (!configObject)
-               BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
+       if (!group->GetFilter()->Evaluate(frame))
+               return false;
 
-       return dynamic_pointer_cast<ServiceGroup>(configObject);
-}
+       Log(LogDebug, "ServiceGroup")
+           << "Assigning membership for group '" << group_name << "' to service '" << service->GetName() << "'";
 
-set<Service::Ptr> ServiceGroup::GetMembers(void) const
-{
-       set<Service::Ptr> services;
+       Array::Ptr groups = service->GetGroups();
+       groups->Add(group_name);
 
-       ValidateMembersCache();
+       return true;
+}
 
-       BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[GetName()]) {
-               Service::Ptr service = svc.lock();
+void ServiceGroup::EvaluateObjectRules(const Service::Ptr& service)
+{
+       CONTEXT("Evaluating group membership for service '" + service->GetName() + "'");
 
-               if (!service)
+       BOOST_FOREACH(const ConfigItem::Ptr& group, ConfigItem::GetItems("ServiceGroup"))
+       {
+               if (!group->GetFilter())
                        continue;
 
-               services.insert(service);
+               EvaluateObjectRule(service, group);
        }
+}
 
-       return services;
+std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
+{
+       boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+       return m_Members;
 }
 
-void ServiceGroup::InvalidateMembersCache(void)
+void ServiceGroup::AddMember(const Service::Ptr& service)
 {
-       m_MembersCacheValid = false;
-       m_MembersCache.clear();
+       service->AddGroup(GetName());
+
+       boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+       m_Members.insert(service);
 }
 
-void ServiceGroup::ValidateMembersCache(void)
+void ServiceGroup::RemoveMember(const Service::Ptr& service)
 {
-       if (m_MembersCacheValid)
-               return;
+       boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
+       m_Members.erase(service);
+}
 
-       m_MembersCache.clear();
+bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
 
-       DynamicObject::Ptr object;
-       BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
-               const Service::Ptr& service = static_pointer_cast<Service>(object);
+       if (add && rstack > 20) {
+               Log(LogWarning, "ServiceGroup")
+                   << "Too many nested groups for group '" << GetName() << "': Service '"
+                   << service->GetName() << "' membership assignment failed.";
 
-               Dictionary::Ptr dict;
-               dict = service->GetGroups();
+               return false;
+       }
 
-               if (dict) {
-                       Value servicegroup;
-                       BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
-                               if (!ServiceGroup::Exists(servicegroup))
-                                       Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
+       Array::Ptr groups = GetGroups();
 
-                               m_MembersCache[servicegroup].push_back(service);
-                       }
+       if (groups && groups->GetLength() > 0) {
+               ObjectLock olock(groups);
+
+               BOOST_FOREACH(const String& name, groups) {
+                       ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
+
+                       if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
+                               return false;
                }
        }
 
-       m_MembersCacheValid = true;
-}
+       if (add)
+               AddMember(service);
+       else
+               RemoveMember(service);
 
+       return true;
+}