2014-05-09 11:32:24 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2014-05-09 11:32:24 +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"
|
|
|
|
#include "remote/apilistener.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
2014-05-09 11:32:24 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ApiListener::UpdateObjectAuthority()
|
2014-05-09 11:32:24 +02:00
|
|
|
{
|
2016-05-11 12:50:08 +02:00
|
|
|
Zone::Ptr my_zone = Zone::GetLocalZone();
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-05-11 12:50:08 +02:00
|
|
|
std::vector<Endpoint::Ptr> endpoints;
|
|
|
|
Endpoint::Ptr my_endpoint;
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-05-11 12:50:08 +02:00
|
|
|
if (my_zone) {
|
|
|
|
my_endpoint = Endpoint::GetLocalEndpoint();
|
2014-08-04 16:34:17 +02:00
|
|
|
|
2016-05-11 12:50:08 +02:00
|
|
|
int num_total = 0;
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Endpoint::Ptr& endpoint : my_zone->GetEndpoints()) {
|
2016-05-11 12:50:08 +02:00
|
|
|
num_total++;
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-05-11 12:50:08 +02:00
|
|
|
if (endpoint != my_endpoint && !endpoint->GetConnected())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
endpoints.push_back(endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
double mainTime = Application::GetMainTime();
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-05-11 12:50:08 +02:00
|
|
|
if (num_total > 1 && endpoints.size() <= 1 && (mainTime == 0 || Utility::GetTime() - mainTime < 60))
|
|
|
|
return;
|
|
|
|
|
2016-08-27 09:35:08 +02:00
|
|
|
std::sort(endpoints.begin(), endpoints.end(),
|
|
|
|
[](const ConfigObject::Ptr& a, const ConfigObject::Ptr& b) {
|
|
|
|
return a->GetName() < b->GetName();
|
|
|
|
}
|
|
|
|
);
|
2016-05-11 12:50:08 +02:00
|
|
|
}
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
|
2016-08-17 09:19:05 +02:00
|
|
|
if (!object->IsActive() || object->GetHAMode() != HARunOnce)
|
2016-05-11 12:50:08 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
bool authority;
|
|
|
|
|
|
|
|
if (!my_zone)
|
|
|
|
authority = true;
|
|
|
|
else
|
|
|
|
authority = endpoints[Utility::SDBM(object->GetName()) % endpoints.size()] == my_endpoint;
|
2014-05-09 11:32:24 +02:00
|
|
|
|
2016-05-11 12:50:08 +02:00
|
|
|
object->SetAuthority(authority);
|
2014-05-09 11:32:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|