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