]> granicus.if.org Git - icinga2/blob - lib/livestatus/endpointstable.cpp
Docs: Explain across midnight time periods with an overlapping range
[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/algorithm/string/replace.hpp>
14
15 using namespace icinga;
16
17 EndpointsTable::EndpointsTable()
18 {
19         AddColumns(this);
20 }
21
22 void EndpointsTable::AddColumns(Table *table, const String& prefix,
23         const Column::ObjectAccessor& objectAccessor)
24 {
25         table->AddColumn(prefix + "name", Column(&EndpointsTable::NameAccessor, objectAccessor));
26         table->AddColumn(prefix + "identity", Column(&EndpointsTable::IdentityAccessor, objectAccessor));
27         table->AddColumn(prefix + "node", Column(&EndpointsTable::NodeAccessor, objectAccessor));
28         table->AddColumn(prefix + "is_connected", Column(&EndpointsTable::IsConnectedAccessor, objectAccessor));
29         table->AddColumn(prefix + "zone", Column(&EndpointsTable::ZoneAccessor, objectAccessor));
30 }
31
32 String EndpointsTable::GetName() const
33 {
34         return "endpoints";
35 }
36
37 String EndpointsTable::GetPrefix() const
38 {
39         return "endpoint";
40 }
41
42 void EndpointsTable::FetchRows(const AddRowFunction& addRowFn)
43 {
44         for (const Endpoint::Ptr& endpoint : ConfigType::GetObjectsByType<Endpoint>()) {
45                 if (!addRowFn(endpoint, LivestatusGroupByNone, Empty))
46                         return;
47         }
48 }
49
50 Value EndpointsTable::NameAccessor(const Value& row)
51 {
52         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
53
54         if (!endpoint)
55                 return Empty;
56
57         return endpoint->GetName();
58 }
59
60 Value EndpointsTable::IdentityAccessor(const Value& row)
61 {
62         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
63
64         if (!endpoint)
65                 return Empty;
66
67         return endpoint->GetName();
68 }
69
70 Value EndpointsTable::NodeAccessor(const Value& row)
71 {
72         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
73
74         if (!endpoint)
75                 return Empty;
76
77         return IcingaApplication::GetInstance()->GetNodeName();
78 }
79
80 Value EndpointsTable::IsConnectedAccessor(const Value& row)
81 {
82         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
83
84         if (!endpoint)
85                 return Empty;
86
87         unsigned int is_connected = endpoint->GetConnected() ? 1 : 0;
88
89         /* if identity is equal to node, fake is_connected */
90         if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
91                 is_connected = 1;
92
93         return is_connected;
94 }
95
96 Value EndpointsTable::ZoneAccessor(const Value& row)
97 {
98         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
99
100         if (!endpoint)
101                 return Empty;
102
103         Zone::Ptr zone = endpoint->GetZone();
104
105         if (!zone)
106                 return Empty;
107
108         return zone->GetName();
109 }