]> granicus.if.org Git - icinga2/blob - lib/livestatus/endpointstable.cpp
Merge branch 'support/2.10'
[icinga2] / lib / livestatus / endpointstable.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/endpointstable.hpp"
4 #include "icinga/host.hpp"
5 #include "icinga/service.hpp"
6 #include "icinga/icingaapplication.hpp"
7 #include "remote/endpoint.hpp"
8 #include "remote/zone.hpp"
9 #include "base/configtype.hpp"
10 #include "base/objectlock.hpp"
11 #include "base/convert.hpp"
12 #include "base/utility.hpp"
13 #include <boost/tuple/tuple.hpp>
14 #include <boost/algorithm/string/replace.hpp>
15
16 using namespace icinga;
17
18 EndpointsTable::EndpointsTable()
19 {
20         AddColumns(this);
21 }
22
23 void EndpointsTable::AddColumns(Table *table, const String& prefix,
24         const Column::ObjectAccessor& objectAccessor)
25 {
26         table->AddColumn(prefix + "name", Column(&EndpointsTable::NameAccessor, objectAccessor));
27         table->AddColumn(prefix + "identity", Column(&EndpointsTable::IdentityAccessor, objectAccessor));
28         table->AddColumn(prefix + "node", Column(&EndpointsTable::NodeAccessor, objectAccessor));
29         table->AddColumn(prefix + "is_connected", Column(&EndpointsTable::IsConnectedAccessor, objectAccessor));
30         table->AddColumn(prefix + "zone", Column(&EndpointsTable::ZoneAccessor, objectAccessor));
31 }
32
33 String EndpointsTable::GetName() const
34 {
35         return "endpoints";
36 }
37
38 String EndpointsTable::GetPrefix() const
39 {
40         return "endpoint";
41 }
42
43 void EndpointsTable::FetchRows(const AddRowFunction& addRowFn)
44 {
45         for (const Endpoint::Ptr& endpoint : ConfigType::GetObjectsByType<Endpoint>()) {
46                 if (!addRowFn(endpoint, LivestatusGroupByNone, Empty))
47                         return;
48         }
49 }
50
51 Value EndpointsTable::NameAccessor(const Value& row)
52 {
53         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
54
55         if (!endpoint)
56                 return Empty;
57
58         return endpoint->GetName();
59 }
60
61 Value EndpointsTable::IdentityAccessor(const Value& row)
62 {
63         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
64
65         if (!endpoint)
66                 return Empty;
67
68         return endpoint->GetName();
69 }
70
71 Value EndpointsTable::NodeAccessor(const Value& row)
72 {
73         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
74
75         if (!endpoint)
76                 return Empty;
77
78         return IcingaApplication::GetInstance()->GetNodeName();
79 }
80
81 Value EndpointsTable::IsConnectedAccessor(const Value& row)
82 {
83         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
84
85         if (!endpoint)
86                 return Empty;
87
88         unsigned int is_connected = endpoint->GetConnected() ? 1 : 0;
89
90         /* if identity is equal to node, fake is_connected */
91         if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
92                 is_connected = 1;
93
94         return is_connected;
95 }
96
97 Value EndpointsTable::ZoneAccessor(const Value& row)
98 {
99         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
100
101         if (!endpoint)
102                 return Empty;
103
104         Zone::Ptr zone = endpoint->GetZone();
105
106         if (!zone)
107                 return Empty;
108
109         return zone->GetName();
110 }