From b81035e35255f2731118f497857fd9f751bacdfa Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 5 Dec 2014 16:31:42 +0100 Subject: [PATCH] Livestatus: Fix missing host downtimes/comments fixes #7064 --- doc/9-appendix.md | 8 ++-- lib/livestatus/commentstable.cpp | 31 +++++++++++++-- lib/livestatus/commentstable.hpp | 1 + lib/livestatus/downtimestable.cpp | 38 ++++++++++++++++++- lib/livestatus/downtimestable.hpp | 1 + test/livestatus/queries/comment/comment_short | 4 ++ .../queries/downtime/downtime_short | 4 ++ 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 test/livestatus/queries/comment/comment_short create mode 100644 test/livestatus/queries/downtime/downtime_short diff --git a/doc/9-appendix.md b/doc/9-appendix.md index 30765d928..beaea1680 100644 --- a/doc/9-appendix.md +++ b/doc/9-appendix.md @@ -571,7 +571,8 @@ Not supported: `neb_callbacks`, `neb_callbacks_rate`, `requests`, `requests_rate entry_type | int | . expires | int | . expire_time | string | Seconds. - service_ | join | Prefix for attributes from implicit join with services table (and implicit host table). + service_ | join | Prefix for attributes from implicit join with services table. + host_ | join | Prefix for attributes from implicit join with hosts table. #### Livestatus Downtimes Table Attributes @@ -582,7 +583,7 @@ Not supported: `neb_callbacks`, `neb_callbacks_rate`, `requests`, `requests_rate comment | string | . id | int | legacy_id. entry_time | string | Seconds. - type | int | 1=host, 2=service. + type | int | 1=active, 0=pending. is_service | int | . start_time | string | Seconds. end_time | string | Seconds. @@ -591,7 +592,8 @@ Not supported: `neb_callbacks`, `neb_callbacks_rate`, `requests`, `requests_rate triggered_by | int | legacy_id. triggers | int | NEW in Icinga 2. trigger_time | string | NEW in Icinga 2. - service_ | join | Prefix for attributes from implicit join with services table (and implicit host table). + service_ | join | Prefix for attributes from implicit join with services table. + host_ | join | Prefix for attributes from implicit join with hosts table. #### Livestatus Timeperiod Table Attributes diff --git a/lib/livestatus/commentstable.cpp b/lib/livestatus/commentstable.cpp index 71cf399f5..bf2910bda 100644 --- a/lib/livestatus/commentstable.cpp +++ b/lib/livestatus/commentstable.cpp @@ -18,6 +18,7 @@ ******************************************************************************/ #include "livestatus/commentstable.hpp" +#include "livestatus/hoststable.hpp" #include "livestatus/servicestable.hpp" #include "icinga/service.hpp" #include "base/dynamictype.hpp" @@ -47,7 +48,9 @@ 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)); + /* order is important - host w/o services must not be empty */ ServicesTable::AddColumns(table, "service_", boost::bind(&CommentsTable::ServiceAccessor, _1, objectAccessor)); + HostsTable::AddColumns(table, "host_", boost::bind(&CommentsTable::HostAccessor, _1, objectAccessor)); } String CommentsTable::GetName(void) const @@ -70,7 +73,8 @@ void CommentsTable::FetchRows(const AddRowFunction& addRowFn) String id; Comment::Ptr comment; BOOST_FOREACH(tie(id, comment), comments) { - addRowFn(comment); + if (Host::GetOwnerByCommentID(id) == host) + addRowFn(comment); } } @@ -82,15 +86,36 @@ void CommentsTable::FetchRows(const AddRowFunction& addRowFn) String id; Comment::Ptr comment; BOOST_FOREACH(tie(id, comment), comments) { - addRowFn(comment); + if (Service::GetOwnerByCommentID(id) == service) + addRowFn(comment); } } } +Object::Ptr CommentsTable::HostAccessor(const Value& row, const Column::ObjectAccessor&) +{ + Comment::Ptr comment = static_cast(row); + + Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId()); + + Host::Ptr host; + Service::Ptr service; + tie(host, service) = GetHostService(checkable); + + return host; +} + Object::Ptr CommentsTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&) { Comment::Ptr comment = static_cast(row); - return Checkable::GetOwnerByCommentID(comment->GetId()); // XXX: this might return a Host object + + Checkable::Ptr checkable = Checkable::GetOwnerByCommentID(comment->GetId()); + + Host::Ptr host; + Service::Ptr service; + tie(host, service) = GetHostService(checkable); + + return service; } Value CommentsTable::AuthorAccessor(const Value& row) diff --git a/lib/livestatus/commentstable.hpp b/lib/livestatus/commentstable.hpp index 0918c59f9..b6fb6ca4c 100644 --- a/lib/livestatus/commentstable.hpp +++ b/lib/livestatus/commentstable.hpp @@ -47,6 +47,7 @@ protected: virtual void FetchRows(const AddRowFunction& addRowFn); private: + static Object::Ptr HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor); static Object::Ptr ServiceAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor); static Value AuthorAccessor(const Value& row); diff --git a/lib/livestatus/downtimestable.cpp b/lib/livestatus/downtimestable.cpp index 829261c49..8d9521eba 100644 --- a/lib/livestatus/downtimestable.cpp +++ b/lib/livestatus/downtimestable.cpp @@ -18,6 +18,7 @@ ******************************************************************************/ #include "livestatus/downtimestable.hpp" +#include "livestatus/hoststable.hpp" #include "livestatus/servicestable.hpp" #include "icinga/service.hpp" #include "base/dynamictype.hpp" @@ -47,7 +48,9 @@ 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)); + /* order is important - host w/o services must not be empty */ ServicesTable::AddColumns(table, "service_", boost::bind(&DowntimesTable::ServiceAccessor, _1, objectAccessor)); + HostsTable::AddColumns(table, "host_", boost::bind(&DowntimesTable::HostAccessor, _1, objectAccessor)); } String DowntimesTable::GetName(void) const @@ -62,6 +65,19 @@ String DowntimesTable::GetPrefix(void) const void DowntimesTable::FetchRows(const AddRowFunction& addRowFn) { + BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType()) { + Dictionary::Ptr downtimes = host->GetDowntimes(); + + ObjectLock olock(downtimes); + + String id; + Downtime::Ptr downtime; + BOOST_FOREACH(boost::tie(id, downtime), downtimes) { + if (Host::GetOwnerByDowntimeID(id) == host) + addRowFn(downtime); + } + } + BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType()) { Dictionary::Ptr downtimes = service->GetDowntimes(); @@ -76,10 +92,30 @@ void DowntimesTable::FetchRows(const AddRowFunction& addRowFn) } } +Object::Ptr DowntimesTable::HostAccessor(const Value& row, const Column::ObjectAccessor&) +{ + Downtime::Ptr downtime = static_cast(row); + + Checkable::Ptr checkable = Checkable::GetOwnerByDowntimeID(downtime->GetId()); + + Host::Ptr host; + Service::Ptr service; + tie(host, service) = GetHostService(checkable); + + return host; +} + Object::Ptr DowntimesTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&) { Downtime::Ptr downtime = static_cast(row); - return Service::GetOwnerByDowntimeID(downtime->GetId()); + + Checkable::Ptr checkable = Checkable::GetOwnerByDowntimeID(downtime->GetId()); + + Host::Ptr host; + Service::Ptr service; + tie(host, service) = GetHostService(checkable); + + return service; } Value DowntimesTable::AuthorAccessor(const Value& row) diff --git a/lib/livestatus/downtimestable.hpp b/lib/livestatus/downtimestable.hpp index 29435346d..e92db4eac 100644 --- a/lib/livestatus/downtimestable.hpp +++ b/lib/livestatus/downtimestable.hpp @@ -47,6 +47,7 @@ protected: virtual void FetchRows(const AddRowFunction& addRowFn); private: + static Object::Ptr HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor); static Object::Ptr ServiceAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor); static Value AuthorAccessor(const Value& row); diff --git a/test/livestatus/queries/comment/comment_short b/test/livestatus/queries/comment/comment_short new file mode 100644 index 000000000..e5c0072e0 --- /dev/null +++ b/test/livestatus/queries/comment/comment_short @@ -0,0 +1,4 @@ +GET comments +Columns: id type is_service host_name service_description author comment +ResponseHeader: fixed16 + diff --git a/test/livestatus/queries/downtime/downtime_short b/test/livestatus/queries/downtime/downtime_short new file mode 100644 index 000000000..c2e1cf5f9 --- /dev/null +++ b/test/livestatus/queries/downtime/downtime_short @@ -0,0 +1,4 @@ +GET downtimes +Columns: id type is_service host_name service_description author comment start_time end_time +ResponseHeader: fixed16 +