Fix: GraphiteWriter: reconnect on broken socket.

Fixes #4883
This commit is contained in:
Gunnar Beutner 2013-10-18 09:11:21 +02:00
parent c824fc4f9f
commit 89d313008d
4 changed files with 35 additions and 8 deletions

View File

@ -37,6 +37,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/exception/diagnostic_information.hpp>
using namespace icinga;
@ -198,17 +199,28 @@ void GraphiteWriter::AddServiceMetric(std::vector<String>& metrics, const Servic
void GraphiteWriter::SendMetrics(const std::vector<String>& metrics)
{
if (!m_Stream) {
Log(LogWarning, "perfdata", "GraphiteWriter not connected!");
return;
}
BOOST_FOREACH(const String& metric, metrics) {
if (metric.IsEmpty())
continue;
Log(LogDebug, "perfdata", "GraphiteWriter: Sending metric '" + metric + "'.");
m_Stream->Write(metric.CStr(), metric.GetLength());
ObjectLock olock(this);
if (!m_Stream)
return;
try {
m_Stream->Write(metric.CStr(), metric.GetLength());
} catch (const std::exception& ex) {
std::ostringstream msgbuf;
msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
<< boost::diagnostic_information(ex);
Log(LogCritical, "base", msgbuf.str());
m_Stream.reset();
}
}
}

View File

@ -151,6 +151,9 @@ size_t BufferedStream::Read(void *buffer, size_t count)
if (m_Exception)
boost::rethrow_exception(m_Exception);
if (m_Eof)
BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to read from closed socket."));
return m_RecvQ->Read(buffer, count);
}
@ -169,6 +172,9 @@ void BufferedStream::Write(const void *buffer, size_t count)
if (m_Exception)
boost::rethrow_exception(m_Exception);
if (m_Eof)
BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to write to closed socket."));
m_SendQ->Write(buffer, count);
m_WriteCV.notify_all();
}

View File

@ -45,6 +45,9 @@ size_t NetworkStream::Read(void *buffer, size_t count)
{
size_t rc;
if (m_Eof)
BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to read from closed socket."));
try {
rc = m_Socket->Read(buffer, count);
} catch (...) {
@ -70,6 +73,9 @@ void NetworkStream::Write(const void *buffer, size_t count)
{
size_t rc;
if (m_Eof)
BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to write to closed socket."));
try {
rc = m_Socket->Write(buffer, count);
} catch (...) {
@ -78,8 +84,11 @@ void NetworkStream::Write(const void *buffer, size_t count)
throw;
}
if (rc < count)
if (rc < count) {
m_Eof = true;
BOOST_THROW_EXCEPTION(std::runtime_error("Short write for socket."));
}
}
bool NetworkStream::IsEof(void) const

View File

@ -216,7 +216,7 @@ void Socket::Listen(void)
}
/**
* Processes data that is available for this socket.
* Sends data for the socket.
*/
size_t Socket::Write(const void *buffer, size_t count)
{