diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index bc7b2b425..37904643a 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -21,6 +21,9 @@ using namespace icinga; +map > Host::m_ServicesCache; +bool Host::m_ServicesCacheValid = true; + static AttributeDescription hostAttributes[] = { { "alias", Attribute_Config }, { "hostgroups", Attribute_Config } @@ -286,3 +289,43 @@ void Host::OnAttributeChanged(const String& name, const Value& oldValue) HostGroup::InvalidateMembersCache(); } +set Host::GetServices(void) const +{ + set services; + + ValidateServicesCache(); + + BOOST_FOREACH(const String& svc, m_ServicesCache[GetName()]) { + if (!Service::Exists(svc)) + continue; + + Service::Ptr service = Service::GetByName(svc); + services.insert(service); + } + + return services; +} + +void Host::InvalidateServicesCache(void) +{ + m_ServicesCacheValid = false; + m_ServicesCache.clear(); +} + +void Host::ValidateServicesCache(void) +{ + if (m_ServicesCacheValid) + return; + + m_ServicesCache.clear(); + + DynamicObject::Ptr object; + BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) { + const Service::Ptr& service = static_pointer_cast(object); + + m_ServicesCache[service->GetHost()->GetName()].push_back(service->GetName()); + } + + m_ServicesCacheValid = true; +} + diff --git a/lib/icinga/host.h b/lib/icinga/host.h index 5b908b0c4..37459c669 100644 --- a/lib/icinga/host.h +++ b/lib/icinga/host.h @@ -23,6 +23,8 @@ namespace icinga { +class Service; + /** * An Icinga host. * @@ -48,14 +50,22 @@ public: bool IsReachable(void); bool IsUp(void); + set > GetServices(void) const; + static void InvalidateServicesCache(void); + protected: void OnAttributeChanged(const String& name, const Value& oldValue); private: static bool m_InitializerDone; + static map > m_ServicesCache; + static bool m_ServicesCacheValid; + static void ObjectCommittedHandler(const ConfigItem::Ptr& item); static void ObjectRemovedHandler(const ConfigItem::Ptr& item); + + static void ValidateServicesCache(void); }; } diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 5fba2678f..1c9c453aa 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -64,11 +64,13 @@ Service::Service(const Dictionary::Ptr& serializedObject) : DynamicObject(serializedObject) { ServiceGroup::InvalidateMembersCache(); + Host::InvalidateServicesCache(); } Service::~Service(void) { ServiceGroup::InvalidateMembersCache(); + Host::InvalidateServicesCache(); } String Service::GetAlias(void) const @@ -592,6 +594,8 @@ void Service::OnAttributeChanged(const String& name, const Value& oldValue) OnNextCheckChanged(GetSelf(), oldValue); else if (name == "servicegroups") ServiceGroup::InvalidateMembersCache(); + else if (name == "host_name") + Host::InvalidateServicesCache(); } void Service::BeginExecuteCheck(const function& callback)