livestatus: use enum for error codes, fix empty line

refs #4372
This commit is contained in:
Michael Friedrich 2013-07-12 18:25:18 +02:00
parent 45db7e08a2
commit 358aa3a6a5
2 changed files with 23 additions and 9 deletions

View File

@ -47,6 +47,13 @@ using namespace livestatus;
Query::Query(const std::vector<String>& lines) Query::Query(const std::vector<String>& lines)
: m_KeepAlive(false), m_OutputFormat("csv"), m_ColumnHeaders(true), m_Limit(-1) : m_KeepAlive(false), m_OutputFormat("csv"), m_ColumnHeaders(true), m_Limit(-1)
{ {
if (lines.size() == 0) {
m_Verb = "ERROR";
m_ErrorCode = LivestatusErrorQuery;
m_ErrorMessage = "Empty Query. Aborting.";
return;
}
String line = lines[0]; String line = lines[0];
size_t sp_index = line.FindFirstOf(" "); size_t sp_index = line.FindFirstOf(" ");
@ -65,7 +72,7 @@ Query::Query(const std::vector<String>& lines)
m_Table = target; m_Table = target;
} else { } else {
m_Verb = "ERROR"; m_Verb = "ERROR";
m_ErrorCode = 452; m_ErrorCode = LivestatusErrorQuery;
m_ErrorMessage = "Unknown livestatus verb: " + m_Verb; m_ErrorMessage = "Unknown livestatus verb: " + m_Verb;
return; return;
} }
@ -96,7 +103,7 @@ Query::Query(const std::vector<String>& lines)
if (!filter) { if (!filter) {
m_Verb = "ERROR"; m_Verb = "ERROR";
m_ErrorCode = 452; m_ErrorCode = LivestatusErrorQuery;
m_ErrorMessage = "Invalid filter specification: " + line; m_ErrorMessage = "Invalid filter specification: " + line;
return; return;
} }
@ -108,7 +115,7 @@ Query::Query(const std::vector<String>& lines)
if (tokens.size() < 2) { if (tokens.size() < 2) {
m_Verb = "ERROR"; m_Verb = "ERROR";
m_ErrorCode = 452; m_ErrorCode = LivestatusErrorQuery;
m_ErrorMessage = "Missing aggregator column name: " + line; m_ErrorMessage = "Missing aggregator column name: " + line;
return; return;
} }
@ -138,7 +145,7 @@ Query::Query(const std::vector<String>& lines)
if (!filter) { if (!filter) {
m_Verb = "ERROR"; m_Verb = "ERROR";
m_ErrorCode = 452; m_ErrorCode = LivestatusErrorQuery;
m_ErrorMessage = "Invalid filter specification: " + line; m_ErrorMessage = "Invalid filter specification: " + line;
return; return;
} }
@ -312,7 +319,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
Table::Ptr table = Table::GetByName(m_Table); Table::Ptr table = Table::GetByName(m_Table);
if (!table) { if (!table) {
SendResponse(stream, 404, "Table '" + m_Table + "' does not exist."); SendResponse(stream, LivestatusErrorNotFound, "Table '" + m_Table + "' does not exist.");
return; return;
} }
@ -364,14 +371,14 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
std::ostringstream result; std::ostringstream result;
PrintResultSet(result, columns, rs); PrintResultSet(result, columns, rs);
SendResponse(stream, 200, result.str()); SendResponse(stream, LivestatusErrorOK, result.str());
} }
void Query::ExecuteCommandHelper(const Stream::Ptr& stream) void Query::ExecuteCommandHelper(const Stream::Ptr& stream)
{ {
Log(LogInformation, "livestatus", "Executing command: " + m_Command); Log(LogInformation, "livestatus", "Executing command: " + m_Command);
ExternalCommandProcessor::Execute(m_Command); ExternalCommandProcessor::Execute(m_Command);
SendResponse(stream, 200, ""); SendResponse(stream, LivestatusErrorOK, "");
} }
void Query::ExecuteErrorHelper(const Stream::Ptr& stream) void Query::ExecuteErrorHelper(const Stream::Ptr& stream)
@ -384,7 +391,7 @@ void Query::SendResponse(const Stream::Ptr& stream, int code, const String& data
if (m_ResponseHeader == "fixed16") if (m_ResponseHeader == "fixed16")
PrintFixed16(stream, code, data); PrintFixed16(stream, code, data);
if (m_ResponseHeader == "fixed16" || code == 200) if (m_ResponseHeader == "fixed16" || code == LivestatusErrorOK)
stream->Write(data.CStr(), data.GetLength()); stream->Write(data.CStr(), data.GetLength());
} }
@ -417,7 +424,7 @@ bool Query::Execute(const Stream::Ptr& stream)
std::ostringstream info; std::ostringstream info;
st->Print(info); st->Print(info);
Log(LogWarning, "livestatus", info.str()); Log(LogWarning, "livestatus", info.str());
SendResponse(stream, 452, boost::diagnostic_information(ex)); SendResponse(stream, LivestatusErrorQuery, boost::diagnostic_information(ex));
} }
if (!m_KeepAlive) { if (!m_KeepAlive) {

View File

@ -32,6 +32,13 @@ using namespace icinga;
namespace livestatus namespace livestatus
{ {
enum LivestatusError
{
LivestatusErrorOK = 200,
LivestatusErrorNotFound = 404,
LivestatusErrorQuery = 452
};
/** /**
* @ingroup livestatus * @ingroup livestatus
*/ */