1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/table.hpp"
4 #include "livestatus/statustable.hpp"
5 #include "livestatus/contactgroupstable.hpp"
6 #include "livestatus/contactstable.hpp"
7 #include "livestatus/hostgroupstable.hpp"
8 #include "livestatus/hoststable.hpp"
9 #include "livestatus/servicegroupstable.hpp"
10 #include "livestatus/servicestable.hpp"
11 #include "livestatus/commandstable.hpp"
12 #include "livestatus/commentstable.hpp"
13 #include "livestatus/downtimestable.hpp"
14 #include "livestatus/endpointstable.hpp"
15 #include "livestatus/zonestable.hpp"
16 #include "livestatus/timeperiodstable.hpp"
17 #include "livestatus/logtable.hpp"
18 #include "livestatus/statehisttable.hpp"
19 #include "livestatus/filter.hpp"
20 #include "base/array.hpp"
21 #include "base/dictionary.hpp"
22 #include <boost/algorithm/string/case_conv.hpp>
23 #include <boost/tuple/tuple.hpp>
25 using namespace icinga;
27 Table::Table(LivestatusGroupByType type)
28 : m_GroupByType(type), m_GroupByObject(Empty)
31 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
34 return new StatusTable();
35 else if (name == "contactgroups")
36 return new ContactGroupsTable();
37 else if (name == "contacts")
38 return new ContactsTable();
39 else if (name == "hostgroups")
40 return new HostGroupsTable();
41 else if (name == "hosts")
42 return new HostsTable();
43 else if (name == "hostsbygroup")
44 return new HostsTable(LivestatusGroupByHostGroup);
45 else if (name == "servicegroups")
46 return new ServiceGroupsTable();
47 else if (name == "services")
48 return new ServicesTable();
49 else if (name == "servicesbygroup")
50 return new ServicesTable(LivestatusGroupByServiceGroup);
51 else if (name == "servicesbyhostgroup")
52 return new ServicesTable(LivestatusGroupByHostGroup);
53 else if (name == "commands")
54 return new CommandsTable();
55 else if (name == "comments")
56 return new CommentsTable();
57 else if (name == "downtimes")
58 return new DowntimesTable();
59 else if (name == "timeperiods")
60 return new TimePeriodsTable();
61 else if (name == "log")
62 return new LogTable(compat_log_path, from, until);
63 else if (name == "statehist")
64 return new StateHistTable(compat_log_path, from, until);
65 else if (name == "endpoints")
66 return new EndpointsTable();
67 else if (name == "zones")
68 return new ZonesTable();
73 void Table::AddColumn(const String& name, const Column& column)
75 std::pair<String, Column> item = std::make_pair(name, column);
77 auto ret = m_Columns.insert(item);
80 ret.first->second = column;
83 Column Table::GetColumn(const String& name) const
86 String prefix = GetPrefix() + "_";
88 if (dname.Find(prefix) == 0)
89 dname = dname.SubStr(prefix.GetLength());
91 auto it = m_Columns.find(dname);
93 if (it == m_Columns.end())
94 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
99 std::vector<String> Table::GetColumnNames() const
101 std::vector<String> names;
103 for (const auto& kv : m_Columns) {
104 names.push_back(kv.first);
110 std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
112 std::vector<LivestatusRowValue> rs;
114 FetchRows(std::bind(&Table::FilteredAddRow, this, std::ref(rs), filter, limit, _1, _2, _3));
119 bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
121 if (limit != -1 && static_cast<int>(rs.size()) == limit)
124 if (!filter || filter->Apply(this, row)) {
125 LivestatusRowValue rval;
127 rval.GroupByType = groupByType;
128 rval.GroupByObject = groupByObject;
130 rs.emplace_back(std::move(rval));
136 Value Table::ZeroAccessor(const Value&)
141 Value Table::OneAccessor(const Value&)
146 Value Table::EmptyStringAccessor(const Value&)
151 Value Table::EmptyArrayAccessor(const Value&)
156 Value Table::EmptyDictionaryAccessor(const Value&)
158 return new Dictionary();
161 LivestatusGroupByType Table::GetGroupByType() const
163 return m_GroupByType;