1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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. *
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. *
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 ******************************************************************************/
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>
42 using namespace icinga;
44 Table::Table(LivestatusGroupByType type)
45 : m_GroupByType(type), m_GroupByObject(Empty)
48 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
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();
90 void Table::AddColumn(const String& name, const Column& column)
92 std::pair<String, Column> item = std::make_pair(name, column);
94 auto ret = m_Columns.insert(item);
97 ret.first->second = column;
100 Column Table::GetColumn(const String& name) const
103 String prefix = GetPrefix() + "_";
105 if (dname.Find(prefix) == 0)
106 dname = dname.SubStr(prefix.GetLength());
108 auto it = m_Columns.find(dname);
110 if (it == m_Columns.end())
111 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
116 std::vector<String> Table::GetColumnNames() const
118 std::vector<String> names;
120 for (const auto& kv : m_Columns) {
121 names.push_back(kv.first);
127 std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
129 std::vector<LivestatusRowValue> rs;
131 FetchRows(std::bind(&Table::FilteredAddRow, this, std::ref(rs), filter, limit, _1, _2, _3));
136 bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
138 if (limit != -1 && static_cast<int>(rs.size()) == limit)
141 if (!filter || filter->Apply(this, row)) {
142 LivestatusRowValue rval;
144 rval.GroupByType = groupByType;
145 rval.GroupByObject = groupByObject;
147 rs.emplace_back(std::move(rval));
153 Value Table::ZeroAccessor(const Value&)
158 Value Table::OneAccessor(const Value&)
163 Value Table::EmptyStringAccessor(const Value&)
168 Value Table::EmptyArrayAccessor(const Value&)
173 Value Table::EmptyDictionaryAccessor(const Value&)
175 return new Dictionary();
178 LivestatusGroupByType Table::GetGroupByType() const
180 return m_GroupByType;