]> granicus.if.org Git - icinga2/commitdiff
Moved host reachability/state code into the cib library.
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 9 Jul 2012 11:27:02 +0000 (13:27 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 9 Jul 2012 11:27:59 +0000 (13:27 +0200)
cib/host.cpp
cib/host.h
cib/service.cpp
components/compat/compatcomponent.cpp

index 044bc35124fb47ab44b701e6d61829e733dd5279..a156113a49205a25ee71905f6a4c2668a7ae9047 100644 (file)
@@ -59,3 +59,41 @@ set<string> 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;
+}
index d931585b94cde80c07277cf840595474e6add072..719bac86e2cc0aaf36428a0f5b80f5eec8cfda49 100644 (file)
@@ -17,6 +17,9 @@ public:
        string GetAlias(void) const;
        Dictionary::Ptr GetGroups(void) const;
        set<string> GetParents(void) const;
+
+       bool IsReachable(void) const;
+       bool IsUp(void) const;
 };
 
 }
index 41be7597e47a42327a2e1eb29cd862bb2ec2cd36..595256fd21ffd4f4f5ef70bc3a85b49fd9467b01 100644 (file)
@@ -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;
index aeab1db1876232cfb0f8575c813d382d42d5b13a..6585446bc45bc2b07470870f15428d63d232a195 100644 (file)
@@ -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";
 }