]> granicus.if.org Git - icinga2/blob - lib/remote/apilistener.hpp
Make sure we don't include zones.d directories for zones which were removed
[icinga2] / lib / remote / apilistener.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef APILISTENER_H
21 #define APILISTENER_H
22
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"
32 #include <set>
33
34 namespace icinga
35 {
36
37 class ApiClient;
38
39 /**
40 * @ingroup remote
41 */
42 class I2_REMOTE_API ApiListener : public ObjectImpl<ApiListener>
43 {
44 public:
45         DECLARE_OBJECT(ApiListener);
46         DECLARE_OBJECTNAME(ApiListener);
47
48         static boost::signals2::signal<void(bool)> OnMasterChanged;
49
50         static ApiListener::Ptr GetInstance(void);
51
52         boost::shared_ptr<SSL_CTX> GetSSLContext(void) const;
53
54         Endpoint::Ptr GetMaster(void) const;
55         bool IsMaster(void) const;
56
57         static String GetApiDir(void);
58
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);
61
62         static Value StatsFunc(Dictionary::Ptr& status, Array::Ptr& perfdata);
63         std::pair<Dictionary::Ptr, Dictionary::Ptr> GetStatus(void);
64
65         void AddAnonymousClient(const ApiClient::Ptr& aclient);
66         void RemoveAnonymousClient(const ApiClient::Ptr& aclient);
67         std::set<ApiClient::Ptr> GetAnonymousClients(void) const;
68
69         static Value ConfigUpdateHandler(const MessageOrigin& origin, const Dictionary::Ptr& params);
70
71 protected:
72         virtual void OnConfigLoaded(void);
73         virtual void OnAllConfigLoaded(void);
74         virtual void Start(void);
75
76 private:
77         boost::shared_ptr<SSL_CTX> m_SSLContext;
78         std::set<TcpSocket::Ptr> m_Servers;
79         std::set<ApiClient::Ptr> m_AnonymousClients;
80         Timer::Ptr m_Timer;
81
82         void ApiTimerHandler(void);
83
84         bool AddListener(const String& node, const String& service);
85         void AddConnection(const Endpoint::Ptr& endpoint);
86
87         void NewClientHandler(const Socket::Ptr& client, ConnectionRole role);
88         void ListenerThreadProc(const Socket::Ptr& server);
89
90         WorkQueue m_RelayQueue;
91
92         boost::mutex m_LogLock;
93         Stream::Ptr m_LogFile;
94         size_t m_LogMessageCount;
95
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);
98
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);
104
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);
107
108         void SyncZoneDirs(void) const;
109         void SyncZoneDir(const Zone::Ptr& zone) const;
110
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);
114 };
115
116 }
117
118 #endif /* APILISTENER_H */