]> granicus.if.org Git - icinga2/blobdiff - lib/livestatus/hostgroupstable.cpp
Remove more redundant wrappers from CompatUtility class
[icinga2] / lib / livestatus / hostgroupstable.cpp
index 6babcb210cffdd386163eeb03ab9ae80d4c78c8f..117a84a7d8626a72b6db05212f1190bdd4c20aaf 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
  *                                                                            *
  * This program is free software; you can redistribute it and/or              *
  * modify it under the terms of the GNU General Public License                *
 #include "icinga/hostgroup.hpp"
 #include "icinga/host.hpp"
 #include "icinga/service.hpp"
-#include "base/dynamictype.hpp"
-#include <boost/foreach.hpp>
+#include "base/configtype.hpp"
 
 using namespace icinga;
 
-HostGroupsTable::HostGroupsTable(void)
+HostGroupsTable::HostGroupsTable()
 {
        AddColumns(this);
 }
 
 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
-    const Column::ObjectAccessor& objectAccessor)
+       const Column::ObjectAccessor& objectAccessor)
 {
        table->AddColumn(prefix + "name", Column(&HostGroupsTable::NameAccessor, objectAccessor));
        table->AddColumn(prefix + "alias", Column(&HostGroupsTable::AliasAccessor, objectAccessor));
@@ -48,7 +47,7 @@ void HostGroupsTable::AddColumns(Table *table, const String& prefix,
        table->AddColumn(prefix + "num_hosts_down", Column(&HostGroupsTable::NumHostsDownAccessor, objectAccessor));
        table->AddColumn(prefix + "num_hosts_unreach", Column(&HostGroupsTable::NumHostsUnreachAccessor, objectAccessor));
        table->AddColumn(prefix + "num_services", Column(&HostGroupsTable::NumServicesAccessor, objectAccessor));
-       table->AddColumn(prefix + "worst_services_state", Column(&HostGroupsTable::WorstServicesStateAccessor, objectAccessor));
+       table->AddColumn(prefix + "worst_service_state", Column(&HostGroupsTable::WorstServiceStateAccessor, objectAccessor));
        table->AddColumn(prefix + "num_services_pending", Column(&HostGroupsTable::NumServicesPendingAccessor, objectAccessor));
        table->AddColumn(prefix + "num_services_ok", Column(&HostGroupsTable::NumServicesOkAccessor, objectAccessor));
        table->AddColumn(prefix + "num_services_warn", Column(&HostGroupsTable::NumServicesWarnAccessor, objectAccessor));
@@ -61,53 +60,84 @@ void HostGroupsTable::AddColumns(Table *table, const String& prefix,
        table->AddColumn(prefix + "num_services_hard_unknown", Column(&HostGroupsTable::NumServicesHardUnknownAccessor, objectAccessor));
 }
 
-String HostGroupsTable::GetName(void) const
+String HostGroupsTable::GetName() const
 {
        return "hostgroups";
 }
 
-String HostGroupsTable::GetPrefix(void) const
+String HostGroupsTable::GetPrefix() const
 {
        return "hostgroup";
 }
 
 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
 {
-       BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjectsByType<HostGroup>()) {
-               addRowFn(hg);
+       for (const HostGroup::Ptr& hg : ConfigType::GetObjectsByType<HostGroup>()) {
+               if (!addRowFn(hg, LivestatusGroupByNone, Empty))
+                       return;
        }
 }
 
 Value HostGroupsTable::NameAccessor(const Value& row)
 {
-       return static_cast<HostGroup::Ptr>(row)->GetName();
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
+       return hg->GetName();
 }
 
 Value HostGroupsTable::AliasAccessor(const Value& row)
 {
-       return static_cast<HostGroup::Ptr>(row)->GetDisplayName();
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
+       return hg->GetDisplayName();
 }
 
 Value HostGroupsTable::NotesAccessor(const Value& row)
 {
-       return static_cast<HostGroup::Ptr>(row)->GetNotes();
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
+       return hg->GetNotes();
 }
 
 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
 {
-       return static_cast<HostGroup::Ptr>(row)->GetNotesUrl();
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
+       return hg->GetNotesUrl();
 }
 
 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
 {
-       return static_cast<HostGroup::Ptr>(row)->GetActionUrl();
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
+       return hg->GetActionUrl();
 }
 
 Value HostGroupsTable::MembersAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        Array::Ptr members = new Array();
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (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<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        Array::Ptr members = new Array();
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (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<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int worst_host = HostUp;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (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<long>(static_cast<HostGroup::Ptr>(row)->GetMembers().size());
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
+       return hg->GetMembers().size();
 }
 
 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_hosts = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (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<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_hosts = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (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<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_hosts = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (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<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_hosts = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
                if (!host->IsReachable())
                        num_hosts++;
        }
@@ -196,25 +261,34 @@ Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesAccessor(const Value& row)
 {
-       int num_services = 0;
        HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
 
+       if (!hg)
+               return Empty;
+
+       int num_services = 0;
+
        if (hg->GetMembers().size() == 0)
                return 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
                num_services += host->GetServices().size();
        }
 
        return num_services;
 }
 
-Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
+Value HostGroupsTable::WorstServiceStateAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        Value worst_service = ServiceOK;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetState() > worst_service)
                                worst_service = service->GetState();
                }
@@ -225,10 +299,15 @@ Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (!service->GetLastCheckResult())
                                num_services++;
                }
@@ -239,10 +318,15 @@ Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetState() == ServiceOK)
                                num_services++;
                }
@@ -253,10 +337,15 @@ Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetState() == ServiceWarning)
                                num_services++;
                }
@@ -267,10 +356,15 @@ Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetState() == ServiceCritical)
                                num_services++;
                }
@@ -281,10 +375,15 @@ Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetState() == ServiceUnknown)
                                num_services++;
                }
@@ -295,10 +394,15 @@ Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
 
 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        Value worst_service = ServiceOK;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetStateType() == StateTypeHard) {
                                if (service->GetState() > worst_service)
                                        worst_service = service->GetState();
@@ -311,10 +415,15 @@ Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
                                num_services++;
                }
@@ -325,10 +434,15 @@ Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
                                num_services++;
                }
@@ -339,10 +453,15 @@ Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
                                num_services++;
                }
@@ -353,10 +472,15 @@ Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
 
 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
 {
+       HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
+
+       if (!hg)
+               return Empty;
+
        int num_services = 0;
 
-       BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
+       for (const Host::Ptr& host : hg->GetMembers()) {
+               for (const Service::Ptr& service : host->GetServices()) {
                        if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)
                                num_services++;
                }