]> granicus.if.org Git - icinga2/commitdiff
Fix check results for localhost in HA cluster
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 22 Aug 2014 10:59:24 +0000 (12:59 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 22 Aug 2014 11:01:06 +0000 (13:01 +0200)
fixes #6964

lib/icinga/apievents.cpp
lib/icinga/apievents.hpp

index 723f5ee80f5c00192f9ce503c88246f2f2458212..4d82e97bcfadf5700f003e268882bf619e00970d 100644 (file)
@@ -95,7 +95,7 @@ void ApiEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckR
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("cr", Serialize(cr));
@@ -114,7 +114,7 @@ Value ApiEvents::CheckResultAPIHandler(const MessageOrigin& origin, const Dictio
 
        CheckResult::Ptr cr = Deserialize(crv, true);
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -149,7 +149,7 @@ void ApiEvents::NextCheckChangedHandler(const Checkable::Ptr& checkable, double
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("next_check", nextCheck);
@@ -167,7 +167,7 @@ Value ApiEvents::NextCheckChangedAPIHandler(const MessageOrigin& origin, const D
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -239,7 +239,7 @@ void ApiEvents::ForceNextCheckChangedHandler(const Checkable::Ptr& checkable, bo
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("forced", forced);
@@ -257,7 +257,7 @@ Value ApiEvents::ForceNextCheckChangedAPIHandler(const MessageOrigin& origin, co
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -292,7 +292,7 @@ void ApiEvents::ForceNextNotificationChangedHandler(const Checkable::Ptr& checka
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("forced", forced);
@@ -310,7 +310,7 @@ Value ApiEvents::ForceNextNotificationChangedAPIHandler(const MessageOrigin& ori
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -345,7 +345,7 @@ void ApiEvents::EnableActiveChecksChangedHandler(const Checkable::Ptr& checkable
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("enabled", enabled);
@@ -363,7 +363,7 @@ Value ApiEvents::EnableActiveChecksChangedAPIHandler(const MessageOrigin& origin
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -398,7 +398,7 @@ void ApiEvents::EnablePassiveChecksChangedHandler(const Checkable::Ptr& checkabl
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("enabled", enabled);
@@ -416,7 +416,7 @@ Value ApiEvents::EnablePassiveChecksChangedAPIHandler(const MessageOrigin& origi
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -451,7 +451,7 @@ void ApiEvents::EnableNotificationsChangedHandler(const Checkable::Ptr& checkabl
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("enabled", enabled);
@@ -469,7 +469,7 @@ Value ApiEvents::EnableNotificationsChangedAPIHandler(const MessageOrigin& origi
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -504,7 +504,7 @@ void ApiEvents::EnableFlappingChangedHandler(const Checkable::Ptr& checkable, bo
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("enabled", enabled);
@@ -522,7 +522,7 @@ Value ApiEvents::EnableFlappingChangedAPIHandler(const MessageOrigin& origin, co
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -557,7 +557,7 @@ void ApiEvents::CommentAddedHandler(const Checkable::Ptr& checkable, const Comme
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("comment", Serialize(comment));
@@ -575,7 +575,7 @@ Value ApiEvents::CommentAddedAPIHandler(const MessageOrigin& origin, const Dicti
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -613,7 +613,7 @@ void ApiEvents::CommentRemovedHandler(const Checkable::Ptr& checkable, const Com
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("id", comment->GetId());
@@ -631,7 +631,7 @@ Value ApiEvents::CommentRemovedAPIHandler(const MessageOrigin& origin, const Dic
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -666,7 +666,7 @@ void ApiEvents::DowntimeAddedHandler(const Checkable::Ptr& checkable, const Down
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("downtime", Serialize(downtime));
@@ -684,7 +684,7 @@ Value ApiEvents::DowntimeAddedAPIHandler(const MessageOrigin& origin, const Dict
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -725,7 +725,7 @@ void ApiEvents::DowntimeRemovedHandler(const Checkable::Ptr& checkable, const Do
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("id", downtime->GetId());
@@ -743,7 +743,7 @@ Value ApiEvents::DowntimeRemovedAPIHandler(const MessageOrigin& origin, const Di
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -780,7 +780,7 @@ void ApiEvents::AcknowledgementSetHandler(const Checkable::Ptr& checkable,
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
        params->Set("author", author);
@@ -801,7 +801,7 @@ Value ApiEvents::AcknowledgementSetAPIHandler(const MessageOrigin& origin, const
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -838,7 +838,7 @@ void ApiEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, c
        tie(host, service) = GetHostService(checkable);
 
        Dictionary::Ptr params = make_shared<Dictionary>();
-       params->Set("host", GetVirtualHostName(host));
+       params->Set("host", host->GetName());
        if (service)
                params->Set("service", service->GetShortName());
 
@@ -855,7 +855,7 @@ Value ApiEvents::AcknowledgementClearedAPIHandler(const MessageOrigin& origin, c
        if (!params)
                return Empty;
 
-       Host::Ptr host = FindHostByVirtualName(params->Get("host"));
+       Host::Ptr host = FindHostByVirtualName(params->Get("host"), origin);
 
        if (!host)
                return Empty;
@@ -894,7 +894,7 @@ void ApiEvents::RepositoryTimerHandler(void)
                        services->Add(service->GetShortName());
                }
 
-               repository->Set(GetVirtualHostName(host), services);
+               repository->Set(host->GetName(), services);
        }
 
        Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
@@ -935,6 +935,18 @@ Value ApiEvents::UpdateRepositoryAPIHandler(const MessageOrigin& origin, const D
        if (!params)
                return Empty;
 
+       Value vrepository = params->Get("repository");
+       if (vrepository.IsEmpty() || !vrepository.IsObjectType<Dictionary>())
+               return Empty;
+
+       Dictionary::Ptr repository = vrepository;
+       Value hostInfo = repository->Get("localhost");
+
+       if (!hostInfo.IsEmpty() && origin.FromZone) {
+               repository->Remove("localhost");
+               repository->Set(origin.FromZone->GetName(), hostInfo);
+       }
+
        String repositoryFile = GetRepositoryDir() + SHA256(params->Get("endpoint"));
        String repositoryTempFile = repositoryFile + ".tmp";
 
@@ -968,30 +980,10 @@ Value ApiEvents::UpdateRepositoryAPIHandler(const MessageOrigin& origin, const D
        return Empty;
 }
 
-String ApiEvents::GetVirtualHostName(const Host::Ptr& host)
-{
-       String host_name = host->GetName();
-       if (host_name == "localhost") {
-               Endpoint::Ptr local = Endpoint::GetLocalEndpoint();
-
-               if (!local)
-                       return Empty;
-
-               host_name = local->GetName();
-       }
-
-       return host_name;
-}
-
-Host::Ptr ApiEvents::FindHostByVirtualName(const String& hostName)
+Host::Ptr ApiEvents::FindHostByVirtualName(const String& hostName, const MessageOrigin& origin)
 {
-       Endpoint::Ptr local = Endpoint::GetLocalEndpoint();
-
-       if (!local)
-               return Host::Ptr();
-
-       if (hostName == local->GetName())
-               return Host::GetByName("localhost");
+       if (origin.FromZone && hostName == "localhost")
+               return Host::GetByName(origin.FromZone->GetName());
        else
                return Host::GetByName(hostName);
 }
index 5ebe217e8ebd9c63d3e09b440c99731ef00e6438..53ef20098043479c8a7ba75544140d4e43f2431a 100644 (file)
@@ -83,8 +83,7 @@ public:
        static void RepositoryTimerHandler(void);
        static Value UpdateRepositoryAPIHandler(const MessageOrigin& origin, const Dictionary::Ptr& params);
 
-       static String GetVirtualHostName(const Host::Ptr& host);
-       static Host::Ptr FindHostByVirtualName(const String& hostName);
+       static Host::Ptr FindHostByVirtualName(const String& hostName, const MessageOrigin& origin);
 };
 
 }