icinga2/lib/livestatus/table.cpp
Yonas Habteab 91c7e60df8 Replace all existing copyright headers with SPDX headers
I've used the following command to replace the original copyright header
lines in a C-style comment block:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

For files that use shell-style comments (#) like CMakeLists.txt, I've
used this command:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

And for SQL files:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```
2026-02-04 14:00:05 +01:00

166 lines
4.3 KiB
C++

// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#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"
#include "livestatus/zonestable.hpp"
#include "livestatus/timeperiodstable.hpp"
#include "livestatus/logtable.hpp"
#include "livestatus/statehisttable.hpp"
#include "livestatus/filter.hpp"
#include "base/array.hpp"
#include "base/dictionary.hpp"
#include <boost/algorithm/string/case_conv.hpp>
using namespace icinga;
Table::Table(LivestatusGroupByType type)
: m_GroupByType(type), m_GroupByObject(Empty)
{ }
Table::Ptr Table::GetByName(const String& name, const String& compat_log_path, const unsigned long& from, const unsigned long& until)
{
if (name == "status")
return new StatusTable();
else if (name == "contactgroups")
return new ContactGroupsTable();
else if (name == "contacts")
return new ContactsTable();
else if (name == "hostgroups")
return new HostGroupsTable();
else if (name == "hosts")
return new HostsTable();
else if (name == "hostsbygroup")
return new HostsTable(LivestatusGroupByHostGroup);
else if (name == "servicegroups")
return new ServiceGroupsTable();
else if (name == "services")
return new ServicesTable();
else if (name == "servicesbygroup")
return new ServicesTable(LivestatusGroupByServiceGroup);
else if (name == "servicesbyhostgroup")
return new ServicesTable(LivestatusGroupByHostGroup);
else if (name == "commands")
return new CommandsTable();
else if (name == "comments")
return new CommentsTable();
else if (name == "downtimes")
return new DowntimesTable();
else if (name == "timeperiods")
return new TimePeriodsTable();
else if (name == "log")
return new LogTable(compat_log_path, from, until);
else if (name == "statehist")
return new StateHistTable(compat_log_path, from, until);
else if (name == "endpoints")
return new EndpointsTable();
else if (name == "zones")
return new ZonesTable();
return nullptr;
}
void Table::AddColumn(const String& name, const Column& column)
{
std::pair<String, Column> item = std::make_pair(name, column);
auto ret = m_Columns.insert(item);
if (!ret.second)
ret.first->second = column;
}
Column Table::GetColumn(const String& name) const
{
String dname = name;
String prefix = GetPrefix() + "_";
if (dname.Find(prefix) == 0)
dname = dname.SubStr(prefix.GetLength());
auto it = m_Columns.find(dname);
if (it == m_Columns.end())
BOOST_THROW_EXCEPTION(std::invalid_argument("Column '" + dname + "' does not exist in table '" + GetName() + "'."));
return it->second;
}
std::vector<String> Table::GetColumnNames() const
{
std::vector<String> names;
for (const auto& kv : m_Columns) {
names.push_back(kv.first);
}
return names;
}
std::vector<LivestatusRowValue> Table::FilterRows(const Filter::Ptr& filter, int limit)
{
std::vector<LivestatusRowValue> rs;
FetchRows([this, filter, limit, &rs](const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject) {
return FilteredAddRow(rs, filter, limit, row, groupByType, groupByObject);
});
return rs;
}
bool Table::FilteredAddRow(std::vector<LivestatusRowValue>& rs, const Filter::Ptr& filter, int limit, const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
{
if (limit != -1 && static_cast<int>(rs.size()) == limit)
return false;
if (!filter || filter->Apply(this, row)) {
LivestatusRowValue rval;
rval.Row = row;
rval.GroupByType = groupByType;
rval.GroupByObject = groupByObject;
rs.emplace_back(std::move(rval));
}
return true;
}
Value Table::ZeroAccessor(const Value&)
{
return 0;
}
Value Table::OneAccessor(const Value&)
{
return 1;
}
Value Table::EmptyStringAccessor(const Value&)
{
return "";
}
Value Table::EmptyArrayAccessor(const Value&)
{
return new Array();
}
Value Table::EmptyDictionaryAccessor(const Value&)
{
return new Dictionary();
}
LivestatusGroupByType Table::GetGroupByType() const
{
return m_GroupByType;
}