]> granicus.if.org Git - icinga2/blob - lib/livestatus/zonestable.cpp
Update copyright headers for 2016
[icinga2] / lib / livestatus / zonestable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
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 #include <boost/foreach.hpp>
24
25 using namespace icinga;
26
27 ZonesTable::ZonesTable(void)
28 {
29         AddColumns(this);
30 }
31
32 void ZonesTable::AddColumns(Table *table, const String& prefix,
33     const Column::ObjectAccessor& objectAccessor)
34 {
35         table->AddColumn(prefix + "name", Column(&ZonesTable::NameAccessor, objectAccessor));
36         table->AddColumn(prefix + "parent", Column(&ZonesTable::ParentAccessor, objectAccessor));
37         table->AddColumn(prefix + "endpoints", Column(&ZonesTable::EndpointsAccessor, objectAccessor));
38         table->AddColumn(prefix + "global", Column(&ZonesTable::GlobalAccessor, objectAccessor));
39 }
40
41 String ZonesTable::GetName(void) const
42 {
43         return "zones";
44 }
45
46 String ZonesTable::GetPrefix(void) const
47 {
48         return "zone";
49 }
50
51 void ZonesTable::FetchRows(const AddRowFunction& addRowFn)
52 {
53         BOOST_FOREACH(const Zone::Ptr& zone, ConfigType::GetObjectsByType<Zone>()) {
54                 if (!addRowFn(zone, LivestatusGroupByNone, Empty))
55                         return;
56         }
57 }
58
59 Value ZonesTable::NameAccessor(const Value& row)
60 {
61         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
62
63         if (!zone)
64                 return Empty;
65
66         return zone->GetName();
67 }
68
69 Value ZonesTable::ParentAccessor(const Value& row)
70 {
71         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
72
73         if (!zone)
74                 return Empty;
75
76         Zone::Ptr parent_zone = zone->GetParent();
77
78         if (!parent_zone)
79                 return Empty;
80
81         return parent_zone->GetName();
82 }
83
84 Value ZonesTable::EndpointsAccessor(const Value& row)
85 {
86         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
87
88         if (!zone)
89                 return Empty;
90
91         std::set<Endpoint::Ptr> endpoints = zone->GetEndpoints();
92
93         Array::Ptr endpoint_names = new Array();
94
95         BOOST_FOREACH(const Endpoint::Ptr endpoint, endpoints) {
96                 endpoint_names->Add(endpoint->GetName());
97         }
98
99         if (!endpoint_names)
100                 return Empty;
101
102         return endpoint_names;
103 }
104
105 Value ZonesTable::GlobalAccessor(const Value& row)
106 {
107         Zone::Ptr zone = static_cast<Zone::Ptr>(row);
108
109         if (!zone)
110                 return Empty;
111
112         return zone->GetGlobal() ? 1 : 0;
113 }