]> granicus.if.org Git - icinga2/commitdiff
livestatus: more fixes on joins, ptrs
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 10 Jul 2013 14:11:40 +0000 (16:11 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 10 Jul 2013 14:11:40 +0000 (16:11 +0200)
refs #4372

20 files changed:
components/livestatus/andfilter.cpp
components/livestatus/andfilter.h
components/livestatus/attributefilter.cpp
components/livestatus/attributefilter.h
components/livestatus/column.cpp
components/livestatus/column.h
components/livestatus/commentstable.cpp
components/livestatus/commentstable.h
components/livestatus/downtimestable.cpp
components/livestatus/downtimestable.h
components/livestatus/filter.h
components/livestatus/negatefilter.cpp
components/livestatus/negatefilter.h
components/livestatus/orfilter.cpp
components/livestatus/orfilter.h
components/livestatus/query.cpp
components/livestatus/servicestable.cpp
components/livestatus/servicestable.h
components/livestatus/table.cpp
components/livestatus/table.h

index 73ab3892710e8af6d4688c3ae58f580c61e7d54d..2fb8eb6b3f68a715748960666aa10551c0b34caf 100644 (file)
@@ -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;
        }
 
index aafe5bf9871c6efc0d249fb73a05c387c5408c21..8cee8df4bafe213722a6f675df49065e4cf4b823 100644 (file)
@@ -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);
 };
 
 }
index c02dc93d830906c423cf1177ef3cd474e8dc4ab9..27e13af7cba76abf2f34ab4f271104a52c9360cc 100644 (file)
@@ -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<Array>()) {
                if (m_Operator == ">=") {
index 939f030d61439c18c113cf26abc42eba62b860fb..faee695d496157d5f8bed802f80d9a3ac5cb13ed 100644 (file)
@@ -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;
index 2ab9e7fceee95f7ffef2693749dec5a0356dbfcc..bef4b51d94dcc5a6091838b922d5870c6d293379 100644 (file)
@@ -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);
 }
index e259a62344c7c2b23527e7b87945372bdd81bfba..52d5f2b73028080b88ea9ceee82473fbe001a3e5 100644 (file)
@@ -31,12 +31,12 @@ namespace livestatus
 class Column
 {
 public:
-       typedef boost::function<Value (const Object::Ptr&)> ValueAccessor;
-       typedef boost::function<Object::Ptr (const Object::Ptr&)> ObjectAccessor;
+       typedef boost::function<Value (const Value&)> ValueAccessor;
+       typedef boost::function<Value (const Value&)> 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;
index fa9bff2e3a0dfb69891588bc12bb7cb0d0ff77f2..05c077bf95c5898b59099687be62153e0993eb71 100644 (file)
@@ -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");
 }
index 8663ed306328f6bda05121672ae943e7fabcfe75..8d7ea186392d8502eb8d675060adc0bbee43d848 100644 (file)
@@ -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);
index 6b64742941f1d829c072ad88f5ee708f6104d684..f8e6650c9b789427bf634ccdfc82b34c2af35e8d 100644 (file)
@@ -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");
-       */
 }
index f9a7742d4654c00ad7842e988c4b21c83a6d6539..2e08bdca9d9863b2ef8f7f31a4a1e6f76bba5777 100644 (file)
@@ -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);
index 1038da9cc04a58496cda658ca3e9927bcdb7f99b..0eb16d3a61df2300cacc6dd622d7c0d9d22209e1 100644 (file)
@@ -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);
index b6dedcff04394c79a10ca348782f0830735955c0..0883e7aae856e884cda3221ab25381a34550450a 100644 (file)
@@ -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);
 }
index 3461c0f8bf8915c186ba51abe284722826b1c07c..215142c7f4561b683dec5dc1f359b30b67cb6412 100644 (file)
@@ -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;
index 60166556243c91b726b313bdf0b2ad720a08ba7e..b67f4c2ebb42e424281cff5daba7c93bef9abbb2 100644 (file)
@@ -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;
        }
 
index 207207575cffd726c0af49d40b896c0c2af9dbd8..ea1654179a198d5a86dd1799bcf0b3fe5804f861 100644 (file)
@@ -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);
 };
 
 }
index 9e17b70acc8fb82c88ea2970b06685cf1b7138fe..8793601582c6991bc9753a21592a08b368942169 100644 (file)
@@ -27,6 +27,7 @@
 #include "base/convert.h"
 #include "base/objectlock.h"
 #include "base/logger_fwd.h"
+#include "base/exception.h"
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/smart_ptr/make_shared.hpp>
 #include <boost/foreach.hpp>
@@ -220,7 +221,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
                return;
        }
 
-       std::vector<Object::Ptr> objects = table->FilterRows(m_Filter);
+       std::vector<Value> objects = table->FilterRows(m_Filter);
        std::vector<String> columns;
        
        if (m_Columns.size() > 0)
@@ -231,7 +232,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
        Array::Ptr rs = boost::make_shared<Array>();
 
        if (m_Stats.empty()) {
-               BOOST_FOREACH(const Object::Ptr& object, objects) {
+               BOOST_FOREACH(const Value& object, objects) {
                        Array::Ptr row = boost::make_shared<Array>();
 
                        BOOST_FOREACH(const String& columnName, columns) {
@@ -245,7 +246,7 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
        } else {
                std::vector<int> 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));
        }
 
index 79220fb8b02e5771fe92cf2d54712672bb254558..69bd519788b32d1d4b5465dfcce1a8f5884d9aa3 100644 (file)
@@ -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<Service::Ptr>(row)->GetHost();
+       return static_cast<Service::Ptr>(parentObjectAccessor(row))->GetHost();
 }
 
 Value ServicesTable::ShortNameAccessor(const Value& row)
index 05686e98cddc1d9da9fb8479c71ef91e74290c3c..53f6135a7b316842f977551f63d84afe00e04201 100644 (file)
@@ -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);
index 3d0d45e0affa7c8f7ac1d7d7380c1d577bc91023..97da8ef25a51c337f35dbb8c22ff27598f761100 100644 (file)
@@ -101,19 +101,19 @@ std::vector<String> Table::GetColumnNames(void) const
        return names;
 }
 
-std::vector<Object::Ptr> Table::FilterRows(const Filter::Ptr& filter)
+std::vector<Value> Table::FilterRows(const Filter::Ptr& filter)
 {
-       std::vector<Object::Ptr> rs;
+       std::vector<Value> rs;
 
        FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1));
 
        return rs;
 }
 
-void Table::FilteredAddRow(std::vector<Object::Ptr>& rs, const Filter::Ptr& filter, const Object::Ptr& object)
+void Table::FilteredAddRow(std::vector<Value>& 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&)
index e3abee6eb149e27ee9101b8cd3bbf8066e50e69b..cada20eaf66c1739264a822a87fe010a54acc0a6 100644 (file)
@@ -45,7 +45,7 @@ public:
 
        virtual String GetName(void) const = 0;
 
-       std::vector<Object::Ptr> FilterRows(const shared_ptr<Filter>& filter);
+       std::vector<Value> FilterRows(const shared_ptr<Filter>& filter);
 
        void AddColumn(const String& name, const Column& column);
        Column GetColumn(const String& name) const;
@@ -65,7 +65,7 @@ protected:
 private:
        std::map<String, Column> m_Columns;
 
-       void FilteredAddRow(std::vector<Object::Ptr>& rs, const shared_ptr<Filter>& filter, const Object::Ptr& object);
+       void FilteredAddRow(std::vector<Value>& rs, const shared_ptr<Filter>& filter, const Value& row);
 };
 
 }