1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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/apiclient.hpp"
25 #include "remote/endpoint.hpp"
26 #include "remote/messageorigin.hpp"
27 #include "base/dynamicobject.hpp"
28 #include "base/timer.hpp"
29 #include "base/workqueue.hpp"
30 #include "base/tcpsocket.hpp"
31 #include "base/tlsstream.hpp"
42 class I2_REMOTE_API ApiListener : public ObjectImpl<ApiListener>
45 DECLARE_OBJECT(ApiListener);
46 DECLARE_OBJECTNAME(ApiListener);
48 static boost::signals2::signal<void(bool)> OnMasterChanged;
50 static ApiListener::Ptr GetInstance(void);
52 boost::shared_ptr<SSL_CTX> GetSSLContext(void) const;
54 Endpoint::Ptr GetMaster(void) const;
55 bool IsMaster(void) const;
57 static String GetApiDir(void);
59 void SyncSendMessage(const Endpoint::Ptr& endpoint, const Dictionary::Ptr& message);
60 void RelayMessage(const MessageOrigin& origin, const DynamicObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
62 static Value StatsFunc(Dictionary::Ptr& status, Array::Ptr& perfdata);
63 std::pair<Dictionary::Ptr, Dictionary::Ptr> GetStatus(void);
65 void AddAnonymousClient(const ApiClient::Ptr& aclient);
66 void RemoveAnonymousClient(const ApiClient::Ptr& aclient);
67 std::set<ApiClient::Ptr> GetAnonymousClients(void) const;
69 static Value ConfigUpdateHandler(const MessageOrigin& origin, const Dictionary::Ptr& params);
72 virtual void OnConfigLoaded(void);
73 virtual void OnAllConfigLoaded(void);
74 virtual void Start(void);
77 boost::shared_ptr<SSL_CTX> m_SSLContext;
78 std::set<TcpSocket::Ptr> m_Servers;
79 std::set<ApiClient::Ptr> m_AnonymousClients;
82 void ApiTimerHandler(void);
84 bool AddListener(const String& node, const String& service);
85 void AddConnection(const Endpoint::Ptr& endpoint);
87 void NewClientHandler(const Socket::Ptr& client, ConnectionRole role);
88 void ListenerThreadProc(const Socket::Ptr& server);
90 WorkQueue m_RelayQueue;
92 boost::mutex m_LogLock;
93 Stream::Ptr m_LogFile;
94 size_t m_LogMessageCount;
96 void SyncRelayMessage(const MessageOrigin& origin, const DynamicObject::Ptr& secobj, const Dictionary::Ptr& message, bool log);
97 void PersistMessage(const Dictionary::Ptr& message, const DynamicObject::Ptr& secobj);
99 void OpenLogFile(void);
100 void RotateLogFile(void);
101 void CloseLogFile(void);
102 static void LogGlobHandler(std::vector<int>& files, const String& file);
103 void ReplayLog(const ApiClient::Ptr& client);
105 static Dictionary::Ptr LoadConfigDir(const String& dir);
106 static bool UpdateConfigDir(const Dictionary::Ptr& oldConfig, const Dictionary::Ptr& newConfig, const String& configDir, bool authoritative);
108 void SyncZoneDirs(void) const;
109 void SyncZoneDir(const Zone::Ptr& zone) const;
111 bool IsConfigMaster(const Zone::Ptr& zone) const;
112 static void ConfigGlobHandler(Dictionary::Ptr& config, const String& path, const String& file);
113 void SendConfigUpdate(const ApiClient::Ptr& aclient);
118 #endif /* APILISTENER_H */