2013-03-10 03:09:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "livestatus/query.h"
|
|
|
|
#include "livestatus/attributefilter.h"
|
|
|
|
#include "livestatus/negatefilter.h"
|
|
|
|
#include "livestatus/orfilter.h"
|
|
|
|
#include "livestatus/andfilter.h"
|
|
|
|
#include "icinga/externalcommandprocessor.h"
|
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/convert.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-03-18 11:02:18 +01:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
2013-03-10 09:23:13 +01:00
|
|
|
using namespace livestatus;
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Query::Query(const std::vector<String>& lines)
|
2013-03-10 15:14:14 +01:00
|
|
|
: m_KeepAlive(false), m_OutputFormat("csv"), m_ColumnHeaders(true), m_Limit(-1)
|
2013-03-10 05:10:51 +01:00
|
|
|
{
|
|
|
|
String line = lines[0];
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
size_t sp_index = line.FindFirstOf(" ");
|
|
|
|
|
|
|
|
if (sp_index == String::NPos)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Livestatus header must contain a verb."));
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
String verb = line.SubStr(0, sp_index);
|
|
|
|
String target = line.SubStr(sp_index + 1);
|
|
|
|
|
2013-03-10 05:10:51 +01:00
|
|
|
m_Verb = verb;
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-03-10 05:10:51 +01:00
|
|
|
if (m_Verb == "COMMAND") {
|
|
|
|
m_Command = target;
|
|
|
|
} else if (m_Verb == "GET") {
|
|
|
|
m_Table = target;
|
2013-03-10 03:09:01 +01:00
|
|
|
} else {
|
2013-03-10 15:11:32 +01:00
|
|
|
m_Verb = "ERROR";
|
|
|
|
m_ErrorCode = 452;
|
|
|
|
m_ErrorMessage = "Unknown livestatus verb: " + m_Verb;
|
|
|
|
return;
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::deque<Filter::Ptr> filters, stats;
|
2013-03-10 15:11:32 +01:00
|
|
|
|
|
|
|
for (unsigned int i = 1; i < lines.size(); i++) {
|
2013-03-10 05:10:51 +01:00
|
|
|
line = lines[i];
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
size_t col_index = line.FindFirstOf(":");
|
|
|
|
String header = line.SubStr(0, col_index);
|
|
|
|
String params = line.SubStr(col_index + 2);
|
|
|
|
|
|
|
|
if (header == "ResponseHeader")
|
2013-03-10 05:10:51 +01:00
|
|
|
m_ResponseHeader = params;
|
2013-03-10 09:23:13 +01:00
|
|
|
else if (header == "OutputFormat")
|
|
|
|
m_OutputFormat = params;
|
|
|
|
else if (header == "Columns")
|
2013-03-18 11:02:18 +01:00
|
|
|
boost::algorithm::split(m_Columns, params, boost::is_any_of(" "));
|
2013-03-10 15:11:32 +01:00
|
|
|
else if (header == "ColumnHeaders")
|
|
|
|
m_ColumnHeaders = (params == "on");
|
2013-03-10 17:54:46 +01:00
|
|
|
else if (header == "Filter" || header == "Stats") {
|
2013-03-18 11:02:18 +01:00
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, params, boost::is_any_of(" "));
|
2013-03-10 15:11:32 +01:00
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
if (tokens.size() == 2)
|
|
|
|
tokens.push_back("");
|
|
|
|
|
2013-03-10 15:11:32 +01:00
|
|
|
if (tokens.size() < 3) {
|
|
|
|
m_Verb = "ERROR";
|
|
|
|
m_ErrorCode = 452;
|
|
|
|
m_ErrorMessage = "Expected 3 parameters in the filter specification.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String op = tokens[1];
|
|
|
|
bool negate = false;
|
|
|
|
|
|
|
|
if (op == "!=") {
|
|
|
|
op = "=";
|
|
|
|
negate = true;
|
|
|
|
} else if (op == "!~") {
|
|
|
|
op = "~";
|
|
|
|
negate = true;
|
|
|
|
} else if (op == "!=~") {
|
|
|
|
op = "=~";
|
|
|
|
negate = true;
|
|
|
|
} else if (op == "!~~") {
|
|
|
|
op = "~~";
|
|
|
|
negate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter::Ptr filter = boost::make_shared<AttributeFilter>(tokens[0], op, tokens[2]);
|
|
|
|
|
|
|
|
if (negate)
|
|
|
|
filter = boost::make_shared<NegateFilter>(filter);
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::deque<Filter::Ptr>& deq = (header == "Filter") ? filters : stats;
|
2013-03-10 17:54:46 +01:00
|
|
|
deq.push_back(filter);
|
2013-03-10 15:11:32 +01:00
|
|
|
} else if (header == "Or" || header == "And") {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::deque<Filter::Ptr>& deq = (header == "Or" || header == "And") ? filters : stats;
|
2013-03-10 17:54:46 +01:00
|
|
|
|
2013-03-10 15:11:32 +01:00
|
|
|
int num = Convert::ToLong(params);
|
|
|
|
CombinerFilter::Ptr filter;
|
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
if (header == "Or" || header == "StatsOr")
|
2013-03-10 15:11:32 +01:00
|
|
|
filter = boost::make_shared<OrFilter>();
|
|
|
|
else
|
|
|
|
filter = boost::make_shared<AndFilter>();
|
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
if (num > deq.size()) {
|
|
|
|
m_Verb = "ERROR";
|
|
|
|
m_ErrorCode = 451;
|
|
|
|
m_ErrorMessage = "Or/StatsOr is referencing " + Convert::ToString(num) + " filters; stack only contains " + Convert::ToString(deq.size()) + " filters";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (num--) {
|
|
|
|
filter->AddSubFilter(deq.back());
|
|
|
|
deq.pop_back();
|
2013-03-10 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
deq.push_back(filter);
|
|
|
|
} else if (header == "Negate" || header == "StatsNegate") {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::deque<Filter::Ptr>& deq = (header == "Negate") ? filters : stats;
|
2013-03-10 15:27:55 +01:00
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
if (deq.empty()) {
|
|
|
|
m_Verb = "ERROR";
|
|
|
|
m_ErrorCode = 451;
|
|
|
|
m_ErrorMessage = "Negate/StatsNegate used, however the filter stack is empty";
|
|
|
|
return;
|
2013-03-10 15:27:55 +01:00
|
|
|
}
|
2013-03-10 17:54:46 +01:00
|
|
|
|
|
|
|
Filter::Ptr filter = deq.back();
|
|
|
|
filters.pop_back();
|
|
|
|
|
|
|
|
deq.push_back(boost::make_shared<NegateFilter>(filter));
|
2013-03-10 15:11:32 +01:00
|
|
|
}
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
2013-03-10 15:11:32 +01:00
|
|
|
|
2013-03-10 15:23:08 +01:00
|
|
|
/* Combine all top-level filters into a single filter. */
|
|
|
|
AndFilter::Ptr top_filter = boost::make_shared<AndFilter>();
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Filter::Ptr& filter, filters) {
|
|
|
|
top_filter->AddSubFilter(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Filter = top_filter;
|
2013-03-10 15:11:32 +01:00
|
|
|
m_Stats.swap(stats);
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
void Query::PrintResultSet(std::ostream& fp, const std::vector<String>& columns, const Array::Ptr& rs)
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2013-03-10 15:11:32 +01:00
|
|
|
if (m_OutputFormat == "csv" && m_Columns.size() == 0 && m_ColumnHeaders) {
|
2013-03-10 09:23:13 +01:00
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& column, columns) {
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
fp << ";";
|
|
|
|
|
|
|
|
fp << column;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_OutputFormat == "csv") {
|
|
|
|
ObjectLock olock(rs);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Array::Ptr& row, rs) {
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
ObjectLock rlock(row);
|
|
|
|
BOOST_FOREACH(const Value& value, row) {
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
fp << ";";
|
|
|
|
|
|
|
|
fp << Convert::ToString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp << "\n";
|
|
|
|
}
|
|
|
|
} else if (m_OutputFormat == "json") {
|
|
|
|
fp << Value(rs).Serialize();
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Query::ExecuteGetHelper(const Stream::Ptr& stream)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "livestatus", "Table: " + m_Table);
|
2013-03-10 09:23:13 +01:00
|
|
|
|
|
|
|
Table::Ptr table = Table::GetByName(m_Table);
|
|
|
|
|
|
|
|
if (!table) {
|
|
|
|
SendResponse(stream, 404, "Table '" + m_Table + "' does not exist.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Object::Ptr> objects = table->FilterRows(m_Filter);
|
|
|
|
std::vector<String> columns;
|
2013-03-10 09:23:13 +01:00
|
|
|
|
|
|
|
if (m_Columns.size() > 0)
|
|
|
|
columns = m_Columns;
|
|
|
|
else
|
|
|
|
columns = table->GetColumnNames();
|
|
|
|
|
|
|
|
Array::Ptr rs = boost::make_shared<Array>();
|
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
if (m_Stats.empty()) {
|
|
|
|
BOOST_FOREACH(const Object::Ptr& object, objects) {
|
|
|
|
Array::Ptr row = boost::make_shared<Array>();
|
2013-03-10 09:23:13 +01:00
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
BOOST_FOREACH(const String& columnName, columns) {
|
|
|
|
Column column = table->GetColumn(columnName);
|
2013-03-10 17:54:46 +01:00
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
row->Add(column.ExtractValue(object));
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
rs->Add(row);
|
|
|
|
}
|
|
|
|
} else {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<int> stats(m_Stats.size(), 0);
|
2013-03-10 17:54:46 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(const Object::Ptr& object, objects) {
|
|
|
|
int index = 0;
|
|
|
|
BOOST_FOREACH(const Filter::Ptr filter, m_Stats) {
|
|
|
|
if (filter->Apply(table, object))
|
|
|
|
stats[index]++;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 17:54:46 +01:00
|
|
|
Array::Ptr row = boost::make_shared<Array>();
|
|
|
|
for (int i = 0; i < m_Stats.size(); i++)
|
|
|
|
row->Add(stats[i]);
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
rs->Add(row);
|
2013-03-10 17:54:46 +01:00
|
|
|
|
|
|
|
m_ColumnHeaders = false;
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream result;
|
2013-03-10 09:23:13 +01:00
|
|
|
PrintResultSet(result, columns, rs);
|
|
|
|
|
|
|
|
SendResponse(stream, 200, result.str());
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Query::ExecuteCommandHelper(const Stream::Ptr& stream)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "livestatus", "Executing command: " + m_Command);
|
2013-03-10 15:11:32 +01:00
|
|
|
ExternalCommandProcessor::Execute(m_Command);
|
|
|
|
SendResponse(stream, 200, "");
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Query::ExecuteErrorHelper(const Stream::Ptr& stream)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-10 17:54:46 +01:00
|
|
|
SendResponse(stream, m_ErrorCode, m_ErrorMessage);
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Query::SendResponse(const Stream::Ptr& stream, int code, const String& data)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
|
|
|
if (m_ResponseHeader == "fixed16")
|
|
|
|
PrintFixed16(stream, code, data);
|
2013-03-10 05:10:51 +01:00
|
|
|
|
|
|
|
if (m_ResponseHeader == "fixed16" || code == 200)
|
2013-03-10 03:09:01 +01:00
|
|
|
stream->Write(data.CStr(), data.GetLength());
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Query::PrintFixed16(const Stream::Ptr& stream, int code, const String& data)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
|
|
|
ASSERT(code >= 100 && code <= 999);
|
|
|
|
|
|
|
|
String sCode = Convert::ToString(code);
|
|
|
|
String sLength = Convert::ToString(data.GetLength());
|
|
|
|
|
|
|
|
String header = sCode + String(16 - 3 - sLength.GetLength() - 1, ' ') + sLength + "\n";
|
|
|
|
stream->Write(header.CStr(), header.GetLength());
|
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
void Query::Execute(const Stream::Ptr& stream)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-10 15:11:32 +01:00
|
|
|
try {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "livestatus", "Executing livestatus query: " + m_Verb);
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
if (m_Verb == "GET")
|
|
|
|
ExecuteGetHelper(stream);
|
|
|
|
else if (m_Verb == "COMMAND")
|
|
|
|
ExecuteCommandHelper(stream);
|
|
|
|
else if (m_Verb == "ERROR")
|
|
|
|
ExecuteErrorHelper(stream);
|
|
|
|
else
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid livestatus query verb."));
|
2013-03-10 15:11:32 +01:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
SendResponse(stream, 452, boost::diagnostic_information(ex));
|
|
|
|
}
|
2013-03-10 05:10:51 +01:00
|
|
|
|
|
|
|
if (!m_KeepAlive)
|
|
|
|
stream->Close();
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|