Skip to content

Commit

Permalink
Add LogstashWriter feature
Browse files Browse the repository at this point in the history
This adds the UdpSocket class.

refs #4054
  • Loading branch information
Kai Goller authored and Michael Friedrich committed Feb 8, 2017
1 parent 336deea commit 444dfc3
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 5 deletions.
13 changes: 13 additions & 0 deletions etc/icinga2/features-available/logstash.conf
@@ -0,0 +1,13 @@
/**
* The LogstashWriter type writes check result metrics and
* performance data to a TCP or UDP socket.
*/

library "perfdata"

object LogstashWriter "logstash" {
//host = "127.0.0.1"
//port = 9201
/* default is tcp */
//defaultProtocol = true
}
2 changes: 1 addition & 1 deletion lib/base/CMakeLists.txt
Expand Up @@ -36,7 +36,7 @@ set(base_SOURCES
function.cpp function.thpp function-script.cpp functionwrapper.cpp scriptglobal.cpp
scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp socketevents-epoll.cpp socketevents-poll.cpp stacktrace.cpp
statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp
sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp threadpool.cpp timer.cpp
sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp udpsocket.cpp threadpool.cpp timer.cpp
tlsstream.cpp tlsutility.cpp type.cpp typetype-script.cpp unixsocket.cpp utility.cpp value.cpp
value-operators.cpp workqueue.cpp
)
Expand Down
1 change: 1 addition & 0 deletions lib/base/dictionary.cpp
Expand Up @@ -45,6 +45,7 @@ Value Dictionary::Get(const String& key) const
return it->second;
}


/**
* Retrieves a value from a dictionary.
*
Expand Down
85 changes: 84 additions & 1 deletion lib/base/socket.cpp
Expand Up @@ -29,7 +29,7 @@
#include <socketpair.h>

#ifndef _WIN32
# include <poll.h>
#include <poll.h>
#endif /* _WIN32 */

using namespace icinga;
Expand Down Expand Up @@ -423,3 +423,86 @@ void Socket::SocketPair(SOCKET s[2])
<< boost::errinfo_errno(errno));
}

/**
* Creates a socket and connects to the specified node and service.
*
* @param node The node.
* @param service The service.
* @param protocol The protocol
*/
void Socket::Connect(const String& node, const String& service)
{
addrinfo hints;
addrinfo *result;
int error;
const char *func;

SocketType();
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = socktype;
hints.ai_protocol = protocol;
int rc = getaddrinfo(node.CStr(), service.CStr(), &hints, &result);

if (rc != 0) {
Log(LogCritical, protocol+"Socket")
<< "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";

BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function("getaddrinfo")
<< errinfo_getaddrinfo_error(rc));
}

int fd = INVALID_SOCKET;

for (addrinfo *info = result; info != NULL; info = info->ai_next) {
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);

if (fd == INVALID_SOCKET) {
#ifdef _WIN32
error = WSAGetLastError();
#else /* _WIN32 */
error = errno;
#endif /* _WIN32 */
func = "socket";

continue;
}

rc = connect(fd, info->ai_addr, info->ai_addrlen);

if (rc < 0) {
#ifdef _WIN32
error = WSAGetLastError();
#else /* _WIN32 */
error = errno;
#endif /* _WIN32 */
func = "connect";

closesocket(fd);

continue;
}

SetFD(fd);

break;
}

freeaddrinfo(result);

if (GetFD() == INVALID_SOCKET) {
Log(LogCritical, "UdpSocket")
<< "Invalid socket: " << Utility::FormatErrorNumber(error);

#ifndef _WIN32
BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function(func)
<< boost::errinfo_errno(error));
#else /* _WIN32 */
BOOST_THROW_EXCEPTION(socket_error()
<< boost::errinfo_api_function(func)
<< errinfo_win32_error(error));
#endif /* _WIN32 */
}
}
7 changes: 6 additions & 1 deletion lib/base/socket.hpp
Expand Up @@ -51,18 +51,23 @@ class I2_BASE_API Socket : public Object
size_t Write(const void *buffer, size_t size);

void Listen(void);
void Connect(const String& node, const String& service);
Socket::Ptr Accept(void);

bool Poll(bool read, bool write, struct timeval *timeout = NULL);

void MakeNonBlocking(void);

static void SocketPair(SOCKET s[2]);

protected:
void SetFD(SOCKET fd);

int GetError(void) const;
int socktype;
int protocol;

virtual void SocketType(){};

mutable boost::mutex m_SocketMutex;

Expand Down
5 changes: 5 additions & 0 deletions lib/base/tcpsocket.cpp
Expand Up @@ -27,6 +27,11 @@

using namespace icinga;

void TcpSocket::SocketType(){
socktype = SOCK_STREAM;
protocol = IPPROTO_TCP;
}

/**
* Creates a socket and binds it to the specified service.
*
Expand Down
3 changes: 2 additions & 1 deletion lib/base/tcpsocket.hpp
Expand Up @@ -39,7 +39,8 @@ class I2_BASE_API TcpSocket : public Socket
void Bind(const String& service, int family);
void Bind(const String& node, const String& service, int family);

void Connect(const String& node, const String& service);
private:
void SocketType();
};

}
Expand Down
33 changes: 33 additions & 0 deletions lib/base/udpsocket.cpp
@@ -0,0 +1,33 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
* *
* 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/udpsocket.hpp"
#include "base/logger.hpp"
#include "base/utility.hpp"
#include "base/exception.hpp"
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <iostream>

using namespace icinga;

void UdpSocket::SocketType(){
socktype = SOCK_DGRAM;
protocol = IPPROTO_UDP;
}
45 changes: 45 additions & 0 deletions lib/base/udpsocket.hpp
@@ -0,0 +1,45 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
* *
* 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 UDPSOCKET_H
#define UDPSOCKET_H

#include "base/i2-base.hpp"
#include "base/socket.hpp"

namespace icinga
{

/**
* A UDP socket.
*
* @ingroup base
*/
class I2_BASE_API UdpSocket : public Socket
{
public:
DECLARE_PTR_TYPEDEFS(UdpSocket);

private:
void SocketType();
};
}

#endif /* UDPSOCKET_H */

8 changes: 7 additions & 1 deletion lib/perfdata/CMakeLists.txt
Expand Up @@ -17,12 +17,13 @@

mkclass_target(gelfwriter.ti gelfwriter.tcpp gelfwriter.thpp)
mkclass_target(graphitewriter.ti graphitewriter.tcpp graphitewriter.thpp)
mkclass_target(logstashwriter.ti logstashwriter.tcpp logstashwriter.thpp)
mkclass_target(influxdbwriter.ti influxdbwriter.tcpp influxdbwriter.thpp)
mkclass_target(opentsdbwriter.ti opentsdbwriter.tcpp opentsdbwriter.thpp)
mkclass_target(perfdatawriter.ti perfdatawriter.tcpp perfdatawriter.thpp)

set(perfdata_SOURCES
gelfwriter.cpp gelfwriter.thpp graphitewriter.cpp graphitewriter.thpp influxdbwriter.cpp influxdbwriter.thpp opentsdbwriter.cpp opentsdbwriter.thpp perfdatawriter.cpp perfdatawriter.thpp
gelfwriter.cpp gelfwriter.thpp graphitewriter.cpp graphitewriter.thpp logstashwriter.cpp logstashwriter.thpp influxdbwriter.cpp influxdbwriter.thpp opentsdbwriter.cpp opentsdbwriter.thpp perfdatawriter.cpp perfdatawriter.thpp
)

if(ICINGA2_UNITY_BUILD)
Expand Down Expand Up @@ -51,6 +52,11 @@ install_if_not_exists(
${CMAKE_INSTALL_SYSCONFDIR}/icinga2/features-available
)

install_if_not_exists(
${PROJECT_SOURCE_DIR}/etc/icinga2/features-available/logstash.conf
${CMAKE_INSTALL_SYSCONFDIR}/icinga2/features-available
)

install_if_not_exists(
${PROJECT_SOURCE_DIR}/etc/icinga2/features-available/influxdb.conf
${CMAKE_INSTALL_SYSCONFDIR}/icinga2/features-available
Expand Down
1 change: 1 addition & 0 deletions lib/perfdata/gelfwriter.hpp
Expand Up @@ -63,3 +63,4 @@ class GelfWriter : public ObjectImpl<GelfWriter>
}

#endif /* GELFWRITER_H */

0 comments on commit 444dfc3

Please sign in to comment.