diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:32:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:32:39 +0000 |
commit | 56ae875861ab260b80a030f50c4aff9f9dc8fff0 (patch) | |
tree | 531412110fc901a5918c7f7442202804a83cada9 /lib/livestatus/servicegroupstable.cpp | |
parent | Initial commit. (diff) | |
download | icinga2-56ae875861ab260b80a030f50c4aff9f9dc8fff0.tar.xz icinga2-56ae875861ab260b80a030f50c4aff9f9dc8fff0.zip |
Adding upstream version 2.14.2.upstream/2.14.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | lib/livestatus/servicegroupstable.cpp | 323 |
1 files changed, 323 insertions, 0 deletions
diff --git a/lib/livestatus/servicegroupstable.cpp b/lib/livestatus/servicegroupstable.cpp new file mode 100644 index 0000000..38d6d05 --- /dev/null +++ b/lib/livestatus/servicegroupstable.cpp @@ -0,0 +1,323 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#include "livestatus/servicegroupstable.hpp" +#include "icinga/servicegroup.hpp" +#include "base/configtype.hpp" + +using namespace icinga; + +ServiceGroupsTable::ServiceGroupsTable() +{ + AddColumns(this); +} + +void ServiceGroupsTable::AddColumns(Table *table, const String& prefix, + const Column::ObjectAccessor& objectAccessor) +{ + table->AddColumn(prefix + "name", Column(&ServiceGroupsTable::NameAccessor, objectAccessor)); + table->AddColumn(prefix + "alias", Column(&ServiceGroupsTable::AliasAccessor, objectAccessor)); + table->AddColumn(prefix + "notes", Column(&ServiceGroupsTable::NotesAccessor, objectAccessor)); + table->AddColumn(prefix + "notes_url", Column(&ServiceGroupsTable::NotesUrlAccessor, objectAccessor)); + table->AddColumn(prefix + "action_url", Column(&ServiceGroupsTable::ActionUrlAccessor, objectAccessor)); + table->AddColumn(prefix + "members", Column(&ServiceGroupsTable::MembersAccessor, objectAccessor)); + table->AddColumn(prefix + "members_with_state", Column(&ServiceGroupsTable::MembersWithStateAccessor, objectAccessor)); + table->AddColumn(prefix + "worst_service_state", Column(&ServiceGroupsTable::WorstServiceStateAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services", Column(&ServiceGroupsTable::NumServicesAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_ok", Column(&ServiceGroupsTable::NumServicesOkAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_warn", Column(&ServiceGroupsTable::NumServicesWarnAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_crit", Column(&ServiceGroupsTable::NumServicesCritAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_unknown", Column(&ServiceGroupsTable::NumServicesUnknownAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_pending", Column(&ServiceGroupsTable::NumServicesPendingAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_hard_ok", Column(&ServiceGroupsTable::NumServicesHardOkAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_hard_warn", Column(&ServiceGroupsTable::NumServicesHardWarnAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_hard_crit", Column(&ServiceGroupsTable::NumServicesHardCritAccessor, objectAccessor)); + table->AddColumn(prefix + "num_services_hard_unknown", Column(&ServiceGroupsTable::NumServicesHardUnknownAccessor, objectAccessor)); +} + +String ServiceGroupsTable::GetName() const +{ + return "servicegroups"; +} + +String ServiceGroupsTable::GetPrefix() const +{ + return "servicegroup"; +} + +void ServiceGroupsTable::FetchRows(const AddRowFunction& addRowFn) +{ + for (const ServiceGroup::Ptr& sg : ConfigType::GetObjectsByType<ServiceGroup>()) { + if (!addRowFn(sg, LivestatusGroupByNone, Empty)) + return; + } +} + +Value ServiceGroupsTable::NameAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + return sg->GetName(); +} + +Value ServiceGroupsTable::AliasAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + return sg->GetDisplayName(); +} + +Value ServiceGroupsTable::NotesAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + return sg->GetNotes(); +} + +Value ServiceGroupsTable::NotesUrlAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + return sg->GetNotesUrl(); +} + +Value ServiceGroupsTable::ActionUrlAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + return sg->GetActionUrl(); +} + +Value ServiceGroupsTable::MembersAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + ArrayData result; + + for (const Service::Ptr& service : sg->GetMembers()) { + result.push_back(new Array({ + service->GetHost()->GetName(), + service->GetShortName() + })); + } + + return new Array(std::move(result)); +} + +Value ServiceGroupsTable::MembersWithStateAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + ArrayData result; + + for (const Service::Ptr& service : sg->GetMembers()) { + result.push_back(new Array({ + service->GetHost()->GetName(), + service->GetShortName(), + service->GetHost()->GetState(), + service->GetState() + })); + } + + return new Array(std::move(result)); +} + +Value ServiceGroupsTable::WorstServiceStateAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + Value worst_service = ServiceOK; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetState() > worst_service) + worst_service = service->GetState(); + } + + return worst_service; +} + +Value ServiceGroupsTable::NumServicesAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + return sg->GetMembers().size(); +} + +Value ServiceGroupsTable::NumServicesOkAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetState() == ServiceOK) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesWarnAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetState() == ServiceWarning) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesCritAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetState() == ServiceCritical) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesUnknownAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetState() == ServiceUnknown) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesPendingAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (!service->GetLastCheckResult()) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesHardOkAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesHardWarnAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesHardCritAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical) + num_services++; + } + + return num_services; +} + +Value ServiceGroupsTable::NumServicesHardUnknownAccessor(const Value& row) +{ + ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row); + + if (!sg) + return Empty; + + int num_services = 0; + + for (const Service::Ptr& service : sg->GetMembers()) { + if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown) + num_services++; + } + + return num_services; +} |