Implement Host::GetServices().

Fixes #3565
This commit is contained in:
Gunnar Beutner 2013-01-24 15:10:17 +01:00
parent a0e7f751cd
commit b3e7dc32e9
3 changed files with 57 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);
};
}

View File

@ -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)