2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2012-05-10 12:06:41 +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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/endpoint.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "remote/endpoint.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/jsonrpcconnection.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/zone.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"
|
|
|
|
#include "base/exception.hpp"
|
2014-08-22 15:39:34 +02:00
|
|
|
#include "base/convert.hpp"
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Endpoint);
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
boost::signals2::signal<void(const Endpoint::Ptr&, const JsonRpcConnection::Ptr&)> Endpoint::OnConnected;
|
|
|
|
boost::signals2::signal<void(const Endpoint::Ptr&, const JsonRpcConnection::Ptr&)> Endpoint::OnDisconnected;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2014-11-21 21:01:44 +01:00
|
|
|
void Endpoint::OnAllConfigLoaded(void)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::OnConfigLoaded();
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const Zone::Ptr& zone, ConfigType::GetObjectsByType<Zone>()) {
|
2014-05-03 20:02:22 +02:00
|
|
|
const std::set<Endpoint::Ptr> members = zone->GetEndpoints();
|
2014-04-25 14:33:45 +02:00
|
|
|
|
2014-06-06 13:30:06 +02:00
|
|
|
if (members.empty())
|
|
|
|
continue;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
if (members.find(this) != members.end()) {
|
2014-05-03 20:02:22 +02:00
|
|
|
if (m_Zone)
|
2015-09-22 17:58:12 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName()
|
|
|
|
+ "' is in more than one zone.", GetDebugInfo()));
|
2014-05-03 20:02:22 +02:00
|
|
|
|
|
|
|
m_Zone = zone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_Zone)
|
2015-09-22 17:58:12 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName() +
|
|
|
|
"' does not belong to a zone.", GetDebugInfo()));
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
bool was_master = ApiListener::GetInstance()->IsMaster();
|
2014-04-25 14:33:45 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ClientsLock);
|
|
|
|
m_Clients.insert(client);
|
|
|
|
}
|
2013-10-17 10:56:42 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
bool is_master = ApiListener::GetInstance()->IsMaster();
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
if (was_master != is_master)
|
|
|
|
ApiListener::OnMasterChanged(is_master);
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
OnConnected(this, client);
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
void Endpoint::RemoveClient(const JsonRpcConnection::Ptr& client)
|
2012-05-08 10:13:15 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
bool was_master = ApiListener::GetInstance()->IsMaster();
|
2013-09-02 15:12:20 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ClientsLock);
|
|
|
|
m_Clients.erase(client);
|
2014-08-22 15:39:34 +02:00
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogWarning, "ApiListener")
|
|
|
|
<< "Removing API client for endpoint '" << GetName() << "'. " << m_Clients.size() << " API clients left.";
|
2015-02-26 11:25:01 +01:00
|
|
|
|
|
|
|
SetConnecting(false);
|
2014-05-03 20:02:22 +02:00
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
bool is_master = ApiListener::GetInstance()->IsMaster();
|
2012-09-03 10:28:14 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
if (was_master != is_master)
|
|
|
|
ApiListener::OnMasterChanged(is_master);
|
2014-02-21 14:12:34 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
OnDisconnected(this, client);
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
std::set<JsonRpcConnection::Ptr> Endpoint::GetClients(void) const
|
2012-09-03 10:28:14 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ClientsLock);
|
|
|
|
return m_Clients;
|
|
|
|
}
|
2014-02-21 14:12:34 +01:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
Zone::Ptr Endpoint::GetZone(void) const
|
|
|
|
{
|
|
|
|
return m_Zone;
|
|
|
|
}
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2015-10-22 10:52:38 +02:00
|
|
|
bool Endpoint::GetConnected(void) const
|
2014-05-03 20:02:22 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_ClientsLock);
|
|
|
|
return !m_Clients.empty();
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
Endpoint::Ptr Endpoint::GetLocalEndpoint(void)
|
2013-09-12 10:03:48 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
2013-09-12 10:03:48 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
if (!listener)
|
|
|
|
return Endpoint::Ptr();
|
2013-09-12 10:03:48 +02:00
|
|
|
|
2015-11-24 15:25:55 +01:00
|
|
|
return listener->GetLocalEndpoint();
|
2013-09-12 10:03:48 +02:00
|
|
|
}
|