2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-10-19 17:31:18 +02:00
|
|
|
|
|
|
|
#ifndef EVENTQUEUE_H
|
|
|
|
#define EVENTQUEUE_H
|
|
|
|
|
|
|
|
#include "remote/httphandler.hpp"
|
|
|
|
#include "base/object.hpp"
|
|
|
|
#include "config/expression.hpp"
|
2019-04-05 17:02:42 +02:00
|
|
|
#include <boost/asio/deadline_timer.hpp>
|
2019-02-15 12:32:22 +01:00
|
|
|
#include <boost/asio/spawn.hpp>
|
2015-10-19 17:31:18 +02:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/condition_variable.hpp>
|
2019-04-05 17:02:42 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <mutex>
|
2015-10-19 17:31:18 +02:00
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
#include <deque>
|
2019-04-05 17:02:42 +02:00
|
|
|
#include <queue>
|
2015-10-19 17:31:18 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2018-01-04 06:11:04 +01:00
|
|
|
class EventQueue final : public Object
|
2015-10-19 17:31:18 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(EventQueue);
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
EventQueue(String name);
|
2015-10-19 17:31:18 +02:00
|
|
|
|
|
|
|
bool CanProcessEvent(const String& type) const;
|
|
|
|
void ProcessEvent(const Dictionary::Ptr& event);
|
|
|
|
void AddClient(void *client);
|
|
|
|
void RemoveClient(void *client);
|
|
|
|
|
|
|
|
void SetTypes(const std::set<String>& types);
|
2017-12-15 05:34:46 +01:00
|
|
|
void SetFilter(std::unique_ptr<Expression> filter);
|
2015-10-19 17:31:18 +02:00
|
|
|
|
|
|
|
static std::vector<EventQueue::Ptr> GetQueuesForType(const String& type);
|
|
|
|
static void UnregisterIfUnused(const String& name, const EventQueue::Ptr& queue);
|
|
|
|
|
|
|
|
static EventQueue::Ptr GetByName(const String& name);
|
|
|
|
static void Register(const String& name, const EventQueue::Ptr& function);
|
|
|
|
static void Unregister(const String& name);
|
|
|
|
|
|
|
|
private:
|
2016-09-02 08:51:51 +02:00
|
|
|
String m_Name;
|
|
|
|
|
2015-10-19 17:31:18 +02:00
|
|
|
mutable boost::mutex m_Mutex;
|
|
|
|
boost::condition_variable m_CV;
|
|
|
|
|
|
|
|
std::set<String> m_Types;
|
2017-12-15 05:34:46 +01:00
|
|
|
std::unique_ptr<Expression> m_Filter;
|
2015-10-19 17:31:18 +02:00
|
|
|
|
|
|
|
std::map<void *, std::deque<Dictionary::Ptr> > m_Events;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A registry for API event queues.
|
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class EventQueueRegistry : public Registry<EventQueueRegistry, EventQueue::Ptr>
|
2015-10-19 17:31:18 +02:00
|
|
|
{
|
|
|
|
public:
|
2018-01-04 04:25:35 +01:00
|
|
|
static EventQueueRegistry *GetInstance();
|
2015-10-19 17:31:18 +02:00
|
|
|
};
|
|
|
|
|
2019-04-05 17:02:42 +02:00
|
|
|
enum class EventType : uint_fast8_t
|
|
|
|
{
|
|
|
|
AcknowledgementCleared,
|
|
|
|
AcknowledgementSet,
|
|
|
|
CheckResult,
|
|
|
|
CommentAdded,
|
|
|
|
CommentRemoved,
|
|
|
|
DowntimeAdded,
|
|
|
|
DowntimeRemoved,
|
|
|
|
DowntimeStarted,
|
|
|
|
DowntimeTriggered,
|
|
|
|
Flapping,
|
|
|
|
Notification,
|
|
|
|
StateChange
|
|
|
|
};
|
|
|
|
|
|
|
|
class EventsInbox : public Object
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DECLARE_PTR_TYPEDEFS(EventsInbox);
|
|
|
|
|
|
|
|
EventsInbox(String filter, const String& filterSource);
|
|
|
|
EventsInbox(const EventsInbox&) = delete;
|
|
|
|
EventsInbox(EventsInbox&&) = delete;
|
|
|
|
EventsInbox& operator=(const EventsInbox&) = delete;
|
|
|
|
EventsInbox& operator=(EventsInbox&&) = delete;
|
|
|
|
~EventsInbox();
|
|
|
|
|
|
|
|
const std::shared_ptr<Expression>& GetFilter();
|
|
|
|
|
|
|
|
void Push(Dictionary::Ptr event);
|
|
|
|
Dictionary::Ptr Shift(boost::asio::yield_context yc, double timeout = 5);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Filter
|
|
|
|
{
|
|
|
|
std::size_t Refs;
|
|
|
|
std::shared_ptr<Expression> Expr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::mutex m_FiltersMutex;
|
|
|
|
static std::map<String, Filter> m_Filters;
|
|
|
|
|
|
|
|
std::mutex m_Mutex;
|
|
|
|
decltype(m_Filters.begin()) m_Filter;
|
|
|
|
std::queue<Dictionary::Ptr> m_Queue;
|
|
|
|
boost::asio::deadline_timer m_Timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EventsSubscriber
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EventsSubscriber(std::set<EventType> types, String filter, const String& filterSource);
|
|
|
|
EventsSubscriber(const EventsSubscriber&) = delete;
|
|
|
|
EventsSubscriber(EventsSubscriber&&) = delete;
|
|
|
|
EventsSubscriber& operator=(const EventsSubscriber&) = delete;
|
|
|
|
EventsSubscriber& operator=(EventsSubscriber&&) = delete;
|
|
|
|
~EventsSubscriber();
|
|
|
|
|
2019-04-05 17:34:46 +02:00
|
|
|
const EventsInbox::Ptr& GetInbox();
|
|
|
|
|
2019-04-05 17:02:42 +02:00
|
|
|
private:
|
|
|
|
std::set<EventType> m_Types;
|
|
|
|
EventsInbox::Ptr m_Inbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EventsFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EventsFilter(std::map<std::shared_ptr<Expression>, std::set<EventsInbox::Ptr>> inboxes);
|
|
|
|
|
|
|
|
operator bool();
|
|
|
|
|
|
|
|
void Push(Dictionary::Ptr event);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<std::shared_ptr<Expression>, std::set<EventsInbox::Ptr>> m_Inboxes;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EventsRouter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static EventsRouter& GetInstance();
|
|
|
|
|
|
|
|
void Subscribe(const std::set<EventType>& types, const EventsInbox::Ptr& inbox);
|
|
|
|
void Unsubscribe(const std::set<EventType>& types, const EventsInbox::Ptr& inbox);
|
|
|
|
EventsFilter GetInboxes(EventType type);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static EventsRouter m_Instance;
|
|
|
|
|
|
|
|
EventsRouter() = default;
|
|
|
|
EventsRouter(const EventsRouter&) = delete;
|
|
|
|
EventsRouter(EventsRouter&&) = delete;
|
|
|
|
EventsRouter& operator=(const EventsRouter&) = delete;
|
|
|
|
EventsRouter& operator=(EventsRouter&&) = delete;
|
|
|
|
~EventsRouter() = default;
|
|
|
|
|
|
|
|
std::mutex m_Mutex;
|
|
|
|
std::map<EventType, std::map<std::shared_ptr<Expression>, std::set<EventsInbox::Ptr>>> m_Subscribers;
|
|
|
|
};
|
|
|
|
|
2015-10-19 17:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EVENTQUEUE_H */
|