2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-04-25 14:33:45 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/zone.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "remote/zone-ti.cpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/jsonrpcconnection.hpp"
|
2018-08-08 14:15:01 +02:00
|
|
|
#include "base/array.hpp"
|
2014-06-06 13:30:06 +02:00
|
|
|
#include "base/objectlock.hpp"
|
2016-07-26 08:10:47 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-04-25 14:33:45 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
REGISTER_TYPE(Zone);
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Zone::OnAllConfigLoaded()
|
2015-11-24 15:25:55 +01:00
|
|
|
{
|
2016-08-14 22:10:30 +02:00
|
|
|
ObjectImpl<Zone>::OnAllConfigLoaded();
|
|
|
|
|
2015-11-24 15:25:55 +01:00
|
|
|
m_Parent = Zone::GetByName(GetParentRaw());
|
2016-01-27 12:18:16 +01:00
|
|
|
|
2018-06-15 14:40:09 +02:00
|
|
|
if (m_Parent && m_Parent->IsGlobal())
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Zone '" + GetName() + "' can not have a global zone as parent.", GetDebugInfo()));
|
|
|
|
|
2016-01-27 12:18:16 +01:00
|
|
|
Zone::Ptr zone = m_Parent;
|
2016-03-23 09:01:10 +01:00
|
|
|
int levels = 0;
|
2016-01-27 12:18:16 +01:00
|
|
|
|
2016-08-16 13:53:45 +02:00
|
|
|
Array::Ptr endpoints = GetEndpointsRaw();
|
|
|
|
|
|
|
|
if (endpoints) {
|
2016-08-17 09:16:27 +02:00
|
|
|
ObjectLock olock(endpoints);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& endpoint : endpoints) {
|
2016-08-26 10:36:53 +02:00
|
|
|
Endpoint::Ptr ep = Endpoint::GetByName(endpoint);
|
|
|
|
|
|
|
|
if (ep)
|
|
|
|
ep->SetCachedZone(this);
|
2016-08-16 13:53:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 12:18:16 +01:00
|
|
|
while (zone) {
|
|
|
|
m_AllParents.push_back(zone);
|
|
|
|
|
|
|
|
zone = Zone::GetByName(zone->GetParentRaw());
|
2016-03-23 09:01:10 +01:00
|
|
|
levels++;
|
|
|
|
|
|
|
|
if (levels > 32)
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Infinite recursion detected while resolving zone graph. Check your zone hierarchy.", GetDebugInfo()));
|
2016-01-27 12:18:16 +01:00
|
|
|
}
|
2015-11-24 15:25:55 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Zone::Ptr Zone::GetParent() const
|
2014-04-25 14:33:45 +02:00
|
|
|
{
|
2015-11-24 15:25:55 +01:00
|
|
|
return m_Parent;
|
2014-04-25 14:33:45 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
std::set<Endpoint::Ptr> Zone::GetEndpoints() const
|
2014-04-25 14:33:45 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
std::set<Endpoint::Ptr> result;
|
2014-04-25 14:33:45 +02:00
|
|
|
|
2014-06-06 13:30:06 +02:00
|
|
|
Array::Ptr endpoints = GetEndpointsRaw();
|
|
|
|
|
|
|
|
if (endpoints) {
|
|
|
|
ObjectLock olock(endpoints);
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& name : endpoints) {
|
2014-06-06 13:30:06 +02:00
|
|
|
Endpoint::Ptr endpoint = Endpoint::GetByName(name);
|
|
|
|
|
|
|
|
if (!endpoint)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.insert(endpoint);
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 14:33:45 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
return result;
|
2014-04-25 14:33:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-08 13:59:43 +02:00
|
|
|
std::vector<Zone::Ptr> Zone::GetAllParentsRaw() const
|
2016-01-27 12:18:16 +01:00
|
|
|
{
|
|
|
|
return m_AllParents;
|
|
|
|
}
|
|
|
|
|
2018-08-08 14:15:01 +02:00
|
|
|
Array::Ptr Zone::GetAllParents() const
|
|
|
|
{
|
|
|
|
auto result (new Array);
|
|
|
|
|
|
|
|
for (auto& parent : m_AllParents)
|
|
|
|
result->Add(parent->GetName());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
bool Zone::CanAccessObject(const ConfigObject::Ptr& object)
|
2014-04-25 14:33:45 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
Zone::Ptr object_zone;
|
|
|
|
|
2016-01-27 08:43:20 +01:00
|
|
|
if (object->GetReflectionType() == Zone::TypeInstance)
|
2014-05-03 20:02:22 +02:00
|
|
|
object_zone = static_pointer_cast<Zone>(object);
|
2014-04-25 14:33:45 +02:00
|
|
|
else
|
2015-11-24 15:25:55 +01:00
|
|
|
object_zone = static_pointer_cast<Zone>(object->GetZone());
|
2014-05-03 20:02:22 +02:00
|
|
|
|
|
|
|
if (!object_zone)
|
2014-05-08 12:36:48 +02:00
|
|
|
object_zone = Zone::GetLocalZone();
|
2016-11-11 10:41:49 +01:00
|
|
|
|
|
|
|
if (object_zone->GetGlobal())
|
|
|
|
return true;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
return object_zone->IsChildOf(this);
|
2014-05-13 15:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Zone::IsChildOf(const Zone::Ptr& zone)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Zone::Ptr azone = this;
|
2014-05-13 15:57:02 +02:00
|
|
|
|
|
|
|
while (azone) {
|
|
|
|
if (azone == zone)
|
2014-05-03 20:02:22 +02:00
|
|
|
return true;
|
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
azone = azone->GetParent();
|
2014-05-03 20:02:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-04-25 14:33:45 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool Zone::IsGlobal() const
|
2014-06-12 14:31:07 +02:00
|
|
|
{
|
2014-11-08 22:57:09 +01:00
|
|
|
return GetGlobal();
|
2014-06-12 14:31:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool Zone::IsSingleInstance() const
|
2016-08-04 10:12:55 +02:00
|
|
|
{
|
|
|
|
Array::Ptr endpoints = GetEndpointsRaw();
|
|
|
|
return !endpoints || endpoints->GetLength() < 2;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Zone::Ptr Zone::GetLocalZone()
|
2014-04-25 14:33:45 +02:00
|
|
|
{
|
2014-05-22 23:01:15 +02:00
|
|
|
Endpoint::Ptr local = Endpoint::GetLocalEndpoint();
|
|
|
|
|
|
|
|
if (!local)
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2014-05-22 23:01:15 +02:00
|
|
|
|
|
|
|
return local->GetZone();
|
2014-04-25 14:33:45 +02:00
|
|
|
}
|
2015-09-22 17:58:12 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void Zone::ValidateEndpointsRaw(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils)
|
2016-07-26 08:10:47 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<Zone>::ValidateEndpointsRaw(lvalue, utils);
|
2016-07-26 08:10:47 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (lvalue() && lvalue()->GetLength() > 2) {
|
2016-07-26 08:10:47 +02:00
|
|
|
Log(LogWarning, "Zone")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "The Zone object '" << GetName() << "' has more than two endpoints."
|
|
|
|
<< " Due to a known issue this type of configuration is strongly"
|
|
|
|
<< " discouraged and may cause Icinga to use excessive amounts of CPU time.";
|
2016-07-26 08:10:47 +02:00
|
|
|
}
|
|
|
|
}
|