#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;
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();
+ }
}
}
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);
}
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();
}
{
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 (...) {
{
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 (...) {
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
}
/**
- * Processes data that is available for this socket.
+ * Sends data for the socket.
*/
size_t Socket::Write(const void *buffer, size_t count)
{