From: Gunnar Beutner Date: Sun, 13 Apr 2014 10:41:34 +0000 (+0200) Subject: Fix auto-discovery info files. X-Git-Tag: v0.0.10~103 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e92ef19340d9944042f3f39386d26e381057d652;p=icinga2 Fix auto-discovery info files. Refs #6002 --- diff --git a/components/agent/agentlistener.cpp b/components/agent/agentlistener.cpp index ed216cc44..cf05ef42a 100644 --- a/components/agent/agentlistener.cpp +++ b/components/agent/agentlistener.cpp @@ -206,17 +206,39 @@ void AgentListener::MessageHandler(const TlsStream::Ptr& sender, const String& i } if (method == "push_crs") { - Host::Ptr host = Host::GetByName(identity); + Dictionary::Ptr params = message->Get("params"); - if (!host) { - Log(LogWarning, "agent", "Ignoring check results for host '" + identity + "'."); + if (!params) return; + + Dictionary::Ptr inventoryDescr = make_shared(); + inventoryDescr->Set("identity", identity); + inventoryDescr->Set("crs", params); + + String inventoryFile = GetInventoryDir() + SHA256(identity); + String inventoryTempFile = inventoryFile + ".tmp"; + + std::ofstream fp(inventoryTempFile.CStr(), std::ofstream::out | std::ostream::trunc); + fp << JsonSerialize(inventoryDescr); + fp.close(); + +#ifdef _WIN32 + _unlink(inventoryFile.CStr()); +#endif /* _WIN32 */ + + if (rename(inventoryTempFile.CStr(), inventoryFile.CStr()) < 0) { + BOOST_THROW_EXCEPTION(posix_error() + << boost::errinfo_api_function("rename") + << boost::errinfo_errno(errno) + << boost::errinfo_file_name(inventoryTempFile)); } - Dictionary::Ptr params = message->Get("params"); + Host::Ptr host = Host::GetByName(identity); - if (!params) + if (!host) { + Log(LogWarning, "agent", "Ignoring check results for host '" + identity + "'."); return; + } Value hostcr = Deserialize(params->Get("host"), true); @@ -252,28 +274,6 @@ void AgentListener::MessageHandler(const TlsStream::Ptr& sender, const String& i CheckResult::Ptr cr = servicecr; service->ProcessCheckResult(cr); } - - Dictionary::Ptr inventoryDescr = make_shared(); - inventoryDescr->Set("identity", identity); - inventoryDescr->Set("crs", params); - - String inventoryFile = GetInventoryDir() + SHA256(identity); - String inventoryTempFile = inventoryFile + ".tmp"; - - std::ofstream fp(inventoryTempFile.CStr(), std::ofstream::out | std::ostream::trunc); - fp << JsonSerialize(inventoryDescr); - fp.close(); - -#ifdef _WIN32 - _unlink(inventoryFile.CStr()); -#endif /* _WIN32 */ - - if (rename(inventoryTempFile.CStr(), inventoryFile.CStr()) < 0) { - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("rename") - << boost::errinfo_errno(errno) - << boost::errinfo_file_name(inventoryTempFile)); - } } }