]> granicus.if.org Git - icinga2/blob - lib/livestatus/table.cpp
Move all libraries into the lib/ directory
[icinga2] / lib / livestatus / table.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
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/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>
42
43 using namespace icinga;
44
45 Table::Table(void)
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 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>();
78
79         return Table::Ptr();
80 }
81
82 void Table::AddColumn(const String& name, const Column& column)
83 {
84         std::pair<String, Column> item = std::make_pair(name, column);
85
86         std::pair<std::map<String, Column>::iterator, bool> ret = m_Columns.insert(item);
87
88         if (!ret.second)
89                 ret.first->second = column;
90 }
91
92 Column Table::GetColumn(const String& name) const
93 {
94         String dname = name;
95         String prefix = GetPrefix() + "_";
96
97         if (dname.Find(prefix) == 0)
98                 dname = dname.SubStr(prefix.GetLength());
99
100         std::map<String, Column>::const_iterator it = m_Columns.find(dname);
101
102         if (it == m_Columns.end())
103                 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
104
105         return it->second;
106 }
107
108 std::vector<String> Table::GetColumnNames(void) const
109 {
110         std::vector<String> names;
111
112         String name;
113         BOOST_FOREACH(boost::tie(name, boost::tuples::ignore), m_Columns) {
114                 names.push_back(name);
115         }
116
117         return names;
118 }
119
120 std::vector<Value> Table::FilterRows(const Filter::Ptr& filter)
121 {
122         std::vector<Value> rs;
123
124         FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1));
125
126         return rs;
127 }
128
129 void Table::FilteredAddRow(std::vector<Value>& rs, const Filter::Ptr& filter, const Value& row)
130 {
131         if (!filter || filter->Apply(GetSelf(), row))
132                 rs.push_back(row);
133 }
134
135 Value Table::ZeroAccessor(const Value&)
136 {
137         return 0;
138 }
139
140 Value Table::OneAccessor(const Value&)
141 {
142         return 1;
143 }
144
145 Value Table::EmptyStringAccessor(const Value&)
146 {
147         return "";
148 }
149
150 Value Table::EmptyArrayAccessor(const Value&)
151 {
152         return make_shared<Array>();
153 }
154
155 Value Table::EmptyDictionaryAccessor(const Value&)
156 {
157         return make_shared<Dictionary>();
158 }