]> granicus.if.org Git - icinga2/blob - lib/livestatus/endpointstable.cpp
Merge branch 'support/2.3'
[icinga2] / lib / livestatus / endpointstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://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/endpointstable.hpp"
21 #include "icinga/host.hpp"
22 #include "icinga/service.hpp"
23 #include "icinga/icingaapplication.hpp"
24 #include "remote/endpoint.hpp"
25 #include "remote/zone.hpp"
26 #include "base/configtype.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/convert.hpp"
29 #include "base/utility.hpp"
30 #include <boost/algorithm/string/classification.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/tuple/tuple.hpp>
33 #include <boost/algorithm/string/replace.hpp>
34 #include <boost/algorithm/string/split.hpp>
35
36 using namespace icinga;
37
38 EndpointsTable::EndpointsTable(void)
39 {
40         AddColumns(this);
41 }
42
43 void EndpointsTable::AddColumns(Table *table, const String& prefix,
44     const Column::ObjectAccessor& objectAccessor)
45 {
46         table->AddColumn(prefix + "name", Column(&EndpointsTable::NameAccessor, objectAccessor));
47         table->AddColumn(prefix + "identity", Column(&EndpointsTable::IdentityAccessor, objectAccessor));
48         table->AddColumn(prefix + "node", Column(&EndpointsTable::NodeAccessor, objectAccessor));
49         table->AddColumn(prefix + "is_connected", Column(&EndpointsTable::IsConnectedAccessor, objectAccessor));
50         table->AddColumn(prefix + "zone", Column(&EndpointsTable::ZoneAccessor, objectAccessor));
51 }
52
53 String EndpointsTable::GetName(void) const
54 {
55         return "endpoints";
56 }
57
58 String EndpointsTable::GetPrefix(void) const
59 {
60         return "endpoint";
61 }
62
63 void EndpointsTable::FetchRows(const AddRowFunction& addRowFn)
64 {
65         BOOST_FOREACH(const Endpoint::Ptr& endpoint, ConfigType::GetObjectsByType<Endpoint>()) {
66                 if (!addRowFn(endpoint, LivestatusGroupByNone, Empty))
67                         return;
68         }
69 }
70
71 Value EndpointsTable::NameAccessor(const Value& row)
72 {
73         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
74
75         if (!endpoint)
76                 return Empty;
77
78         return endpoint->GetName();
79 }
80
81 Value EndpointsTable::IdentityAccessor(const Value& row)
82 {
83         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
84
85         if (!endpoint)
86                 return Empty;
87
88         return endpoint->GetName();
89 }
90
91 Value EndpointsTable::NodeAccessor(const Value& row)
92 {
93         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
94
95         if (!endpoint)
96                 return Empty;
97
98         return IcingaApplication::GetInstance()->GetNodeName();
99 }
100
101 Value EndpointsTable::IsConnectedAccessor(const Value& row)
102 {
103         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
104
105         if (!endpoint)
106                 return Empty;
107
108         unsigned int is_connected = endpoint->GetConnected() ? 1 : 0;
109
110         /* if identity is equal to node, fake is_connected */
111         if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
112                 is_connected = 1;
113
114         return is_connected;
115 }
116
117 Value EndpointsTable::ZoneAccessor(const Value& row)
118 {
119         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
120
121         if (!endpoint)
122                 return Empty;
123
124         Zone::Ptr zone = endpoint->GetZone();
125
126         if (!zone)
127                 return Empty;
128
129         return zone->GetName();
130 }