]> granicus.if.org Git - icinga2/blob - lib/livestatus/table.cpp
Merge pull request #5913 from mcktr/fix/itl-http-certificate-age-5610
[icinga2] / lib / livestatus / table.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/table.hpp"
21 #include "livestatus/statustable.hpp"
22 #include "livestatus/contactgroupstable.hpp"
23 #include "livestatus/contactstable.hpp"
24 #include "livestatus/hostgroupstable.hpp"
25 #include "livestatus/hoststable.hpp"
26 #include "livestatus/servicegroupstable.hpp"
27 #include "livestatus/servicestable.hpp"
28 #include "livestatus/commandstable.hpp"
29 #include "livestatus/commentstable.hpp"
30 #include "livestatus/downtimestable.hpp"
31 #include "livestatus/endpointstable.hpp"
32 #include "livestatus/zonestable.hpp"
33 #include "livestatus/timeperiodstable.hpp"
34 #include "livestatus/logtable.hpp"
35 #include "livestatus/statehisttable.hpp"
36 #include "livestatus/filter.hpp"
37 #include "base/array.hpp"
38 #include "base/dictionary.hpp"
39 #include <boost/algorithm/string/case_conv.hpp>
40 #include <boost/tuple/tuple.hpp>
41
42 using namespace icinga;
43
44 Table::Table(LivestatusGroupByType type)
45         : m_GroupByType(type), m_GroupByObject(Empty)
46 { }
47
48 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
49 {
50         if (name == "status")
51                 return new StatusTable();
52         else if (name == "contactgroups")
53                 return new ContactGroupsTable();
54         else if (name == "contacts")
55                 return new ContactsTable();
56         else if (name == "hostgroups")
57                 return new HostGroupsTable();
58         else if (name == "hosts")
59                 return new HostsTable();
60         else if (name == "hostsbygroup")
61                 return new HostsTable(LivestatusGroupByHostGroup);
62         else if (name == "servicegroups")
63                 return new ServiceGroupsTable();
64         else if (name == "services")
65                 return new ServicesTable();
66         else if (name == "servicesbygroup")
67                 return new ServicesTable(LivestatusGroupByServiceGroup);
68         else if (name == "servicesbyhostgroup")
69                 return new ServicesTable(LivestatusGroupByHostGroup);
70         else if (name == "commands")
71                 return new CommandsTable();
72         else if (name == "comments")
73                 return new CommentsTable();
74         else if (name == "downtimes")
75                 return new DowntimesTable();
76         else if (name == "timeperiods")
77                 return new TimePeriodsTable();
78         else if (name == "log")
79                 return new LogTable(compat_log_path, from, until);
80         else if (name == "statehist")
81                 return new StateHistTable(compat_log_path, from, until);
82         else if (name == "endpoints")
83                 return new EndpointsTable();
84         else if (name == "zones")
85                 return new ZonesTable();
86
87         return nullptr;
88 }
89
90 void Table::AddColumn(const String& name, const Column& column)
91 {
92         std::pair<String, Column> item = std::make_pair(name, column);
93
94         auto ret = m_Columns.insert(item);
95
96         if (!ret.second)
97                 ret.first->second = column;
98 }
99
100 Column Table::GetColumn(const String& name) const
101 {
102         String dname = name;
103         String prefix = GetPrefix() + "_";
104
105         if (dname.Find(prefix) == 0)
106                 dname = dname.SubStr(prefix.GetLength());
107
108         auto it = m_Columns.find(dname);
109
110         if (it == m_Columns.end())
111                 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
112
113         return it->second;
114 }
115
116 std::vector<String> Table::GetColumnNames() const
117 {
118         std::vector<String> names;
119
120         for (const auto& kv : m_Columns) {
121                 names.push_back(kv.first);
122         }
123
124         return names;
125 }
126
127 std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
128 {
129         std::vector<LivestatusRowValue> rs;
130
131         FetchRows(std::bind(&Table::FilteredAddRow, this, std::ref(rs), filter, limit, _1, _2, _3));
132
133         return rs;
134 }
135
136 bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
137 {
138         if (limit != -1 && static_cast<int>(rs.size()) == limit)
139                 return false;
140
141         if (!filter || filter->Apply(this, row)) {
142                 LivestatusRowValue rval;
143                 rval.Row = row;
144                 rval.GroupByType = groupByType;
145                 rval.GroupByObject = groupByObject;
146
147                 rs.emplace_back(std::move(rval));
148         }
149
150         return true;
151 }
152
153 Value Table::ZeroAccessor(const Value&)
154 {
155         return 0;
156 }
157
158 Value Table::OneAccessor(const Value&)
159 {
160         return 1;
161 }
162
163 Value Table::EmptyStringAccessor(const Value&)
164 {
165         return "";
166 }
167
168 Value Table::EmptyArrayAccessor(const Value&)
169 {
170         return new Array();
171 }
172
173 Value Table::EmptyDictionaryAccessor(const Value&)
174 {
175         return new Dictionary();
176 }
177
178 LivestatusGroupByType Table::GetGroupByType() const
179 {
180         return m_GroupByType;
181 }