From 8d27f66b83a35120d0ffa3c46b0b661f7c1fffac Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 9 Jul 2012 13:27:02 +0200 Subject: [PATCH] Moved host reachability/state code into the cib library. --- cib/host.cpp | 38 +++++++++++++++++++++++++ cib/host.h | 3 ++ cib/service.cpp | 8 ++++-- components/compat/compatcomponent.cpp | 40 ++++++--------------------- 4 files changed, 55 insertions(+), 34 deletions(-) diff --git a/cib/host.cpp b/cib/host.cpp index 044bc3512..a156113a4 100644 --- a/cib/host.cpp +++ b/cib/host.cpp @@ -59,3 +59,41 @@ set Host::GetParents(void) const return parents; } + +bool Host::IsReachable(void) const +{ + Dictionary::Ptr dependencies; + if (GetConfigObject()->GetProperty("dependencies", &dependencies)) { + dependencies = Service::ResolveDependencies(*this, dependencies); + + Dictionary::Iterator it; + for (it = dependencies->Begin(); it != dependencies->End(); it++) { + Service service = Service::GetByName(it->second); + + if (!service.IsReachable() || + (service.GetState() != StateOK && service.GetState() != StateWarning)) { + return false; + } + } + } + + return true; +} + +bool Host::IsUp(void) const +{ + Dictionary::Ptr hostchecks; + if (GetConfigObject()->GetProperty("hostchecks", &hostchecks)) { + hostchecks = Service::ResolveDependencies(*this, hostchecks); + + Dictionary::Iterator it; + for (it = hostchecks->Begin(); it != hostchecks->End(); it++) { + Service service = Service::GetByName(it->second); + if (service.GetState() != StateOK && service.GetState() != StateWarning) { + return false; + } + } + } + + return true; +} diff --git a/cib/host.h b/cib/host.h index d931585b9..719bac86e 100644 --- a/cib/host.h +++ b/cib/host.h @@ -17,6 +17,9 @@ public: string GetAlias(void) const; Dictionary::Ptr GetGroups(void) const; set GetParents(void) const; + + bool IsReachable(void) const; + bool IsUp(void) const; }; } diff --git a/cib/service.cpp b/cib/service.cpp index 41be7597e..595256fd2 100644 --- a/cib/service.cpp +++ b/cib/service.cpp @@ -376,10 +376,14 @@ Dictionary::Ptr Service::ResolveDependencies(Host host, const Dictionary::Ptr& d Dictionary::Iterator it; for (it = dependencies->Begin(); it != dependencies->End(); it++) { + string name; + if (services && services->Contains(it->first)) - result->AddUnnamedProperty(host.GetName() + "-" + it->first); + name = host.GetName() + "-" + it->first; else - result->AddUnnamedProperty(it->first); + name = it->first; + + result->SetProperty(name, name); } return result; diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index aeab1db18..6585446bc 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -61,38 +61,13 @@ void CompatComponent::Stop(void) void CompatComponent::DumpHostStatus(ofstream& fp, Host host) { - int state = 0; /* up */ - - Dictionary::Ptr dependencies; - if (host.GetConfigObject()->GetProperty("dependencies", &dependencies)) { - dependencies = Service::ResolveDependencies(host, dependencies); - - Dictionary::Iterator it; - for (it = dependencies->Begin(); it != dependencies->End(); it++) { - Service service = Service::GetByName(it->second); - - if (!service.IsReachable() || - (service.GetState() != StateOK && service.GetState() != StateWarning)) { - std::cerr << service.GetName() << " is unreachable." << std::endl; - state = 2; /* unreachable */ - break; - } - } - } - - Dictionary::Ptr hostchecks; - if (state == 0 && host.GetConfigObject()->GetProperty("hostchecks", &hostchecks)) { - hostchecks = Service::ResolveDependencies(host, hostchecks); - - Dictionary::Iterator it; - for (it = hostchecks->Begin(); it != hostchecks->End(); it++) { - Service service = Service::GetByName(it->second); - if (service.GetState() != StateOK && service.GetState() != StateWarning) { - state = 1; /* down */ - break; - } - } - } + int state; + if (!host.IsReachable()) + state = 2; /* unreachable */ + else if (!host.IsUp()) + state = 1; /* down */ + else + state = 0; /* up */ fp << "hoststatus {" << "\n" << "\t" << "host_name=" << host.GetName() << "\n" @@ -108,6 +83,7 @@ void CompatComponent::DumpHostStatus(ofstream& fp, Host host) << "\t" << "max_attempts=1" << "\n" << "\t" << "active_checks_enabled=1" << "\n" << "\t" << "passive_checks_enabled=1" << "\n" + << "\t" << "last_update=" << time(NULL) << "\n" << "\t" << "}" << "\n" << "\n"; } -- 2.40.0