From c3b365dc62dbe7888974b791619d11b9ec811aae Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Sun, 15 Feb 2015 16:48:09 +0100 Subject: [PATCH] Livestatus: Add sanity checks for empty values --- lib/livestatus/hostgroupstable.cpp | 174 ++++++++++++++++++++++---- lib/livestatus/servicegroupstable.cpp | 126 ++++++++++++++++--- 2 files changed, 257 insertions(+), 43 deletions(-) diff --git a/lib/livestatus/hostgroupstable.cpp b/lib/livestatus/hostgroupstable.cpp index 6babcb210..db46c5c21 100644 --- a/lib/livestatus/hostgroupstable.cpp +++ b/lib/livestatus/hostgroupstable.cpp @@ -80,34 +80,64 @@ void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn) Value HostGroupsTable::NameAccessor(const Value& row) { - return static_cast(row)->GetName(); + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + + return hg->GetName(); } Value HostGroupsTable::AliasAccessor(const Value& row) { - return static_cast(row)->GetDisplayName(); + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + + return hg->GetDisplayName(); } Value HostGroupsTable::NotesAccessor(const Value& row) { - return static_cast(row)->GetNotes(); + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + + return hg->GetNotes(); } Value HostGroupsTable::NotesUrlAccessor(const Value& row) { - return static_cast(row)->GetNotesUrl(); + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + + return hg->GetNotesUrl(); } Value HostGroupsTable::ActionUrlAccessor(const Value& row) { - return static_cast(row)->GetActionUrl(); + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + + return hg->GetActionUrl(); } Value HostGroupsTable::MembersAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + Array::Ptr members = new Array(); - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { members->Add(host->GetName()); } @@ -116,9 +146,14 @@ Value HostGroupsTable::MembersAccessor(const Value& row) Value HostGroupsTable::MembersWithStateAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + Array::Ptr members = new Array(); - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { Array::Ptr member_state = new Array(); member_state->Add(host->GetName()); member_state->Add(host->GetState()); @@ -130,9 +165,14 @@ Value HostGroupsTable::MembersWithStateAccessor(const Value& row) Value HostGroupsTable::WorstHostStateAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int worst_host = HostUp; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { if (host->GetState() > worst_host) worst_host = host->GetState(); } @@ -142,14 +182,24 @@ Value HostGroupsTable::WorstHostStateAccessor(const Value& row) Value HostGroupsTable::NumHostsAccessor(const Value& row) { - return static_cast(static_cast(row)->GetMembers().size()); + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + + return hg->GetMembers().size(); } Value HostGroupsTable::NumHostsPendingAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_hosts = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { /* no checkresult */ if (!host->GetLastCheckResult()) num_hosts++; @@ -160,9 +210,14 @@ Value HostGroupsTable::NumHostsPendingAccessor(const Value& row) Value HostGroupsTable::NumHostsUpAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_hosts = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { if (host->GetState() == HostUp) num_hosts++; } @@ -172,9 +227,14 @@ Value HostGroupsTable::NumHostsUpAccessor(const Value& row) Value HostGroupsTable::NumHostsDownAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_hosts = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { if (host->GetState() == HostDown) num_hosts++; } @@ -184,9 +244,14 @@ Value HostGroupsTable::NumHostsDownAccessor(const Value& row) Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_hosts = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { if (!host->IsReachable()) num_hosts++; } @@ -196,9 +261,13 @@ Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row) Value HostGroupsTable::NumServicesAccessor(const Value& row) { - int num_services = 0; HostGroup::Ptr hg = static_cast(row); + if (!hg) + return Empty; + + int num_services = 0; + if (hg->GetMembers().size() == 0) return 0; @@ -211,9 +280,14 @@ Value HostGroupsTable::NumServicesAccessor(const Value& row) Value HostGroupsTable::WorstServicesStateAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + Value worst_service = ServiceOK; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetState() > worst_service) worst_service = service->GetState(); @@ -225,9 +299,14 @@ Value HostGroupsTable::WorstServicesStateAccessor(const Value& row) Value HostGroupsTable::NumServicesPendingAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (!service->GetLastCheckResult()) num_services++; @@ -239,9 +318,14 @@ Value HostGroupsTable::NumServicesPendingAccessor(const Value& row) Value HostGroupsTable::NumServicesOkAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetState() == ServiceOK) num_services++; @@ -253,9 +337,14 @@ Value HostGroupsTable::NumServicesOkAccessor(const Value& row) Value HostGroupsTable::NumServicesWarnAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetState() == ServiceWarning) num_services++; @@ -267,9 +356,14 @@ Value HostGroupsTable::NumServicesWarnAccessor(const Value& row) Value HostGroupsTable::NumServicesCritAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetState() == ServiceCritical) num_services++; @@ -281,9 +375,14 @@ Value HostGroupsTable::NumServicesCritAccessor(const Value& row) Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetState() == ServiceUnknown) num_services++; @@ -295,9 +394,14 @@ Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row) Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + Value worst_service = ServiceOK; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetStateType() == StateTypeHard) { if (service->GetState() > worst_service) @@ -311,9 +415,14 @@ Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row) Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK) num_services++; @@ -325,9 +434,14 @@ Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row) Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning) num_services++; @@ -339,9 +453,14 @@ Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row) Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical) num_services++; @@ -353,9 +472,14 @@ Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row) Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row) { + HostGroup::Ptr hg = static_cast(row); + + if (!hg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Host::Ptr& host, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) { BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown) num_services++; diff --git a/lib/livestatus/servicegroupstable.cpp b/lib/livestatus/servicegroupstable.cpp index 374826f6e..0a3c8ee8e 100644 --- a/lib/livestatus/servicegroupstable.cpp +++ b/lib/livestatus/servicegroupstable.cpp @@ -71,34 +71,64 @@ void ServiceGroupsTable::FetchRows(const AddRowFunction& addRowFn) Value ServiceGroupsTable::NameAccessor(const Value& row) { - return static_cast(row)->GetName(); + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + + return sg->GetName(); } Value ServiceGroupsTable::AliasAccessor(const Value& row) { - return static_cast(row)->GetDisplayName(); + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + + return sg->GetDisplayName(); } Value ServiceGroupsTable::NotesAccessor(const Value& row) { - return static_cast(row)->GetNotes(); + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + + return sg->GetNotes(); } Value ServiceGroupsTable::NotesUrlAccessor(const Value& row) { - return static_cast(row)->GetNotesUrl(); + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + + return sg->GetNotesUrl(); } Value ServiceGroupsTable::ActionUrlAccessor(const Value& row) { - return static_cast(row)->GetActionUrl(); + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + + return sg->GetActionUrl(); } Value ServiceGroupsTable::MembersAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + Array::Ptr members = new Array(); - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { Array::Ptr host_svc = new Array(); host_svc->Add(service->GetHost()->GetName()); host_svc->Add(service->GetShortName()); @@ -110,9 +140,14 @@ Value ServiceGroupsTable::MembersAccessor(const Value& row) Value ServiceGroupsTable::MembersWithStateAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + Array::Ptr members = new Array(); - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { Array::Ptr host_svc = new Array(); host_svc->Add(service->GetHost()->GetName()); host_svc->Add(service->GetShortName()); @@ -126,9 +161,14 @@ Value ServiceGroupsTable::MembersWithStateAccessor(const Value& row) Value ServiceGroupsTable::WorstServiceStateAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + Value worst_service = ServiceOK; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetState() > worst_service) worst_service = service->GetState(); } @@ -138,14 +178,24 @@ Value ServiceGroupsTable::WorstServiceStateAccessor(const Value& row) Value ServiceGroupsTable::NumServicesAccessor(const Value& row) { - return static_cast(static_cast(row)->GetMembers().size()); + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + + return sg->GetMembers().size(); } Value ServiceGroupsTable::NumServicesOkAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetState() == ServiceOK) num_services++; } @@ -155,9 +205,14 @@ Value ServiceGroupsTable::NumServicesOkAccessor(const Value& row) Value ServiceGroupsTable::NumServicesWarnAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetState() == ServiceWarning) num_services++; } @@ -167,9 +222,14 @@ Value ServiceGroupsTable::NumServicesWarnAccessor(const Value& row) Value ServiceGroupsTable::NumServicesCritAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetState() == ServiceCritical) num_services++; } @@ -179,9 +239,14 @@ Value ServiceGroupsTable::NumServicesCritAccessor(const Value& row) Value ServiceGroupsTable::NumServicesUnknownAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetState() == ServiceUnknown) num_services++; } @@ -191,9 +256,14 @@ Value ServiceGroupsTable::NumServicesUnknownAccessor(const Value& row) Value ServiceGroupsTable::NumServicesPendingAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (!service->GetLastCheckResult()) num_services++; } @@ -203,9 +273,14 @@ Value ServiceGroupsTable::NumServicesPendingAccessor(const Value& row) Value ServiceGroupsTable::NumServicesHardOkAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK) num_services++; } @@ -215,9 +290,14 @@ Value ServiceGroupsTable::NumServicesHardOkAccessor(const Value& row) Value ServiceGroupsTable::NumServicesHardWarnAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning) num_services++; } @@ -227,9 +307,14 @@ Value ServiceGroupsTable::NumServicesHardWarnAccessor(const Value& row) Value ServiceGroupsTable::NumServicesHardCritAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical) num_services++; } @@ -239,9 +324,14 @@ Value ServiceGroupsTable::NumServicesHardCritAccessor(const Value& row) Value ServiceGroupsTable::NumServicesHardUnknownAccessor(const Value& row) { + ServiceGroup::Ptr sg = static_cast(row); + + if (!sg) + return Empty; + int num_services = 0; - BOOST_FOREACH(const Service::Ptr& service, static_cast(row)->GetMembers()) { + BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) { if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown) num_services++; } -- 2.40.0