]> granicus.if.org Git - icinga2/blob - lib/livestatus/table.cpp
Ensure that *.icinga.com is used everywhere
[icinga2] / lib / livestatus / table.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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/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>
42
43 using namespace icinga;
44
45 Table::Table(LivestatusGroupByType type)
46     : m_GroupByType(type), m_GroupByObject(Empty)
47 { }
48
49 Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
50 {
51         if (name == "status")
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();
87
88         return Table::Ptr();
89 }
90
91 void Table::AddColumn(const String& name, const Column& column)
92 {
93         std::pair<String, Column> item = std::make_pair(name, column);
94
95         auto ret = m_Columns.insert(item);
96
97         if (!ret.second)
98                 ret.first->second = column;
99 }
100
101 Column Table::GetColumn(const String& name) const
102 {
103         String dname = name;
104         String prefix = GetPrefix() + "_";
105
106         if (dname.Find(prefix) == 0)
107                 dname = dname.SubStr(prefix.GetLength());
108
109         auto it = m_Columns.find(dname);
110
111         if (it == m_Columns.end())
112                 BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
113
114         return it->second;
115 }
116
117 std::vector<String> Table::GetColumnNames(void) const
118 {
119         std::vector<String> names;
120
121         for (const auto& kv : m_Columns) {
122                 names.push_back(kv.first);
123         }
124
125         return names;
126 }
127
128 std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
129 {
130         std::vector<LivestatusRowValue> rs;
131
132         FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, limit, _1, _2, _3));
133
134         return rs;
135 }
136
137 bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
138 {
139         if (limit != -1 && static_cast<int>(rs.size()) == limit)
140                 return false;
141
142         if (!filter || filter->Apply(this, row)) {
143                 LivestatusRowValue rval;
144                 rval.Row = row;
145                 rval.GroupByType = groupByType;
146                 rval.GroupByObject = groupByObject;
147
148                 rs.push_back(rval);
149
150         }
151
152         return true;
153 }
154
155 Value Table::ZeroAccessor(const Value&)
156 {
157         return 0;
158 }
159
160 Value Table::OneAccessor(const Value&)
161 {
162         return 1;
163 }
164
165 Value Table::EmptyStringAccessor(const Value&)
166 {
167         return "";
168 }
169
170 Value Table::EmptyArrayAccessor(const Value&)
171 {
172         return new Array();
173 }
174
175 Value Table::EmptyDictionaryAccessor(const Value&)
176 {
177         return new Dictionary();
178 }
179
180 LivestatusGroupByType Table::GetGroupByType(void) const
181 {
182         return m_GroupByType;
183 }