2014-04-12 04:21:09 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2014-04-12 04:21:09 +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-03 20:02:22 +02:00
|
|
|
#ifndef APILISTENER_H
|
|
|
|
#define APILISTENER_H
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/apilistener.thpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/jsonrpcconnection.hpp"
|
2015-08-29 01:16:16 +02:00
|
|
|
#include "remote/httpserverconnection.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "remote/messageorigin.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configobject.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include "base/workqueue.hpp"
|
|
|
|
#include "base/tcpsocket.hpp"
|
|
|
|
#include "base/tlsstream.hpp"
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <set>
|
2014-04-12 04:21:09 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
class JsonRpcConnection;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
/**
|
|
|
|
* @ingroup remote
|
|
|
|
*/
|
|
|
|
struct ConfigDirInformation
|
|
|
|
{
|
|
|
|
Dictionary::Ptr UpdateV1;
|
|
|
|
Dictionary::Ptr UpdateV2;
|
|
|
|
};
|
|
|
|
|
2014-04-12 04:21:09 +02:00
|
|
|
/**
|
2014-05-03 20:02:22 +02:00
|
|
|
* @ingroup remote
|
|
|
|
*/
|
|
|
|
class I2_REMOTE_API ApiListener : public ObjectImpl<ApiListener>
|
2014-04-12 04:21:09 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-11-03 00:44:04 +01:00
|
|
|
DECLARE_OBJECT(ApiListener);
|
|
|
|
DECLARE_OBJECTNAME(ApiListener);
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
static boost::signals2::signal<void(bool)> OnMasterChanged;
|
|
|
|
|
2015-03-02 09:56:09 +01:00
|
|
|
ApiListener(void);
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
static ApiListener::Ptr GetInstance(void);
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
boost::shared_ptr<SSL_CTX> GetSSLContext(void) const;
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
Endpoint::Ptr GetMaster(void) const;
|
|
|
|
bool IsMaster(void) const;
|
|
|
|
|
2015-11-24 15:25:55 +01:00
|
|
|
Endpoint::Ptr GetLocalEndpoint(void) const;
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
static String GetApiDir(void);
|
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
void SyncSendMessage(const Endpoint::Ptr& endpoint, const Dictionary::Ptr& message);
|
2015-08-15 20:28:05 +02:00
|
|
|
void RelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2015-02-13 11:28:43 +01:00
|
|
|
static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
|
2014-05-03 20:02:22 +02:00
|
|
|
std::pair<Dictionary::Ptr, Dictionary::Ptr> GetStatus(void);
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
void AddAnonymousClient(const JsonRpcConnection::Ptr& aclient);
|
|
|
|
void RemoveAnonymousClient(const JsonRpcConnection::Ptr& aclient);
|
|
|
|
std::set<JsonRpcConnection::Ptr> GetAnonymousClients(void) const;
|
|
|
|
|
2015-08-29 01:16:16 +02:00
|
|
|
void AddHttpClient(const HttpServerConnection::Ptr& aclient);
|
|
|
|
void RemoveHttpClient(const HttpServerConnection::Ptr& aclient);
|
|
|
|
std::set<HttpServerConnection::Ptr> GetHttpClients(void) const;
|
2014-05-08 15:12:56 +02:00
|
|
|
|
2015-09-25 14:23:42 +02:00
|
|
|
static double CalculateZoneLag(const Endpoint::Ptr& endpoint);
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
/* filesync */
|
2015-08-04 14:47:44 +02:00
|
|
|
static Value ConfigUpdateHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
|
2015-08-20 16:43:03 +02:00
|
|
|
|
|
|
|
/* configsync */
|
|
|
|
static void ConfigUpdateObjectHandler(const ConfigObject::Ptr& object, const Value& cookie);
|
|
|
|
static Value ConfigUpdateObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
|
|
|
|
static Value ConfigDeleteObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
static Value HelloAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
|
2016-05-11 12:50:08 +02:00
|
|
|
|
2016-05-11 13:04:39 +02:00
|
|
|
static void UpdateObjectAuthority(void);
|
2016-05-11 12:50:08 +02:00
|
|
|
|
2016-08-04 10:12:55 +02:00
|
|
|
static bool IsHACluster(void);
|
2017-05-12 10:48:11 +02:00
|
|
|
static String GetFromZoneName(const Zone::Ptr& fromZone);
|
2016-08-04 10:12:55 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
protected:
|
2015-08-18 07:46:04 +02:00
|
|
|
virtual void OnConfigLoaded(void) override;
|
|
|
|
virtual void OnAllConfigLoaded(void) override;
|
2015-08-20 17:18:48 +02:00
|
|
|
virtual void Start(bool runtimeCreated) override;
|
2016-10-11 10:53:51 +02:00
|
|
|
virtual void Stop(bool runtimeDeleted) override;
|
2014-04-15 13:11:14 +02:00
|
|
|
|
2016-08-01 05:32:47 +02:00
|
|
|
virtual void ValidateTlsProtocolmin(const String& value, const ValidationUtils& utils) override;
|
|
|
|
|
2014-04-12 04:21:09 +02:00
|
|
|
private:
|
2014-11-08 21:17:16 +01:00
|
|
|
boost::shared_ptr<SSL_CTX> m_SSLContext;
|
2014-04-12 04:21:09 +02:00
|
|
|
std::set<TcpSocket::Ptr> m_Servers;
|
2015-06-22 11:11:21 +02:00
|
|
|
std::set<JsonRpcConnection::Ptr> m_AnonymousClients;
|
2015-08-29 01:16:16 +02:00
|
|
|
std::set<HttpServerConnection::Ptr> m_HttpClients;
|
2014-04-12 04:21:09 +02:00
|
|
|
Timer::Ptr m_Timer;
|
2016-07-21 12:27:18 +02:00
|
|
|
Timer::Ptr m_ReconnectTimer;
|
2016-08-17 09:19:05 +02:00
|
|
|
Timer::Ptr m_AuthorityTimer;
|
2015-11-24 15:25:55 +01:00
|
|
|
Endpoint::Ptr m_LocalEndpoint;
|
|
|
|
|
|
|
|
static ApiListener::Ptr m_Instance;
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
void ApiTimerHandler(void);
|
2016-07-21 12:27:18 +02:00
|
|
|
void ApiReconnectTimerHandler(void);
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2014-08-25 08:27:19 +02:00
|
|
|
bool AddListener(const String& node, const String& service);
|
2014-06-23 10:00:02 +02:00
|
|
|
void AddConnection(const Endpoint::Ptr& endpoint);
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2015-03-05 14:15:42 +01:00
|
|
|
void NewClientHandler(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
|
2015-08-11 12:08:54 +02:00
|
|
|
void NewClientHandlerInternal(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
|
2014-04-12 04:21:09 +02:00
|
|
|
void ListenerThreadProc(const Socket::Ptr& server);
|
2014-04-13 09:22:27 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
WorkQueue m_RelayQueue;
|
2015-11-25 13:11:41 +01:00
|
|
|
WorkQueue m_SyncQueue;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
|
|
|
boost::mutex m_LogLock;
|
|
|
|
Stream::Ptr m_LogFile;
|
|
|
|
size_t m_LogMessageCount;
|
|
|
|
|
2016-01-27 13:39:31 +01:00
|
|
|
bool RelayMessageOne(const Zone::Ptr& zone, const MessageOrigin::Ptr& origin, const Dictionary::Ptr& message, const Endpoint::Ptr& currentMaster);
|
2015-08-15 20:28:05 +02:00
|
|
|
void SyncRelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
|
|
|
|
void PersistMessage(const Dictionary::Ptr& message, const ConfigObject::Ptr& secobj);
|
2014-04-13 09:22:27 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
void OpenLogFile(void);
|
|
|
|
void RotateLogFile(void);
|
|
|
|
void CloseLogFile(void);
|
|
|
|
static void LogGlobHandler(std::vector<int>& files, const String& file);
|
2015-06-22 11:11:21 +02:00
|
|
|
void ReplayLog(const JsonRpcConnection::Ptr& client);
|
2014-05-13 13:18:27 +02:00
|
|
|
|
2015-09-10 17:48:06 +02:00
|
|
|
/* filesync */
|
2016-01-26 10:46:27 +01:00
|
|
|
static ConfigDirInformation LoadConfigDir(const String& dir);
|
|
|
|
static Dictionary::Ptr MergeConfigUpdate(const ConfigDirInformation& config);
|
|
|
|
static bool UpdateConfigDir(const ConfigDirInformation& oldConfig, const ConfigDirInformation& newConfig, const String& configDir, bool authoritative);
|
2014-06-10 11:40:43 +02:00
|
|
|
|
2014-05-13 13:18:27 +02:00
|
|
|
void SyncZoneDirs(void) const;
|
|
|
|
void SyncZoneDir(const Zone::Ptr& zone) const;
|
2014-06-10 11:40:43 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
static void ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file);
|
2015-06-22 11:11:21 +02:00
|
|
|
void SendConfigUpdate(const JsonRpcConnection::Ptr& aclient);
|
2015-09-10 17:48:06 +02:00
|
|
|
|
|
|
|
/* configsync */
|
2015-08-20 16:43:03 +02:00
|
|
|
void UpdateConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
|
|
|
|
const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
|
2015-09-10 17:48:06 +02:00
|
|
|
void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
|
|
|
|
const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
|
2015-09-15 16:09:56 +02:00
|
|
|
void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);
|
2015-11-25 13:11:41 +01:00
|
|
|
|
2016-02-04 11:30:27 +01:00
|
|
|
void SyncClient(const JsonRpcConnection::Ptr& aclient, const Endpoint::Ptr& endpoint, bool needSync);
|
2014-04-12 04:21:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
#endif /* APILISTENER_H */
|