Add RedisWriter class

refs #4991
This commit is contained in:
Gunnar Beutner 2017-02-09 14:15:23 +01:00 committed by Michael Friedrich
parent 55e4881337
commit 31650b0beb
8 changed files with 253 additions and 0 deletions

View File

@ -37,6 +37,7 @@ option(ICINGA2_WITH_HELLO "Build the hello module" OFF)
option(ICINGA2_WITH_LIVESTATUS "Build the Livestatus module" ON)
option(ICINGA2_WITH_NOTIFICATION "Build the notification module" ON)
option(ICINGA2_WITH_PERFDATA "Build the perfdata module" ON)
option(ICINGA2_WITH_REDIS "Build the redis module" ON)
option(ICINGA2_WITH_STUDIO "Build the Icinga Studio application" OFF)
option(ICINGA2_WITH_TESTS "Run unit tests" ON)

View File

@ -0,0 +1,12 @@
/**
* The redis library implements functionality for putting Icinga
* event data into a redis database.
*/
library "redis"
object RedisWriter "redis" {
//host = "127.0.0.1"
//port = 6379
//password = "xxx"
}

View File

@ -62,4 +62,8 @@ if(ICINGA2_WITH_PERFDATA)
add_subdirectory(perfdata)
endif()
if(ICINGA2_WITH_REDIS)
add_subdirectory(redis)
endif()
set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "${CPACK_NSIS_EXTRA_INSTALL_COMMANDS}" PARENT_SCOPE)

53
lib/redis/CMakeLists.txt Normal file
View File

@ -0,0 +1,53 @@
# Icinga 2
# Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)
#
# 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.
mkclass_target(rediswriter.ti rediswriter.tcpp rediswriter.thpp)
set(redis_SOURCES
rediswriter.cpp rediswriter.thpp
)
if(ICINGA2_UNITY_BUILD)
mkunity_target(redis redis redis_SOURCES)
endif()
add_library(redis SHARED ${redis_SOURCES})
target_link_libraries(redis ${Boost_LIBRARIES} base config icinga remote hiredis)
include_directories(${icinga2_SOURCE_DIR}/third-party)
link_directories(${icinga2_BINARY_DIR}/third-party/hiredis)
set_target_properties (
redis PROPERTIES
INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR}/icinga2
DEFINE_SYMBOL I2_REDIS_BUILD
FOLDER Components
VERSION ${SPEC_VERSION}
)
install_if_not_exists(
${PROJECT_SOURCE_DIR}/etc/icinga2/features-available/redis.conf
${CMAKE_INSTALL_SYSCONFDIR}/icinga2/features-available
)
install(
TARGETS redis
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/icinga2
)

90
lib/redis/rediswriter.cpp Normal file
View File

@ -0,0 +1,90 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
* *
* 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. *
******************************************************************************/
#include "redis/rediswriter.hpp"
#include "redis/rediswriter.tcpp"
#include "remote/eventqueue.hpp"
#include "base/json.hpp"
using namespace icinga;
REGISTER_TYPE(RedisWriter);
/**
* Starts the component.
*/
void RedisWriter::Start(bool runtimeCreated)
{
ObjectImpl<RedisWriter>::Start(runtimeCreated);
boost::thread thread(boost::bind(&RedisWriter::HandleEvents, this));
thread.detach();
String host = GetHost();
m_Context = redisConnect(host.CStr(), GetPort());
String password = GetPassword();
void *reply = redisCommand(m_Context, "AUTH %s", password.CStr());
freeReplyObject(reply);
}
void RedisWriter::HandleEvents(void)
{
String queueName = Utility::NewUniqueID();
EventQueue::Ptr queue = new EventQueue(queueName);
EventQueue::Register(queueName, queue);
std::set<String> types;
types.insert("CheckResult");
types.insert("StateChange");
types.insert("Notification");
types.insert("AcknowledgementSet");
types.insert("AcknowledgementCleared");
types.insert("CommentAdded");
types.insert("CommentRemoved");
types.insert("DowntimeAdded");
types.insert("DowntimeRemoved");
types.insert("DowntimeStarted");
types.insert("DowntimeTriggered");
queue->SetTypes(types);
queue->AddClient(this);
for (;;) {
Dictionary::Ptr result = queue->WaitForEvent(this);
if (!result)
continue;
String body = JsonEncode(result);
//TODO: Reconnect handling
try {
void *reply = redisCommand(m_Context, "LPUSH icinga:events %s", body.CStr());
freeReplyObject(reply);
} catch (const std::exception&) {
queue->RemoveClient(this);
EventQueue::UnregisterIfUnused(queueName, queue);
throw;
}
}
}

50
lib/redis/rediswriter.hpp Normal file
View File

@ -0,0 +1,50 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
* *
* 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. *
******************************************************************************/
#ifndef REDISWRITER_H
#define REDISWRITER_H
#include "redis/rediswriter.thpp"
#include "remote/messageorigin.hpp"
#include "base/timer.hpp"
#include <hiredis/hiredis.h>
namespace icinga
{
/**
* @ingroup redis
*/
class RedisWriter : public ObjectImpl<RedisWriter>
{
public:
DECLARE_OBJECT(RedisWriter);
DECLARE_OBJECTNAME(RedisWriter);
virtual void Start(bool runtimeCreated) override;
private:
void HandleEvents(void);
redisContext *m_Context;
};
}
#endif /* REDISWRITER_H */

38
lib/redis/rediswriter.ti Normal file
View File

@ -0,0 +1,38 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
* *
* 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. *
******************************************************************************/
#include "base/configobject.hpp"
library demo;
namespace icinga
{
class RedisWriter : ConfigObject
{
[config] String host {
default {{{ return "127.0.0.1"; }}}
};
[config] int port {
default {{{ return 6379; }}}
};
[config] String password;
};
}

View File

@ -17,6 +17,11 @@
add_library(hiredis SHARED net.c net.h hiredis.c hiredis.h sds.c sds.h async.c async.h read.c read.h)
if(HAVE_VISIBILITY_HIDDEN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=default")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=default")
endif()
set_target_properties (
hiredis PROPERTIES
FOLDER Lib