2013-03-10 03:09:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2013-03-10 03:09:01 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "livestatus/table.hpp"
|
|
|
|
#include "livestatus/statustable.hpp"
|
|
|
|
#include "livestatus/contactgroupstable.hpp"
|
|
|
|
#include "livestatus/contactstable.hpp"
|
|
|
|
#include "livestatus/hostgroupstable.hpp"
|
|
|
|
#include "livestatus/hoststable.hpp"
|
|
|
|
#include "livestatus/servicegroupstable.hpp"
|
|
|
|
#include "livestatus/servicestable.hpp"
|
|
|
|
#include "livestatus/commandstable.hpp"
|
|
|
|
#include "livestatus/commentstable.hpp"
|
|
|
|
#include "livestatus/downtimestable.hpp"
|
|
|
|
#include "livestatus/endpointstable.hpp"
|
2015-07-14 18:08:55 +02:00
|
|
|
#include "livestatus/zonestable.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "livestatus/timeperiodstable.hpp"
|
|
|
|
#include "livestatus/logtable.hpp"
|
|
|
|
#include "livestatus/statehisttable.hpp"
|
|
|
|
#include "livestatus/filter.hpp"
|
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
2014-06-25 11:30:27 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-02-13 15:50:20 +01:00
|
|
|
Table::Table(LivestatusGroupByType type)
|
2017-12-19 15:50:05 +01:00
|
|
|
: m_GroupByType(type), m_GroupByObject(Empty)
|
2013-03-10 03:09:01 +01:00
|
|
|
{ }
|
|
|
|
|
2013-11-07 14:04:13 +01:00
|
|
|
Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-10 09:23:13 +01:00
|
|
|
if (name == "status")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new StatusTable();
|
2013-03-10 09:55:46 +01:00
|
|
|
else if (name == "contactgroups")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new ContactGroupsTable();
|
2013-03-10 09:55:46 +01:00
|
|
|
else if (name == "contacts")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new ContactsTable();
|
2013-07-09 18:33:27 +02:00
|
|
|
else if (name == "hostgroups")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new HostGroupsTable();
|
2013-03-10 18:49:14 +01:00
|
|
|
else if (name == "hosts")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new HostsTable();
|
2015-02-13 15:50:20 +01:00
|
|
|
else if (name == "hostsbygroup")
|
|
|
|
return new HostsTable(LivestatusGroupByHostGroup);
|
2013-07-09 18:33:27 +02:00
|
|
|
else if (name == "servicegroups")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new ServiceGroupsTable();
|
2013-03-10 22:20:13 +01:00
|
|
|
else if (name == "services")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new ServicesTable();
|
2015-02-13 15:50:20 +01:00
|
|
|
else if (name == "servicesbygroup")
|
|
|
|
return new ServicesTable(LivestatusGroupByServiceGroup);
|
|
|
|
else if (name == "servicesbyhostgroup")
|
|
|
|
return new ServicesTable(LivestatusGroupByHostGroup);
|
2013-07-09 18:09:03 +02:00
|
|
|
else if (name == "commands")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new CommandsTable();
|
2013-03-10 22:20:13 +01:00
|
|
|
else if (name == "comments")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new CommentsTable();
|
2013-03-10 22:20:13 +01:00
|
|
|
else if (name == "downtimes")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new DowntimesTable();
|
2013-07-11 13:03:14 +02:00
|
|
|
else if (name == "timeperiods")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new TimePeriodsTable();
|
2013-07-09 17:46:48 +02:00
|
|
|
else if (name == "log")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new LogTable(compat_log_path, from, until);
|
2013-11-06 13:29:00 +01:00
|
|
|
else if (name == "statehist")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new StateHistTable(compat_log_path, from, until);
|
2014-03-20 18:53:08 +01:00
|
|
|
else if (name == "endpoints")
|
2014-11-08 21:17:16 +01:00
|
|
|
return new EndpointsTable();
|
2015-07-14 18:08:55 +02:00
|
|
|
else if (name == "zones")
|
|
|
|
return new ZonesTable();
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
void Table::AddColumn(const String& name, const Column& column)
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::pair<String, Column> item = std::make_pair(name, column);
|
2013-03-10 18:49:14 +01:00
|
|
|
|
2016-08-27 19:56:12 +02:00
|
|
|
auto ret = m_Columns.insert(item);
|
2013-03-10 18:49:14 +01:00
|
|
|
|
|
|
|
if (!ret.second)
|
|
|
|
ret.first->second = column;
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 18:49:14 +01:00
|
|
|
Column Table::GetColumn(const String& name) const
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2014-06-25 11:30:27 +02:00
|
|
|
String dname = name;
|
|
|
|
String prefix = GetPrefix() + "_";
|
|
|
|
|
|
|
|
if (dname.Find(prefix) == 0)
|
|
|
|
dname = dname.SubStr(prefix.GetLength());
|
|
|
|
|
2016-08-27 08:33:15 +02:00
|
|
|
auto it = m_Columns.find(dname);
|
2013-03-10 09:23:13 +01:00
|
|
|
|
|
|
|
if (it == m_Columns.end())
|
2014-06-25 11:30:27 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
std::vector<String> Table::GetColumnNames() const
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<String> names;
|
2013-03-10 09:23:13 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const auto& kv : m_Columns) {
|
|
|
|
names.push_back(kv.first);
|
2013-03-10 05:10:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 09:23:13 +01:00
|
|
|
return names;
|
|
|
|
}
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2015-03-04 12:03:35 +01:00
|
|
|
std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2015-02-13 15:50:20 +01:00
|
|
|
std::vector<LivestatusRowValue> rs;
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2017-11-23 06:51:48 +01:00
|
|
|
FetchRows(std::bind(&Table::FilteredAddRow, this, std::ref(rs), filter, limit, _1, _2, _3));
|
2013-03-10 09:23:13 +01:00
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
2013-03-10 05:10:51 +01:00
|
|
|
|
2015-03-04 12:03:35 +01:00
|
|
|
bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
2016-08-24 19:59:13 +02:00
|
|
|
if (limit != -1 && static_cast<int>(rs.size()) == limit)
|
2015-03-04 12:03:35 +01:00
|
|
|
return false;
|
|
|
|
|
2015-02-13 15:50:20 +01:00
|
|
|
if (!filter || filter->Apply(this, row)) {
|
|
|
|
LivestatusRowValue rval;
|
|
|
|
rval.Row = row;
|
|
|
|
rval.GroupByType = groupByType;
|
|
|
|
rval.GroupByObject = groupByObject;
|
|
|
|
|
2017-11-30 08:19:58 +01:00
|
|
|
rs.emplace_back(std::move(rval));
|
2015-02-13 15:50:20 +01:00
|
|
|
}
|
2015-03-04 12:03:35 +01:00
|
|
|
|
|
|
|
return true;
|
2013-03-10 09:23:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 13:39:40 +01:00
|
|
|
Value Table::ZeroAccessor(const Value&)
|
2013-03-10 09:23:13 +01:00
|
|
|
{
|
|
|
|
return 0;
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
2013-03-10 09:55:46 +01:00
|
|
|
|
2013-11-05 13:39:40 +01:00
|
|
|
Value Table::OneAccessor(const Value&)
|
2013-03-10 09:55:46 +01:00
|
|
|
{
|
2013-10-03 03:48:12 +02:00
|
|
|
return 1;
|
2013-03-10 09:55:46 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 13:39:40 +01:00
|
|
|
Value Table::EmptyStringAccessor(const Value&)
|
2013-03-10 09:55:46 +01:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-11-05 13:39:40 +01:00
|
|
|
Value Table::EmptyArrayAccessor(const Value&)
|
2013-03-10 09:55:46 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
return new Array();
|
2013-03-10 09:55:46 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 13:39:40 +01:00
|
|
|
Value Table::EmptyDictionaryAccessor(const Value&)
|
2013-03-10 09:55:46 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
return new Dictionary();
|
2013-03-10 09:55:46 +01:00
|
|
|
}
|
2015-02-13 15:50:20 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
LivestatusGroupByType Table::GetGroupByType() const
|
2015-02-13 15:50:20 +01:00
|
|
|
{
|
|
|
|
return m_GroupByType;
|
|
|
|
}
|