]> granicus.if.org Git - icinga2/blob - components/livestatus/endpointstable.cpp
Livestatus: Add endpoints table.
[icinga2] / components / livestatus / endpointstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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.h"
21 #include "icinga/host.h"
22 #include "icinga/service.h"
23 #include "icinga/icingaapplication.h"
24 #include "remote/endpoint.h"
25 #include "base/dynamictype.h"
26 #include "base/objectlock.h"
27 #include "base/convert.h"
28 #include "base/utility.h"
29 #include <boost/algorithm/string/classification.hpp>
30 #include <boost/foreach.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 }
50
51 String EndpointsTable::GetName(void) const
52 {
53         return "endpoints";
54 }
55
56 void EndpointsTable::FetchRows(const AddRowFunction& addRowFn)
57 {
58         BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects<Endpoint>()) {
59                 addRowFn(endpoint);
60         }
61 }
62
63 Value EndpointsTable::NameAccessor(const Value& row)
64 {
65         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
66
67         if (!endpoint)
68                 return Empty;
69
70         return endpoint->GetName();
71 }
72
73 Value EndpointsTable::IdentityAccessor(const Value& row)
74 {
75         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
76
77         if (!endpoint)
78                 return Empty;
79
80         return endpoint->GetName();
81 }
82
83 Value EndpointsTable::NodeAccessor(const Value& row)
84 {
85         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
86
87         if (!endpoint)
88                 return Empty;
89
90         return IcingaApplication::GetInstance()->GetNodeName();
91 }
92
93 Value EndpointsTable::IsConnectedAccessor(const Value& row)
94 {
95         Endpoint::Ptr endpoint = static_cast<Endpoint::Ptr>(row);
96
97         if (!endpoint)
98                 return Empty;
99
100         unsigned int is_connected = endpoint->IsConnected() ? 1 : 0;
101
102         /* if identity is equal to node, fake is_connected */
103         if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
104                 is_connected = 1;
105
106         return is_connected;
107 }
108
109
110