1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://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/timeperiodstable.hpp"
33 #include "livestatus/logtable.hpp"
34 #include "livestatus/statehisttable.hpp"
35 #include "livestatus/filter.hpp"
36 #include "base/array.hpp"
37 #include "base/dictionary.hpp"
38 #include <boost/algorithm/string/case_conv.hpp>
39 #include <boost/tuple/tuple.hpp>
40 #include <boost/foreach.hpp>
41 #include <boost/bind.hpp>
43 using namespace icinga;
48 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
51 return make_shared<StatusTable>();
52 else if (name == "contactgroups")
53 return make_shared<ContactGroupsTable>();
54 else if (name == "contacts")
55 return make_shared<ContactsTable>();
56 else if (name == "hostgroups")
57 return make_shared<HostGroupsTable>();
58 else if (name == "hosts")
59 return make_shared<HostsTable>();
60 else if (name == "servicegroups")
61 return make_shared<ServiceGroupsTable>();
62 else if (name == "services")
63 return make_shared<ServicesTable>();
64 else if (name == "commands")
65 return make_shared<CommandsTable>();
66 else if (name == "comments")
67 return make_shared<CommentsTable>();
68 else if (name == "downtimes")
69 return make_shared<DowntimesTable>();
70 else if (name == "timeperiods")
71 return make_shared<TimePeriodsTable>();
72 else if (name == "log")
73 return make_shared<LogTable>(compat_log_path, from, until);
74 else if (name == "statehist")
75 return make_shared<StateHistTable>(compat_log_path, from, until);
76 else if (name == "endpoints")
77 return make_shared<EndpointsTable>();
82 void Table::AddColumn(const String& name, const Column& column)
84 std::pair<String, Column> item = std::make_pair(name, column);
86 std::pair<std::map<String, Column>::iterator, bool> ret = m_Columns.insert(item);
89 ret.first->second = column;
92 Column Table::GetColumn(const String& name) const
95 String prefix = GetPrefix() + "_";
97 if (dname.Find(prefix) == 0)
98 dname = dname.SubStr(prefix.GetLength());
100 std::map<String, Column>::const_iterator it = m_Columns.find(dname);
102 if (it == m_Columns.end())
103 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
108 std::vector<String> Table::GetColumnNames(void) const
110 std::vector<String> names;
113 BOOST_FOREACH(boost::tie(name, boost::tuples::ignore), m_Columns) {
114 names.push_back(name);
120 std::vector<Value> Table::FilterRows(const Filter::Ptr& filter)
122 std::vector<Value> rs;
124 FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1));
129 void Table::FilteredAddRow(std::vector<Value>& rs, const Filter::Ptr& filter, const Value& row)
131 if (!filter || filter->Apply(GetSelf(), row))
135 Value Table::ZeroAccessor(const Value&)
140 Value Table::OneAccessor(const Value&)
145 Value Table::EmptyStringAccessor(const Value&)
150 Value Table::EmptyArrayAccessor(const Value&)
152 return make_shared<Array>();
155 Value Table::EmptyDictionaryAccessor(const Value&)
157 return make_shared<Dictionary>();