"Fossies" - the Fresh Open Source Software Archive

Member "icinga2-2.10.5/lib/remote/zone.cpp" (23 May 2019, 4553 Bytes) of package /linux/misc/icinga2-2.10.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "zone.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.10.0_vs_2.10.1.

    1 /******************************************************************************
    2  * Icinga 2                                                                   *
    3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
    4  *                                                                            *
    5  * This program is free software; you can redistribute it and/or              *
    6  * modify it under the terms of the GNU General Public License                *
    7  * as published by the Free Software Foundation; either version 2             *
    8  * of the License, or (at your option) any later version.                     *
    9  *                                                                            *
   10  * This program is distributed in the hope that it will be useful,            *
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
   13  * GNU General Public License for more details.                               *
   14  *                                                                            *
   15  * You should have received a copy of the GNU General Public License          *
   16  * along with this program; if not, write to the Free Software Foundation     *
   17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
   18  ******************************************************************************/
   19 
   20 #include "remote/zone.hpp"
   21 #include "remote/zone-ti.cpp"
   22 #include "remote/jsonrpcconnection.hpp"
   23 #include "base/array.hpp"
   24 #include "base/objectlock.hpp"
   25 #include "base/logger.hpp"
   26 
   27 using namespace icinga;
   28 
   29 REGISTER_TYPE(Zone);
   30 
   31 void Zone::OnAllConfigLoaded()
   32 {
   33     ObjectImpl<Zone>::OnAllConfigLoaded();
   34 
   35     m_Parent = Zone::GetByName(GetParentRaw());
   36 
   37     if (m_Parent && m_Parent->IsGlobal())
   38         BOOST_THROW_EXCEPTION(ScriptError("Zone '" + GetName() + "' can not have a global zone as parent.", GetDebugInfo()));
   39 
   40     Zone::Ptr zone = m_Parent;
   41     int levels = 0;
   42 
   43     Array::Ptr endpoints = GetEndpointsRaw();
   44 
   45     if (endpoints) {
   46         ObjectLock olock(endpoints);
   47         for (const String& endpoint : endpoints) {
   48             Endpoint::Ptr ep = Endpoint::GetByName(endpoint);
   49 
   50             if (ep)
   51                 ep->SetCachedZone(this);
   52         }
   53     }
   54 
   55     while (zone) {
   56         m_AllParents.push_back(zone);
   57 
   58         zone = Zone::GetByName(zone->GetParentRaw());
   59         levels++;
   60 
   61         if (levels > 32)
   62             BOOST_THROW_EXCEPTION(ScriptError("Infinite recursion detected while resolving zone graph. Check your zone hierarchy.", GetDebugInfo()));
   63     }
   64 }
   65 
   66 Zone::Ptr Zone::GetParent() const
   67 {
   68     return m_Parent;
   69 }
   70 
   71 std::set<Endpoint::Ptr> Zone::GetEndpoints() const
   72 {
   73     std::set<Endpoint::Ptr> result;
   74 
   75     Array::Ptr endpoints = GetEndpointsRaw();
   76 
   77     if (endpoints) {
   78         ObjectLock olock(endpoints);
   79 
   80         for (const String& name : endpoints) {
   81             Endpoint::Ptr endpoint = Endpoint::GetByName(name);
   82 
   83             if (!endpoint)
   84                 continue;
   85 
   86             result.insert(endpoint);
   87         }
   88     }
   89 
   90     return result;
   91 }
   92 
   93 std::vector<Zone::Ptr> Zone::GetAllParentsRaw() const
   94 {
   95     return m_AllParents;
   96 }
   97 
   98 Array::Ptr Zone::GetAllParents() const
   99 {
  100     auto result (new Array);
  101 
  102     for (auto& parent : m_AllParents)
  103         result->Add(parent->GetName());
  104 
  105     return result;
  106 }
  107 
  108 bool Zone::CanAccessObject(const ConfigObject::Ptr& object)
  109 {
  110     Zone::Ptr object_zone;
  111 
  112     if (object->GetReflectionType() == Zone::TypeInstance)
  113         object_zone = static_pointer_cast<Zone>(object);
  114     else
  115         object_zone = static_pointer_cast<Zone>(object->GetZone());
  116 
  117     if (!object_zone)
  118         object_zone = Zone::GetLocalZone();
  119 
  120     if (object_zone->GetGlobal())
  121         return true;
  122 
  123     return object_zone->IsChildOf(this);
  124 }
  125 
  126 bool Zone::IsChildOf(const Zone::Ptr& zone)
  127 {
  128     Zone::Ptr azone = this;
  129 
  130     while (azone) {
  131         if (azone == zone)
  132             return true;
  133 
  134         azone = azone->GetParent();
  135     }
  136 
  137     return false;
  138 }
  139 
  140 bool Zone::IsGlobal() const
  141 {
  142     return GetGlobal();
  143 }
  144 
  145 bool Zone::IsSingleInstance() const
  146 {
  147     Array::Ptr endpoints = GetEndpointsRaw();
  148     return !endpoints || endpoints->GetLength() < 2;
  149 }
  150 
  151 Zone::Ptr Zone::GetLocalZone()
  152 {
  153     Endpoint::Ptr local = Endpoint::GetLocalEndpoint();
  154 
  155     if (!local)
  156         return nullptr;
  157 
  158     return local->GetZone();
  159 }
  160 
  161 void Zone::ValidateEndpointsRaw(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils)
  162 {
  163     ObjectImpl<Zone>::ValidateEndpointsRaw(lvalue, utils);
  164 
  165     if (lvalue() && lvalue()->GetLength() > 2) {
  166         Log(LogWarning, "Zone")
  167             << "The Zone object '" << GetName() << "' has more than two endpoints."
  168             << " Due to a known issue this type of configuration is strongly"
  169             << " discouraged and may cause Icinga to use excessive amounts of CPU time.";
  170     }
  171 }