1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
23 #include "remote/apilistener.thpp"
24 #include "remote/jsonrpcconnection.hpp"
25 #include "remote/httpserverconnection.hpp"
26 #include "remote/endpoint.hpp"
27 #include "remote/messageorigin.hpp"
28 #include "base/configobject.hpp"
29 #include "base/timer.hpp"
30 #include "base/workqueue.hpp"
31 #include "base/tcpsocket.hpp"
32 #include "base/tlsstream.hpp"
38 class JsonRpcConnection;
43 struct ConfigDirInformation
45 Dictionary::Ptr UpdateV1;
46 Dictionary::Ptr UpdateV2;
52 class I2_REMOTE_API ApiListener : public ObjectImpl<ApiListener>
55 DECLARE_OBJECT(ApiListener);
56 DECLARE_OBJECTNAME(ApiListener);
58 static boost::signals2::signal<void(bool)> OnMasterChanged;
62 static String GetApiDir(void);
63 static String GetCertsDir(void);
64 static String GetCaDir(void);
65 static String GetCertificateRequestsDir(void);
67 void UpdateSSLContext(void);
69 static ApiListener::Ptr GetInstance(void);
71 Endpoint::Ptr GetMaster(void) const;
72 bool IsMaster(void) const;
74 Endpoint::Ptr GetLocalEndpoint(void) const;
76 void SyncSendMessage(const Endpoint::Ptr& endpoint, const Dictionary::Ptr& message);
77 void RelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
79 static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
80 std::pair<Dictionary::Ptr, Dictionary::Ptr> GetStatus(void);
82 void AddAnonymousClient(const JsonRpcConnection::Ptr& aclient);
83 void RemoveAnonymousClient(const JsonRpcConnection::Ptr& aclient);
84 std::set<JsonRpcConnection::Ptr> GetAnonymousClients(void) const;
86 void AddHttpClient(const HttpServerConnection::Ptr& aclient);
87 void RemoveHttpClient(const HttpServerConnection::Ptr& aclient);
88 std::set<HttpServerConnection::Ptr> GetHttpClients(void) const;
90 static double CalculateZoneLag(const Endpoint::Ptr& endpoint);
93 static Value ConfigUpdateHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
96 static void ConfigUpdateObjectHandler(const ConfigObject::Ptr& object, const Value& cookie);
97 static Value ConfigUpdateObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
98 static Value ConfigDeleteObjectAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
100 static Value HelloAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
102 static void UpdateObjectAuthority(void);
104 static bool IsHACluster(void);
105 static String GetFromZoneName(const Zone::Ptr& fromZone);
108 virtual void OnConfigLoaded(void) override;
109 virtual void OnAllConfigLoaded(void) override;
110 virtual void Start(bool runtimeCreated) override;
111 virtual void Stop(bool runtimeDeleted) override;
113 virtual void ValidateTlsProtocolmin(const String& value, const ValidationUtils& utils) override;
116 boost::shared_ptr<SSL_CTX> m_SSLContext;
117 std::set<TcpSocket::Ptr> m_Servers;
118 std::set<JsonRpcConnection::Ptr> m_AnonymousClients;
119 std::set<HttpServerConnection::Ptr> m_HttpClients;
121 Timer::Ptr m_ReconnectTimer;
122 Timer::Ptr m_AuthorityTimer;
123 Timer::Ptr m_CleanupCertificateRequestsTimer;
124 Endpoint::Ptr m_LocalEndpoint;
126 static ApiListener::Ptr m_Instance;
128 void ApiTimerHandler(void);
129 void ApiReconnectTimerHandler(void);
130 void CleanupCertificateRequestsTimerHandler(void);
132 bool AddListener(const String& node, const String& service);
133 void AddConnection(const Endpoint::Ptr& endpoint);
135 void NewClientHandler(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
136 void NewClientHandlerInternal(const Socket::Ptr& client, const String& hostname, ConnectionRole role);
137 void ListenerThreadProc(const Socket::Ptr& server);
139 WorkQueue m_RelayQueue;
140 WorkQueue m_SyncQueue;
142 boost::mutex m_LogLock;
143 Stream::Ptr m_LogFile;
144 size_t m_LogMessageCount;
146 bool RelayMessageOne(const Zone::Ptr& zone, const MessageOrigin::Ptr& origin, const Dictionary::Ptr& message, const Endpoint::Ptr& currentMaster);
147 void SyncRelayMessage(const MessageOrigin::Ptr& origin, const ConfigObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
148 void PersistMessage(const Dictionary::Ptr& message, const ConfigObject::Ptr& secobj);
150 void OpenLogFile(void);
151 void RotateLogFile(void);
152 void CloseLogFile(void);
153 static void LogGlobHandler(std::vector<int>& files, const String& file);
154 void ReplayLog(const JsonRpcConnection::Ptr& client);
157 static ConfigDirInformation LoadConfigDir(const String& dir);
158 static Dictionary::Ptr MergeConfigUpdate(const ConfigDirInformation& config);
159 static bool UpdateConfigDir(const ConfigDirInformation& oldConfig, const ConfigDirInformation& newConfig, const String& configDir, bool authoritative);
161 void SyncZoneDirs(void) const;
162 void SyncZoneDir(const Zone::Ptr& zone) const;
164 static void ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file);
165 void SendConfigUpdate(const JsonRpcConnection::Ptr& aclient);
168 void UpdateConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
169 const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
170 void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
171 const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
172 void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);
174 void SyncClient(const JsonRpcConnection::Ptr& aclient, const Endpoint::Ptr& endpoint, bool needSync);
179 #endif /* APILISTENER_H */