From f9c4543112d981388013795ed0d925eb82124b58 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 8 Oct 2012 10:26:07 +0200 Subject: [PATCH] Compat IDO: remove more hardcoded values, prepare setting the socket --- components/compatido/compatidocomponent.cpp | 39 +++++++++++++++------ components/compatido/compatidocomponent.h | 4 +-- components/compatido/idosocket.cpp | 21 +++++++++++ components/compatido/idosocket.h | 5 +++ 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/components/compatido/compatidocomponent.cpp b/components/compatido/compatidocomponent.cpp index 848acc7e3..0a06f4715 100644 --- a/components/compatido/compatidocomponent.cpp +++ b/components/compatido/compatidocomponent.cpp @@ -98,6 +98,7 @@ bool CompatIdoComponent::GetConfigDumpInProgress(void) return m_ConfigDumpInProgress; } + /** * Starts the component. */ @@ -108,6 +109,10 @@ void CompatIdoComponent::Start(void) const int ProgramStatusTimerInterval = 15; const int ReconnectTimerInterval = GetReconnectInterval(); + /* FIXME - make this a config option when unix sockets are realdy */ + + bool IdoSocketType = true; + /* HINTS - XXX * - only tcp sockets * - only icinga idoutils 1.8 @@ -116,7 +121,7 @@ void CompatIdoComponent::Start(void) /* * open ido socket once */ - OpenIdoSocket(); + OpenIdoSocket(IdoSocketType); /* * tell ido2db that we just started @@ -175,11 +180,12 @@ void CompatIdoComponent::Stop(void) /** * Opens the ido socket, and sends hello to ido2db */ -void CompatIdoComponent::OpenIdoSocket(void) +void CompatIdoComponent::OpenIdoSocket(bool sockettype) { OpenSink(GetSocketAddress(), GetSocketPort()); - SendHello(GetInstanceName()); + SendHello(GetInstanceName(), sockettype); + m_IdoSocket->SetSocketType(sockettype); /* * if we're connected, do not reconnecte */ @@ -261,7 +267,7 @@ void CompatIdoComponent::ReconnectTimerHandler(void) } /* socket was disconnected, recconnect */ - OpenIdoSocket(); + OpenIdoSocket(m_IdoSocket->GetSocketType()); if(m_IdoSocket->IsConnected()) { Logger::Write(LogInformation, "compatido", "Successfully reconnected to ido socket"); @@ -287,19 +293,30 @@ void CompatIdoComponent::OpenSink(String node, String service) /** * sends hello msg to ido2b */ -void CompatIdoComponent::SendHello(String instancename) +void CompatIdoComponent::SendHello(String instancename, bool sockettype) { + /* FIXME */ +#define COMPATIDO_PROTOCOL 2 +#define COMPATIDO_NAME "ICINGA2 COMPATIDO" +#define COMPATIDO_RELEASE_VERSION "2.0" + + String connection; + if(sockettype) + connection = "TCPSOCKET"; + else + connection = "UNIXSOCKET"; + /* connection is always TCP */ /* connecttype is always initial */ stringstream message; message << "\n\n" << "HELLO" << "\n" - << "PROTOCOL" << ": " << 2 << "\n" - << "AGENT" << ": " << "I2 COMPATIDO" << "\n" - << "AGENTVERSION" << ": " << "2.0" << "\n" + << "PROTOCOL" << ": " << COMPATIDO_PROTOCOL<< "\n" + << "AGENT" << ": " << COMPATIDO_NAME << "\n" + << "AGENTVERSION" << ": " << COMPATIDO_RELEASE_VERSION << "\n" << "STARTTIME" << ": " << static_cast(Utility::GetTime()) << "\n" << "DISPOSITION" << ": " << "REALTIME" << "\n" - << "CONNECTION" << ": " << "TCPSOCKET" << "\n" + << "CONNECTION" << ": " << connection << "\n" << "INSTANCENAME" << ": " << instancename << "\n" << "STARTDATADUMP" << "\n\n"; @@ -337,7 +354,7 @@ void CompatIdoComponent::SendStartProcess(void) { /* TODO */ #define PROGRAM_MODIFICATION_DATE "10-17-2012" -#define VERSION "2.0" +#define PROGRAM_RELEASE_VERSION "2.0" stringstream message; message << "\n" @@ -347,7 +364,7 @@ void CompatIdoComponent::SendStartProcess(void) << 3 << "=" << "" << "\n" /* attributes */ << 4 << "=" << std::setprecision(17) << Utility::GetTime() << "\n" /* timestamp */ << 105 << "=" << "Icinga2" << "\n" /* progranname */ - << 107 << "=" << VERSION << "\n" /* programversion */ + << 107 << "=" << PROGRAM_RELEASE_VERSION << "\n" /* programversion */ << 104 << "=" << PROGRAM_MODIFICATION_DATE << "\n" /* programdata */ << 102 << "=" << Utility::GetPid() << "\n" /* process id */ << 999 << "\n\n"; /* enddata */ diff --git a/components/compatido/compatidocomponent.h b/components/compatido/compatidocomponent.h index 6c59ae6ea..682c6f2c7 100644 --- a/components/compatido/compatidocomponent.h +++ b/components/compatido/compatidocomponent.h @@ -55,11 +55,11 @@ private: void ProgramStatusTimerHandler(void); void ReconnectTimerHandler(void); - void OpenIdoSocket(void); + void OpenIdoSocket(bool sockettype); void CloseIdoSocket(void); void OpenSink(String node, String service); - void SendHello(String instancename); + void SendHello(String instancename, bool sockettype); void GoodByeSink(void); void CloseSink(void); void SendStartProcess(void); diff --git a/components/compatido/idosocket.cpp b/components/compatido/idosocket.cpp index b028f2da0..fa07791b7 100644 --- a/components/compatido/idosocket.cpp +++ b/components/compatido/idosocket.cpp @@ -44,6 +44,27 @@ IdoSocket::IdoSocket(TcpClientRole role) } + +/** + * * Set the ido socket type + * * + * * @param type true=tcp, false=unix + * */ +void IdoSocket::SetSocketType(bool type) +{ + m_SocketType = type; +} + +/* + * * Get the ido socket type + * * + * * @returns type true=tcp, false=unix + * */ +bool IdoSocket::GetSocketType(void) +{ + return m_SocketType; +} + /** * Sends a message to the ido socket * diff --git a/components/compatido/idosocket.h b/components/compatido/idosocket.h index 498677bf5..6ac356185 100644 --- a/components/compatido/idosocket.h +++ b/components/compatido/idosocket.h @@ -38,7 +38,11 @@ public: IdoSocket(TcpClientRole role); + void SetSocketType(bool); + bool GetSocketType(void); + void SendMessage(const String& message); + void SetReconnect(bool reconnect); bool GetReconnect(void); @@ -52,6 +56,7 @@ private: void ClientClosedHandler(void); bool m_Reconnect; + bool m_SocketType; friend IdoSocket::Ptr IdoSocketFactory(SOCKET fd, TcpClientRole role); };