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