]> granicus.if.org Git - icinga2/blob - lib/livestatus/zonestable.cpp
Merge pull request #6531 from Icinga/feature/zone-all_parents
[icinga2] / lib / livestatus / zonestable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "livestatus/zonestable.hpp"
21 #include "remote/zone.hpp"
22 #include "base/configtype.hpp"
23
24 using namespace icinga;
25
26 ZonesTable::ZonesTable()
27 {
28         AddColumns(this);
29 }
30
31 void ZonesTable::AddColumns(Table *table, const String& prefix,
32         const Column::ObjectAccessor& objectAccessor)
33 {
34         table->AddColumn(prefix + "name", Column(&ZonesTable::NameAccessor, objectAccessor));
35         table->AddColumn(prefix + "parent", Column(&ZonesTable::ParentAccessor, objectAccessor));
36         table->AddColumn(prefix + "endpoints", Column(&ZonesTable::EndpointsAccessor, objectAccessor));
37         table->AddColumn(prefix + "global", Column(&ZonesTable::GlobalAccessor, objectAccessor));
38 }
39
40 String ZonesTable::GetName() const
41 {
42         return "zones";
43 }
44
45 String ZonesTable::GetPrefix() const
46 {
47         return "zone";
48 }
49
50 void ZonesTable::FetchRows(const AddRowFunction& addRowFn)
51 {
52         for (const Zone::Ptr& zone : ConfigType::GetObjectsByType<Zone>()) {
53                 if (!addRowFn(zone, LivestatusGroupByNone, Empty))
54                         return;
55         }
56 }
57
58 Value ZonesTable::NameAccessor(const Value& row)
59 {
60         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
61
62         if (!zone)
63                 return Empty;
64
65         return zone->GetName();
66 }
67
68 Value ZonesTable::ParentAccessor(const Value& row)
69 {
70         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
71
72         if (!zone)
73                 return Empty;
74
75         Zone::Ptr parent_zone = zone->GetParent();
76
77         if (!parent_zone)
78                 return Empty;
79
80         return parent_zone->GetName();
81 }
82
83 Value ZonesTable::EndpointsAccessor(const Value& row)
84 {
85         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
86
87         if (!zone)
88                 return Empty;
89
90         std::set<Endpoint::Ptr> endpoints = zone->GetEndpoints();
91
92         ArrayData result;
93
94         for (const Endpoint::Ptr& endpoint : endpoints) {
95                 result.push_back(endpoint->GetName());
96         }
97
98         return new Array(std::move(result));
99 }
100
101 Value ZonesTable::GlobalAccessor(const Value& row)
102 {
103         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
104
105         if (!zone)
106                 return Empty;
107
108         return zone->GetGlobal() ? 1 : 0;
109 }