1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/servicegroupstable.hpp"
4 #include "icinga/servicegroup.hpp"
5 #include "base/configtype.hpp"
7 using namespace icinga;
9 ServiceGroupsTable::ServiceGroupsTable()
14 void ServiceGroupsTable::AddColumns(Table *table, const String& prefix,
15 const Column::ObjectAccessor& objectAccessor)
17 table->AddColumn(prefix + "name", Column(&ServiceGroupsTable::NameAccessor, objectAccessor));
18 table->AddColumn(prefix + "alias", Column(&ServiceGroupsTable::AliasAccessor, objectAccessor));
19 table->AddColumn(prefix + "notes", Column(&ServiceGroupsTable::NotesAccessor, objectAccessor));
20 table->AddColumn(prefix + "notes_url", Column(&ServiceGroupsTable::NotesUrlAccessor, objectAccessor));
21 table->AddColumn(prefix + "action_url", Column(&ServiceGroupsTable::ActionUrlAccessor, objectAccessor));
22 table->AddColumn(prefix + "members", Column(&ServiceGroupsTable::MembersAccessor, objectAccessor));
23 table->AddColumn(prefix + "members_with_state", Column(&ServiceGroupsTable::MembersWithStateAccessor, objectAccessor));
24 table->AddColumn(prefix + "worst_service_state", Column(&ServiceGroupsTable::WorstServiceStateAccessor, objectAccessor));
25 table->AddColumn(prefix + "num_services", Column(&ServiceGroupsTable::NumServicesAccessor, objectAccessor));
26 table->AddColumn(prefix + "num_services_ok", Column(&ServiceGroupsTable::NumServicesOkAccessor, objectAccessor));
27 table->AddColumn(prefix + "num_services_warn", Column(&ServiceGroupsTable::NumServicesWarnAccessor, objectAccessor));
28 table->AddColumn(prefix + "num_services_crit", Column(&ServiceGroupsTable::NumServicesCritAccessor, objectAccessor));
29 table->AddColumn(prefix + "num_services_unknown", Column(&ServiceGroupsTable::NumServicesUnknownAccessor, objectAccessor));
30 table->AddColumn(prefix + "num_services_pending", Column(&ServiceGroupsTable::NumServicesPendingAccessor, objectAccessor));
31 table->AddColumn(prefix + "num_services_hard_ok", Column(&ServiceGroupsTable::NumServicesHardOkAccessor, objectAccessor));
32 table->AddColumn(prefix + "num_services_hard_warn", Column(&ServiceGroupsTable::NumServicesHardWarnAccessor, objectAccessor));
33 table->AddColumn(prefix + "num_services_hard_crit", Column(&ServiceGroupsTable::NumServicesHardCritAccessor, objectAccessor));
34 table->AddColumn(prefix + "num_services_hard_unknown", Column(&ServiceGroupsTable::NumServicesHardUnknownAccessor, objectAccessor));
37 String ServiceGroupsTable::GetName() const
39 return "servicegroups";
42 String ServiceGroupsTable::GetPrefix() const
44 return "servicegroup";
47 void ServiceGroupsTable::FetchRows(const AddRowFunction& addRowFn)
49 for (const ServiceGroup::Ptr& sg : ConfigType::GetObjectsByType<ServiceGroup>()) {
50 if (!addRowFn(sg, LivestatusGroupByNone, Empty))
55 Value ServiceGroupsTable::NameAccessor(const Value& row)
57 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
65 Value ServiceGroupsTable::AliasAccessor(const Value& row)
67 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
72 return sg->GetDisplayName();
75 Value ServiceGroupsTable::NotesAccessor(const Value& row)
77 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
82 return sg->GetNotes();
85 Value ServiceGroupsTable::NotesUrlAccessor(const Value& row)
87 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
92 return sg->GetNotesUrl();
95 Value ServiceGroupsTable::ActionUrlAccessor(const Value& row)
97 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
102 return sg->GetActionUrl();
105 Value ServiceGroupsTable::MembersAccessor(const Value& row)
107 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
114 for (const Service::Ptr& service : sg->GetMembers()) {
115 result.push_back(new Array({
116 service->GetHost()->GetName(),
117 service->GetShortName()
121 return new Array(std::move(result));
124 Value ServiceGroupsTable::MembersWithStateAccessor(const Value& row)
126 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
133 for (const Service::Ptr& service : sg->GetMembers()) {
134 result.push_back(new Array({
135 service->GetHost()->GetName(),
136 service->GetShortName(),
137 service->GetHost()->GetState(),
142 return new Array(std::move(result));
145 Value ServiceGroupsTable::WorstServiceStateAccessor(const Value& row)
147 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
152 Value worst_service = ServiceOK;
154 for (const Service::Ptr& service : sg->GetMembers()) {
155 if (service->GetState() > worst_service)
156 worst_service = service->GetState();
159 return worst_service;
162 Value ServiceGroupsTable::NumServicesAccessor(const Value& row)
164 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
169 return sg->GetMembers().size();
172 Value ServiceGroupsTable::NumServicesOkAccessor(const Value& row)
174 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
179 int num_services = 0;
181 for (const Service::Ptr& service : sg->GetMembers()) {
182 if (service->GetState() == ServiceOK)
189 Value ServiceGroupsTable::NumServicesWarnAccessor(const Value& row)
191 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
196 int num_services = 0;
198 for (const Service::Ptr& service : sg->GetMembers()) {
199 if (service->GetState() == ServiceWarning)
206 Value ServiceGroupsTable::NumServicesCritAccessor(const Value& row)
208 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
213 int num_services = 0;
215 for (const Service::Ptr& service : sg->GetMembers()) {
216 if (service->GetState() == ServiceCritical)
223 Value ServiceGroupsTable::NumServicesUnknownAccessor(const Value& row)
225 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
230 int num_services = 0;
232 for (const Service::Ptr& service : sg->GetMembers()) {
233 if (service->GetState() == ServiceUnknown)
240 Value ServiceGroupsTable::NumServicesPendingAccessor(const Value& row)
242 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
247 int num_services = 0;
249 for (const Service::Ptr& service : sg->GetMembers()) {
250 if (!service->GetLastCheckResult())
257 Value ServiceGroupsTable::NumServicesHardOkAccessor(const Value& row)
259 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
264 int num_services = 0;
266 for (const Service::Ptr& service : sg->GetMembers()) {
267 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
274 Value ServiceGroupsTable::NumServicesHardWarnAccessor(const Value& row)
276 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
281 int num_services = 0;
283 for (const Service::Ptr& service : sg->GetMembers()) {
284 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
291 Value ServiceGroupsTable::NumServicesHardCritAccessor(const Value& row)
293 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
298 int num_services = 0;
300 for (const Service::Ptr& service : sg->GetMembers()) {
301 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
308 Value ServiceGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
310 ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
315 int num_services = 0;
317 for (const Service::Ptr& service : sg->GetMembers()) {
318 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)