2014-04-25 14:33:45 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2014-04-25 14:33:45 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/zone.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "remote/zone.tcpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/jsonrpcconnection.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
|
|
|
|
|
|
|
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-01-04 04:25:35 +01:00
|
|
|
std::vector<Zone::Ptr> Zone::GetAllParents() const
|
2016-01-27 12:18:16 +01:00
|
|
|
{
|
|
|
|
return m_AllParents;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|