[icinga-checkins] icinga.org: icinga2/master: Implement Host::GetServices().

git at icinga.org git at icinga.org
Thu Jan 24 15:12:40 CET 2013


Module: icinga2
Branch: master
Commit: b3e7dc32e9ac58b82c566bc3e258dea13140cf77
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=b3e7dc32e9ac58b82c566bc3e258dea13140cf77

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Thu Jan 24 15:10:17 2013 +0100

Implement Host::GetServices().

Fixes #3565

---

 lib/icinga/host.cpp    |   43 +++++++++++++++++++++++++++++++++++++++++++
 lib/icinga/host.h      |   10 ++++++++++
 lib/icinga/service.cpp |    4 ++++
 3 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp
index bc7b2b4..3790464 100644
--- a/lib/icinga/host.cpp
+++ b/lib/icinga/host.cpp
@@ -21,6 +21,9 @@
 
 using namespace icinga;
 
+map<String, vector<String> > 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<Service::Ptr> Host::GetServices(void) const
+{
+	set<Service::Ptr> 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<Service>(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 5b908b0..37459c6 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<shared_ptr<Service> > GetServices(void) const;
+	static void InvalidateServicesCache(void);
+
 protected:
 	void OnAttributeChanged(const String& name, const Value& oldValue);
 
 private:
 	static bool m_InitializerDone;
 
+	static map<String, vector<String> > 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 5fba267..1c9c453 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<void (void)>& callback)





More information about the icinga-checkins mailing list