From 2a097d0b04a1042d1d330a028b4cb8464c7ea366 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 10 Jul 2013 16:11:40 +0200 Subject: [PATCH] livestatus: more fixes on joins, ptrs refs #4372 --- components/livestatus/andfilter.cpp | 4 +-- components/livestatus/andfilter.h | 2 +- components/livestatus/attributefilter.cpp | 4 +-- components/livestatus/attributefilter.h | 2 +- components/livestatus/column.cpp | 10 +++--- components/livestatus/column.h | 6 ++-- components/livestatus/commentstable.cpp | 41 +++++++++++++---------- components/livestatus/commentstable.h | 2 +- components/livestatus/downtimestable.cpp | 16 ++++----- components/livestatus/downtimestable.h | 3 ++ components/livestatus/filter.h | 2 +- components/livestatus/negatefilter.cpp | 4 +-- components/livestatus/negatefilter.h | 2 +- components/livestatus/orfilter.cpp | 4 +-- components/livestatus/orfilter.h | 2 +- components/livestatus/query.cpp | 31 ++++++++++------- components/livestatus/servicestable.cpp | 6 ++-- components/livestatus/servicestable.h | 2 +- components/livestatus/table.cpp | 10 +++--- components/livestatus/table.h | 4 +-- 20 files changed, 85 insertions(+), 72 deletions(-) diff --git a/components/livestatus/andfilter.cpp b/components/livestatus/andfilter.cpp index 73ab38927..2fb8eb6b3 100644 --- a/components/livestatus/andfilter.cpp +++ b/components/livestatus/andfilter.cpp @@ -26,10 +26,10 @@ using namespace livestatus; AndFilter::AndFilter(void) { } -bool AndFilter::Apply(const Table::Ptr& table, const Object::Ptr& object) +bool AndFilter::Apply(const Table::Ptr& table, const Value& row) { BOOST_FOREACH(const Filter::Ptr& filter, m_Filters) { - if (!filter->Apply(table, object)) + if (!filter->Apply(table, row)) return false; } diff --git a/components/livestatus/andfilter.h b/components/livestatus/andfilter.h index aafe5bf98..8cee8df4b 100644 --- a/components/livestatus/andfilter.h +++ b/components/livestatus/andfilter.h @@ -37,7 +37,7 @@ public: AndFilter(void); - virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object); + virtual bool Apply(const Table::Ptr& table, const Value& row); }; } diff --git a/components/livestatus/attributefilter.cpp b/components/livestatus/attributefilter.cpp index c02dc93d8..27e13af7c 100644 --- a/components/livestatus/attributefilter.cpp +++ b/components/livestatus/attributefilter.cpp @@ -29,11 +29,11 @@ AttributeFilter::AttributeFilter(const String& column, const String& op, const S : m_Column(column), m_Operator(op), m_Operand(operand) { } -bool AttributeFilter::Apply(const Table::Ptr& table, const Object::Ptr& object) +bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row) { Column column = table->GetColumn(m_Column); - Value value = column.ExtractValue(object); + Value value = column.ExtractValue(row); if (value.IsObjectType()) { if (m_Operator == ">=") { diff --git a/components/livestatus/attributefilter.h b/components/livestatus/attributefilter.h index 939f030d6..faee695d4 100644 --- a/components/livestatus/attributefilter.h +++ b/components/livestatus/attributefilter.h @@ -37,7 +37,7 @@ public: AttributeFilter(const String& column, const String& op, const String& operand); - virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object); + virtual bool Apply(const Table::Ptr& table, const Value& row); protected: String m_Column; diff --git a/components/livestatus/column.cpp b/components/livestatus/column.cpp index 2ab9e7fce..bef4b51d9 100644 --- a/components/livestatus/column.cpp +++ b/components/livestatus/column.cpp @@ -26,14 +26,14 @@ Column::Column(const ValueAccessor& valueAccessor, const ObjectAccessor& objectA : m_ValueAccessor(valueAccessor), m_ObjectAccessor(objectAccessor) { } -Value Column::ExtractValue(const Object::Ptr& uobject) const +Value Column::ExtractValue(const Value& urow) const { - Object::Ptr object; + Value row; if (!m_ObjectAccessor.empty()) - object = m_ObjectAccessor(uobject); + row = m_ObjectAccessor(urow); else - object = uobject; + row = urow; - return m_ValueAccessor(object); + return m_ValueAccessor(row); } diff --git a/components/livestatus/column.h b/components/livestatus/column.h index e259a6234..52d5f2b73 100644 --- a/components/livestatus/column.h +++ b/components/livestatus/column.h @@ -31,12 +31,12 @@ namespace livestatus class Column { public: - typedef boost::function ValueAccessor; - typedef boost::function ObjectAccessor; + typedef boost::function ValueAccessor; + typedef boost::function ObjectAccessor; Column(const ValueAccessor& valueAccessor, const ObjectAccessor& objectAccessor); - Value ExtractValue(const Object::Ptr& uobject) const; + Value ExtractValue(const Value& urow) const; private: ValueAccessor m_ValueAccessor; diff --git a/components/livestatus/commentstable.cpp b/components/livestatus/commentstable.cpp index fa9bff2e3..05c077bf9 100644 --- a/components/livestatus/commentstable.cpp +++ b/components/livestatus/commentstable.cpp @@ -19,7 +19,6 @@ #include "livestatus/commentstable.h" #include "livestatus/servicestable.h" -#include "livestatus/hoststable.h" #include "icinga/service.h" #include "base/dynamictype.h" #include "base/objectlock.h" @@ -49,8 +48,6 @@ void CommentsTable::AddColumns(Table *table, const String& prefix, table->AddColumn(prefix + "expires", Column(&CommentsTable::ExpiresAccessor, objectAccessor)); table->AddColumn(prefix + "expire_time", Column(&CommentsTable::ExpireTimeAccessor, objectAccessor)); - // TODO: Join hosts and services table with prefix - HostsTable::AddColumns(table, "host_", &CommentsTable::HostAccessor); ServicesTable::AddColumns(table, "service_", &CommentsTable::ServiceAccessor); } @@ -70,10 +67,6 @@ void CommentsTable::FetchRows(const AddRowFunction& addRowFn) ObjectLock olock(comments); - /*Value comment; - BOOST_FOREACH(boost::tie(boost::tuples::ignore, comment), comments) { - addRowFn(comment); - }*/ String id; BOOST_FOREACH(boost::tie(id, boost::tuples::ignore), comments) { addRowFn(id); @@ -81,16 +74,6 @@ void CommentsTable::FetchRows(const AddRowFunction& addRowFn) } } -Object::Ptr CommentsTable::HostAccessor(const Value& row) -{ - Service::Ptr svc = Service::GetOwnerByCommentID(row); - - if (!svc) - return Value(); - - return svc->GetHost(); -} - Object::Ptr CommentsTable::ServiceAccessor(const Value& row) { return Service::GetOwnerByCommentID(row); @@ -100,6 +83,9 @@ Value CommentsTable::AuthorAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("author"); } @@ -107,6 +93,9 @@ Value CommentsTable::CommentAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("text"); } @@ -114,6 +103,9 @@ Value CommentsTable::IdAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("legacy_id"); } @@ -121,6 +113,9 @@ Value CommentsTable::EntryTimeAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("entry_time"); } @@ -134,6 +129,9 @@ Value CommentsTable::IsServiceAccessor(const Value& row) { Service::Ptr svc = Service::GetOwnerByCommentID(row); + if (!svc) + return Value(); + return (svc->IsHostCheck() ? 0 : 1); } @@ -153,6 +151,9 @@ Value CommentsTable::EntryTypeAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("entry_type"); } @@ -160,6 +161,9 @@ Value CommentsTable::ExpiresAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("expires"); } @@ -167,5 +171,8 @@ Value CommentsTable::ExpireTimeAccessor(const Value& row) { Dictionary::Ptr comment = Service::GetCommentByID(row); + if (!comment) + return Value(); + return comment->Get("expire_time"); } diff --git a/components/livestatus/commentstable.h b/components/livestatus/commentstable.h index 8663ed306..8d7ea1863 100644 --- a/components/livestatus/commentstable.h +++ b/components/livestatus/commentstable.h @@ -45,7 +45,7 @@ public: protected: virtual void FetchRows(const AddRowFunction& addRowFn); - static Object::Ptr HostAccessor(const Value& row); +private: static Object::Ptr ServiceAccessor(const Value& row); static Value AuthorAccessor(const Value& row); diff --git a/components/livestatus/downtimestable.cpp b/components/livestatus/downtimestable.cpp index 6b6474294..f8e6650c9 100644 --- a/components/livestatus/downtimestable.cpp +++ b/components/livestatus/downtimestable.cpp @@ -18,6 +18,7 @@ ******************************************************************************/ #include "livestatus/downtimestable.h" +#include "livestatus/servicestable.h" #include "icinga/service.h" #include "base/dynamictype.h" #include "base/objectlock.h" @@ -47,7 +48,7 @@ void DowntimesTable::AddColumns(Table *table, const String& prefix, table->AddColumn(prefix + "duration", Column(&DowntimesTable::DurationAccessor, objectAccessor)); table->AddColumn(prefix + "triggered_by", Column(&DowntimesTable::TriggeredByAccessor, objectAccessor)); - // TODO: Join services table. + ServicesTable::AddColumns(table, "service_", &DowntimesTable::ServiceAccessor); } String DowntimesTable::GetName(void) const @@ -66,10 +67,6 @@ void DowntimesTable::FetchRows(const AddRowFunction& addRowFn) ObjectLock olock(downtimes); - /*Value downtime; - BOOST_FOREACH(boost::tie(boost::tuples::ignore, downtime), downtimes) { - addRowFn(downtime); - }*/ String id; BOOST_FOREACH(boost::tie(id, boost::tuples::ignore), downtimes) { addRowFn(id); @@ -77,6 +74,11 @@ void DowntimesTable::FetchRows(const AddRowFunction& addRowFn) } } +Object::Ptr DowntimesTable::ServiceAccessor(const Value& row) +{ + return Service::GetOwnerByDowntimeID(row); +} + Value DowntimesTable::AuthorAccessor(const Value& row) { Dictionary::Ptr downtime = Service::GetDowntimeByID(row); @@ -142,18 +144,14 @@ Value DowntimesTable::FixedAccessor(const Value& row) Value DowntimesTable::DurationAccessor(const Value& row) { - /* Dictionary::Ptr downtime = Service::GetDowntimeByID(row); return downtime->Get("duration"); - */ } Value DowntimesTable::TriggeredByAccessor(const Value& row) { - /* Dictionary::Ptr downtime = Service::GetDowntimeByID(row); return downtime->Get("triggered_by"); - */ } diff --git a/components/livestatus/downtimestable.h b/components/livestatus/downtimestable.h index f9a7742d4..2e08bdca9 100644 --- a/components/livestatus/downtimestable.h +++ b/components/livestatus/downtimestable.h @@ -45,6 +45,9 @@ public: protected: virtual void FetchRows(const AddRowFunction& addRowFn); +private: + static Object::Ptr ServiceAccessor(const Value& row); + static Value AuthorAccessor(const Value& row); static Value CommentAccessor(const Value& row); static Value IdAccessor(const Value& row); diff --git a/components/livestatus/filter.h b/components/livestatus/filter.h index 1038da9cc..0eb16d3a6 100644 --- a/components/livestatus/filter.h +++ b/components/livestatus/filter.h @@ -33,7 +33,7 @@ class Filter : public Object public: DECLARE_PTR_TYPEDEFS(Filter); - virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object) = 0; + virtual bool Apply(const Table::Ptr& table, const Value& row) = 0; protected: Filter(void); diff --git a/components/livestatus/negatefilter.cpp b/components/livestatus/negatefilter.cpp index b6dedcff0..0883e7aae 100644 --- a/components/livestatus/negatefilter.cpp +++ b/components/livestatus/negatefilter.cpp @@ -26,7 +26,7 @@ NegateFilter::NegateFilter(const Filter::Ptr& inner) : m_Inner(inner) { } -bool NegateFilter::Apply(const Table::Ptr& table, const Object::Ptr& object) +bool NegateFilter::Apply(const Table::Ptr& table, const Value& row) { - return !m_Inner->Apply(table, object); + return !m_Inner->Apply(table, row); } diff --git a/components/livestatus/negatefilter.h b/components/livestatus/negatefilter.h index 3461c0f8b..215142c7f 100644 --- a/components/livestatus/negatefilter.h +++ b/components/livestatus/negatefilter.h @@ -37,7 +37,7 @@ public: NegateFilter(const Filter::Ptr& inner); - virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object); + virtual bool Apply(const Table::Ptr& table, const Value& row); private: Filter::Ptr m_Inner; diff --git a/components/livestatus/orfilter.cpp b/components/livestatus/orfilter.cpp index 601665562..b67f4c2eb 100644 --- a/components/livestatus/orfilter.cpp +++ b/components/livestatus/orfilter.cpp @@ -26,13 +26,13 @@ using namespace livestatus; OrFilter::OrFilter(void) { } -bool OrFilter::Apply(const Table::Ptr& table, const Object::Ptr& object) +bool OrFilter::Apply(const Table::Ptr& table, const Value& row) { if (m_Filters.empty()) return true; BOOST_FOREACH(const Filter::Ptr& filter, m_Filters) { - if (filter->Apply(table, object)) + if (filter->Apply(table, row)) return true; } diff --git a/components/livestatus/orfilter.h b/components/livestatus/orfilter.h index 207207575..ea1654179 100644 --- a/components/livestatus/orfilter.h +++ b/components/livestatus/orfilter.h @@ -37,7 +37,7 @@ public: OrFilter(void); - virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object); + virtual bool Apply(const Table::Ptr& table, const Value& row); }; } diff --git a/components/livestatus/query.cpp b/components/livestatus/query.cpp index 9e17b70ac..879360158 100644 --- a/components/livestatus/query.cpp +++ b/components/livestatus/query.cpp @@ -27,6 +27,7 @@ #include "base/convert.h" #include "base/objectlock.h" #include "base/logger_fwd.h" +#include "base/exception.h" #include #include #include @@ -220,7 +221,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream) return; } - std::vector objects = table->FilterRows(m_Filter); + std::vector objects = table->FilterRows(m_Filter); std::vector columns; if (m_Columns.size() > 0) @@ -231,7 +232,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream) Array::Ptr rs = boost::make_shared(); if (m_Stats.empty()) { - BOOST_FOREACH(const Object::Ptr& object, objects) { + BOOST_FOREACH(const Value& object, objects) { Array::Ptr row = boost::make_shared(); BOOST_FOREACH(const String& columnName, columns) { @@ -245,7 +246,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream) } else { std::vector stats(m_Stats.size(), 0); - BOOST_FOREACH(const Object::Ptr& object, objects) { + BOOST_FOREACH(const Value& object, objects) { int index = 0; BOOST_FOREACH(const Filter::Ptr filter, m_Stats) { if (filter->Apply(table, object)) @@ -305,17 +306,21 @@ void Query::PrintFixed16(const Stream::Ptr& stream, int code, const String& data bool Query::Execute(const Stream::Ptr& stream) { try { - Log(LogInformation, "livestatus", "Executing livestatus query: " + m_Verb); - - if (m_Verb == "GET") - ExecuteGetHelper(stream); - else if (m_Verb == "COMMAND") - ExecuteCommandHelper(stream); - else if (m_Verb == "ERROR") - ExecuteErrorHelper(stream); - else - BOOST_THROW_EXCEPTION(std::runtime_error("Invalid livestatus query verb.")); + Log(LogInformation, "livestatus", "Executing livestatus query: " + m_Verb); + + if (m_Verb == "GET") + ExecuteGetHelper(stream); + else if (m_Verb == "COMMAND") + ExecuteCommandHelper(stream); + else if (m_Verb == "ERROR") + ExecuteErrorHelper(stream); + else + BOOST_THROW_EXCEPTION(std::runtime_error("Invalid livestatus query verb.")); } catch (const std::exception& ex) { + StackTrace *st = Exception::GetLastStackTrace(); + std::ostringstream info; + st->Print(info); + Log(LogWarning, "livestatus", info.str()); SendResponse(stream, 452, boost::diagnostic_information(ex)); } diff --git a/components/livestatus/servicestable.cpp b/components/livestatus/servicestable.cpp index 79220fb8b..69bd51978 100644 --- a/components/livestatus/servicestable.cpp +++ b/components/livestatus/servicestable.cpp @@ -119,7 +119,7 @@ void ServicesTable::AddColumns(Table *table, const String& prefix, table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor)); table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor)); - HostsTable::AddColumns(table, "host_", &ServicesTable::HostAccessor); + HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor)); } String ServicesTable::GetName(void) const @@ -134,9 +134,9 @@ void ServicesTable::FetchRows(const AddRowFunction& addRowFn) } } -Object::Ptr ServicesTable::HostAccessor(const Value& row) +Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor) { - return static_cast(row)->GetHost(); + return static_cast(parentObjectAccessor(row))->GetHost(); } Value ServicesTable::ShortNameAccessor(const Value& row) diff --git a/components/livestatus/servicestable.h b/components/livestatus/servicestable.h index 05686e98c..53f6135a7 100644 --- a/components/livestatus/servicestable.h +++ b/components/livestatus/servicestable.h @@ -45,7 +45,7 @@ public: protected: virtual void FetchRows(const AddRowFunction& addRowFn); - static Object::Ptr HostAccessor(const Value& row); + static Object::Ptr HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor); static Value ShortNameAccessor(const Value& row); static Value DisplayNameAccessor(const Value& row); diff --git a/components/livestatus/table.cpp b/components/livestatus/table.cpp index 3d0d45e0a..97da8ef25 100644 --- a/components/livestatus/table.cpp +++ b/components/livestatus/table.cpp @@ -101,19 +101,19 @@ std::vector Table::GetColumnNames(void) const return names; } -std::vector Table::FilterRows(const Filter::Ptr& filter) +std::vector Table::FilterRows(const Filter::Ptr& filter) { - std::vector rs; + std::vector rs; FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1)); return rs; } -void Table::FilteredAddRow(std::vector& rs, const Filter::Ptr& filter, const Object::Ptr& object) +void Table::FilteredAddRow(std::vector& rs, const Filter::Ptr& filter, const Value& row) { - if (!filter || filter->Apply(GetSelf(), object)) - rs.push_back(object); + if (!filter || filter->Apply(GetSelf(), row)) + rs.push_back(row); } Value Table::ZeroAccessor(const Object::Ptr&) diff --git a/components/livestatus/table.h b/components/livestatus/table.h index e3abee6eb..cada20eaf 100644 --- a/components/livestatus/table.h +++ b/components/livestatus/table.h @@ -45,7 +45,7 @@ public: virtual String GetName(void) const = 0; - std::vector FilterRows(const shared_ptr& filter); + std::vector FilterRows(const shared_ptr& filter); void AddColumn(const String& name, const Column& column); Column GetColumn(const String& name) const; @@ -65,7 +65,7 @@ protected: private: std::map m_Columns; - void FilteredAddRow(std::vector& rs, const shared_ptr& filter, const Object::Ptr& object); + void FilteredAddRow(std::vector& rs, const shared_ptr& filter, const Value& row); }; } -- 2.40.0