1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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>
41 #include <boost/bind.hpp>
43 using namespace icinga;
45 Table::Table(LivestatusGroupByType type)
46 : m_GroupByType(type), m_GroupByObject(Empty)
49 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
52 return new StatusTable();
53 else if (name == "contactgroups")
54 return new ContactGroupsTable();
55 else if (name == "contacts")
56 return new ContactsTable();
57 else if (name == "hostgroups")
58 return new HostGroupsTable();
59 else if (name == "hosts")
60 return new HostsTable();
61 else if (name == "hostsbygroup")
62 return new HostsTable(LivestatusGroupByHostGroup);
63 else if (name == "servicegroups")
64 return new ServiceGroupsTable();
65 else if (name == "services")
66 return new ServicesTable();
67 else if (name == "servicesbygroup")
68 return new ServicesTable(LivestatusGroupByServiceGroup);
69 else if (name == "servicesbyhostgroup")
70 return new ServicesTable(LivestatusGroupByHostGroup);
71 else if (name == "commands")
72 return new CommandsTable();
73 else if (name == "comments")
74 return new CommentsTable();
75 else if (name == "downtimes")
76 return new DowntimesTable();
77 else if (name == "timeperiods")
78 return new TimePeriodsTable();
79 else if (name == "log")
80 return new LogTable(compat_log_path, from, until);
81 else if (name == "statehist")
82 return new StateHistTable(compat_log_path, from, until);
83 else if (name == "endpoints")
84 return new EndpointsTable();
85 else if (name == "zones")
86 return new ZonesTable();
91 void Table::AddColumn(const String& name, const Column& column)
93 std::pair<String, Column> item = std::make_pair(name, column);
95 std::pair<std::map<String, Column>::iterator, bool> ret = m_Columns.insert(item);
98 ret.first->second = column;
101 Column Table::GetColumn(const String& name) const
104 String prefix = GetPrefix() + "_";
106 if (dname.Find(prefix) == 0)
107 dname = dname.SubStr(prefix.GetLength());
109 std::map<String, Column>::const_iterator it = m_Columns.find(dname);
111 if (it == m_Columns.end())
112 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
117 std::vector<String> Table::GetColumnNames(void) const
119 std::vector<String> names;
121 for (const auto& kv : m_Columns) {
122 names.push_back(kv.first);
128 std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
130 std::vector<LivestatusRowValue> rs;
132 FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, limit, _1, _2, _3));
137 bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
139 if (limit != -1 && static_cast<int>(rs.size()) == limit)
142 if (!filter || filter->Apply(this, row)) {
143 LivestatusRowValue rval;
145 rval.GroupByType = groupByType;
146 rval.GroupByObject = groupByObject;
155 Value Table::ZeroAccessor(const Value&)
160 Value Table::OneAccessor(const Value&)
165 Value Table::EmptyStringAccessor(const Value&)
170 Value Table::EmptyArrayAccessor(const Value&)
175 Value Table::EmptyDictionaryAccessor(const Value&)
177 return new Dictionary();
180 LivestatusGroupByType Table::GetGroupByType(void) const
182 return m_GroupByType;