2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-05-09 11:32:24 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|